首页 文章

Oreo - Widget服务和广播接收器:不允许启动服务Intent

提问于
浏览
1

我有一个监视wifi连接的小部件,所以我启动一个服务来启动广播接收器来检测网络变化 . 一切正常,除非我退出主应用程序:服务停止 .

因此,我在窗口小部件中启动一个警报管理器,它几乎每分钟都会唤醒并检查主应用程序是否已退出 . 如果是这种情况我试图重新启动我的wifi监控服务,这次它崩溃了消息:

不允许启动服务Intent {cmp = package.CallbackNetworkWidgetService(has extras)}:app在后台uid UidRecord {f6e65b9 u0a154 RCVR空闲更改:idle | uncached procs:1 seq(0,0,0)}

问题在于新的意图/服务模式 . 它一直有效,直到牛轧糖 .

问题:

  • 我的方法是否正确?

  • 我对前台/后台服务的概念有点失落 . 小部件是一个背景svce? wifi监控服务/ BR也是后台svce?

  • 这里有什么问题?

表现:

<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 回答

  • 1

    您需要为此目的使用前台服务 .

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

相关问题