在AppWidgetProvider中未调用onReceive

时间:2019-03-14 03:41:29

标签: broadcastreceiver android-broadcast intentservice appwidgetprovider

我正在尝试从IntentService发送广播,将其下载,解析并将数据写入文件之后。我的数据正在正确下载和保存,并且广播似乎正在正确发送。但是,我在AppWidgetProvider类中没有从onReceive获得回调。

这是我的IntentService中的onHandleIntent方法。这是广播的发送来源:

@Override
protected void onHandleIntent(@Nullable Intent intent) {
    if(intent.getAction().equals(WeatherWidgetProvider.ACTION_UPDATE_WEATHER)) {
        Log.i(TAG, "onHandleWork: Current intent action = " + intent.getAction());

        Log.i(TAG, "onHandleWork: Service Started");

        createConnection();

        if(dataUpdatedSuccessfully) {
            Log.i(TAG, "onHandleWork: Sending broadcast");
            Intent updateIntent = new Intent();
            updateIntent.setAction(WeatherWidgetProvider.ACTION_UPDATE_WEATHER);
            updateIntent.addCategory(Intent.CATEGORY_DEFAULT);
            sendBroadcast(intent);
        }

        dataUpdatedSuccessfully = false;
    }

    else {
        Log.i(TAG, "onHandleIntent: Incorrect Action");
    }
}

这是我的AppWidgetProvider中的onReceive方法:

@Override
public void onReceive(Context context, Intent intent) {
    Log.i(TAG, "onReceive: Response Received");

    String action = intent.getAction();

    if(action != null && action.equals(ACTION_UPDATE_WEATHER)) {
        Log.i(TAG, "onReceive: Updating Weather");
        Toast.makeText(context, "Updating Weather", Toast.LENGTH_SHORT).show();
    }

    else {
        Log.i(TAG, "onReceive: Incorrect action: " + action);
    }

    super.onReceive(context, intent);
}

这是清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mack.john.mackjohn_ce03">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    android:usesCleartextTraffic="true" >

    <receiver android:name=".WeatherWidgetProvider" android:exported="true">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
            <action android:name="com.mack.john.ACTION_UPDATE_WEATHER"/>
        </intent-filter>

        <meta-data android:name="android.appwidget.provider" android:resource="@xml/provider_info"/>
    </receiver>

    <service android:name=".services.DownloadIntentService"/>

</application>

0 个答案:

没有答案