gpt4 book ai didi

android - 奥利奥 - 小部件服务和广播接收器 : Not allowed to start service Intent

转载 作者:行者123 更新时间:2023-11-29 18:50:58 25 4
gpt4 key购买 nike

我有一个监视 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)}

问题出在 oreo 新的 Intent/服务模型上。它一直有效到牛轧糖。

问题:

  1. 我的做法正确吗?

  2. 我对前台/后台服务的概念有点迷茫。小部件是后台服务吗? wifi监控服务/BR也是后台服务?

  3. 这里有什么问题?

list :

<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);
}
//...
}

关于android - 奥利奥 - 小部件服务和广播接收器 : Not allowed to start service Intent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50984211/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com