Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
408 views
in Technique[技术] by (71.8m points)

android - I would like to use Loader manager and its call backs inside broadcast receiver when broadcast fire?

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) {
    }
}

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

How Can I use LoaderManager inside the broadCast Receiver?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Please, look at https://developer.android.com/reference/android/app/LoaderManager.html

LoaderManager is only used for Activity and Fragment


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...