Oreo - Widget services and broadcast receiver: Not

2019-07-24 20:36发布

问题:

I have a widget that monitors wifi connectivity so I start a service to initiate a broadcast receiver to detect network changes. All works well except when I exit the main app: the service stops.

So I initiate in the widget an Alarm Manager that awakes roughly every minute and checks if the main app has exited. If that is the case I attempt to restart my wifi monitoring service and this time it crashes with the message:

Not allowed to start service Intent { cmp=package.CallbackNetworkWidgetService (has extras) }: app is in background uid UidRecord{f6e65b9 u0a154 RCVR idle change:idle|uncached procs:1 seq(0,0,0)}

The issue is with the oreo new intent/service model. It works until nougat.

Questions:

  1. is my approach correct?

  2. I'm a little bit lost with the notion of foreground/background service. The widget is a background svce? The wifi monitoring service/BR is also a background svce?

  3. What is the problem here?

Manifest:

<receiver android:name=".Widget.TestWidget">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
            <intent-filter>
                <action android:name="AUTO_UPDATE" />
            </intent-filter>
            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/test_widget_info" />
        </receiver>

        <service android:name=".Widget.CallbackNetworkWidgetService"></service>

CallbackWidgetNetworkService

@Override
public int onStartCommand(Intent i, int flags, int startId) {
    context = this.getApplicationContext();
    Log.i("WIDNET", "onStart network");

    isWifiConnected();

    return Service.START_NOT_STICKY;
}

public void isWifiConnected(){

    BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            getNetworkInfo();
          //  Log.i("WIDNET", "broadcastReceiver ipwan: "+ipwan+" type: "+type);
        }
    };

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);

    context.registerReceiver(broadcastReceiver, intentFilter);
}

Appwidget - startNetworkService

 public static void startNetworkService(Context context){
        int[] allWidgetIds = new int[0];

        Log.i("WIDNET", "start network service");
        ComponentName thisWidget = new ComponentName(context,
                TestWidget.class);
        if(widgetManager != null)
            allWidgetIds = widgetManager.getAppWidgetIds(thisWidget);

        if(intentNetwork != null) {
            CallbackNetworkWidgetService cnws = new CallbackNetworkWidgetService();
            cnws.stopSelf();
            Log.i("WIDNET", "stop network service");
        }

        intentNetwork = new Intent(context.getApplicationContext(),
                CallbackNetworkWidgetService.class);
        intentNetwork.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);

        context.startService(intentNetwork);
    }

回答1:

You need to use foreground service for this purpose.

ContextCompat.startForegroundService(context, intentNetwork);

Don't forget for notification for api 26+ :

@Override
public int onStartCommand(Intent i, int flags, int startId) {
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
      Notification.Builder builder = new Notification.Builder(this, ANDROID_CHANNEL_ID)
            .setContentTitle("wifi listener bla bla bla")
            .setContentText(text)
            .setAutoCancel(true);

      Notification notification = builder.build();
      startForeground(1, notification);
   }
   //...
}