我有一个监视wifi连接的小部件,所以我启动了一项服务来启动广播接收器以检测网络变化。一切正常,除非我退出主应用程序:服务停止。
因此,我在小部件中启动了一个警报管理器,该警报管理器大约每分钟醒来一次,并检查主应用程序是否已退出。如果是这种情况,我尝试重新启动wifi监控服务,这次它崩溃并显示以下消息:
不允许启动服务意图{ cmp = package .CallbackNetworkWidgetService(有其他功能)}:应用程序已在 后台uid UidRecord {f6e65b9 u0a154 RCVR空闲更改:空闲|未缓存 procs:1 seq(0,0,0)}
问题出在oreo新的意图/服务模型上。直到牛轧糖都可以使用。
问题:
我的方法正确吗?
我对前台/后台服务的概念有些迷惑。小部件是背景svce? wifi监控服务/ 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);
}
答案 0 :(得分:2)
您需要为此使用前台服务。
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);
}
//...
}