奥利奥 - 小部件服务和广播接收器:不允许启动服务意图
Oreo - Widget services and broadcast receiver: Not allowed to start service Intent
我有一个监视 wifi 连接的小部件,所以我启动了一个服务来启动广播接收器来检测网络变化。一切正常,除了当我退出主应用程序时:服务停止。
所以我在小部件中启动了一个警报管理器,它大约每分钟唤醒一次并检查主应用程序是否已退出。如果是这种情况,我尝试重新启动我的 wifi 监控服务,这次它崩溃并显示消息:
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)}
问题出在奥利奥新 intent/service 型号上。它一直有效到牛轧糖。
问题:
我的做法正确吗?
我对 foreground/background 服务的概念有点迷惑。小部件是后台服务吗? wifi监控service/BR也是后台服务?
这里有什么问题?
清单:
<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);
}
您需要为此使用前台服务。
ContextCompat.startForegroundService(context, intentNetwork);
不要忘记 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);
}
//...
}
我有一个监视 wifi 连接的小部件,所以我启动了一个服务来启动广播接收器来检测网络变化。一切正常,除了当我退出主应用程序时:服务停止。
所以我在小部件中启动了一个警报管理器,它大约每分钟唤醒一次并检查主应用程序是否已退出。如果是这种情况,我尝试重新启动我的 wifi 监控服务,这次它崩溃并显示消息:
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)}
问题出在奥利奥新 intent/service 型号上。它一直有效到牛轧糖。
问题:
我的做法正确吗?
我对 foreground/background 服务的概念有点迷惑。小部件是后台服务吗? wifi监控service/BR也是后台服务?
这里有什么问题?
清单:
<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);
}
您需要为此使用前台服务。
ContextCompat.startForegroundService(context, intentNetwork);
不要忘记 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);
}
//...
}