广播起火时,我想在广播接收器内部使用Loader管理器及其回调吗? [英] I would like to use Loader manager and its call backs inside broadcast receiver when broadcast fire?

查看:110
本文介绍了广播起火时,我想在广播接收器内部使用Loader管理器及其回调吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法获取getSupportLoaderManger或getLoaderManager

I m not able to get getSupportLoaderManger or getLoaderManager

我很困惑如何解决这个问题,

I m confuse how to resolve this,

public class MyBroadCastReceiver extends BroadcastReceiver implements LoaderManager.LoaderCallbacks<Cursor> {
    int cpValue;
    private CursorLoader cursorLoader;
    private Context mContext;

    @Override
    public void onReceive(Context context, Intent intent) {
        mContext = context;
        mContext.getSupportLoaderManager().initLoader(1, null, context);
    }

    private void doTheTask(MyAsync task, Intent intent) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // Android 4.4 (API 19) and above
            task.execute(intent);
        } else {
            // Android 3.0 to Android 4.3
            task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, intent);
        }
    }

    @Override
    public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
        cursorLoader = new CursorLoader(mContext, Uri.parse("content://com.MyApplication.Provider/cte"), null, null, null, null);
        return cursorLoader;
    }

    @Override
    public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor) {
        if (cursor != null) {
            cursor.moveToFirst();
            String res = "0";
            while (!cursor.isAfterLast()) {
                res = cursor.getString(cursor.getColumnIndex("present"));
                cursor.moveToNext();
            }
            cpValue = Integer.valueOf(res);
        } else {
            cpValue = 0;
        }
    }

    @Override
    public void onLoaderReset(Loader<Cursor> arg0) {
    }
}

我的问题是,我如何才能在broadCast中使用loaderManager,它在活动和片段方面都可以正常工作.

My Question is So how can i use loaderManager in broadCast it is working fine in activity and fragment.

如何在broadCast Receiver内使用LoaderManager?

How Can I use LoaderManager inside the broadCast Receiver?

推荐答案

为此,您可以进行透明"活动.

For that One way is there you can go for the Transparent activity.

Manifest.xml

Manifest.xml

<activity
    android:name=".activity.TransparentActivity"
    android:theme="@style/Theme.Transparent">
</activity>

Style.xml

Style.xml

<style name="Theme.Transparent" parent="android:Theme">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">false</item>
</style>

TransparentActivity.java

TransparentActivity.java

import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v7.app.AppCompatActivity;

public class TransparentActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> {

    private CursorLoader cursorLoader;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportLoaderManager().initLoader(1, null, this);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
        cursorLoader = new CursorLoader(this, Uri.parse("content://com.example.yourprovider/cte"), null, null, null, null);
        return cursorLoader;
    }

    @Override
    public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor) {
        if (cursor != null) {
           //Do your stuff
        } 
        finish();
    }

    @Override
    public void onLoaderReset(Loader<Cursor> arg0) {
    }
}

因此,您将从广播中调用此活动,它将更新

So You will call this activity from the broadcast and it will going to update

Intent newIntent = new Intent(context,TransparentActivity.class);
newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(newIntent);

所以我希望这样可以解决您的问题

So this will going to resolve your issues I hope so

这是实现这一目标的一些间接方式

This some how indirect way for this to achieve

这篇关于广播起火时,我想在广播接收器内部使用Loader管理器及其回调吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆