我想在广播火灾时使用Loader管理器及其在广播接收器内的回叫?

时间:2017-11-17 11:10:22

标签: android android-contentprovider android-broadcast

我无法获得getSupportLoaderManger或getLoaderManager

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

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它在活动和片段中运行良好。

如何在broadCast接收器中使用LoaderManager?

2 个答案:

答案 0 :(得分:0)

请查看https://developer.android.com/reference/android/app/LoaderManager.html

LoaderManager仅用于Activity和Fragment

答案 1 :(得分:0)

为此,您可以选择透明活动。

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

所以你将从广播中调用此活动,它将会更新

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

所以这将解决你希望如此的问题

这有些间接的方式来实现