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

百般思念 提交于 2020-01-05 08:39:13

问题


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?


回答1:


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

Manifest.xml

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

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

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




回答2:


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

LoaderManager is only used for Activity and Fragment



来源:https://stackoverflow.com/questions/47349312/i-would-like-to-use-loader-manager-and-its-call-backs-inside-broadcast-receiver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!