Service Intent must be explicit: Intent

后端 未结 5 792
你的背包
你的背包 2020-12-01 00:27

I have an app some time now in which I call a service through a broadcast receiver (MyStartupIntentReceiver). The code in the broadcast receiver in order to call the service

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-01 01:15

    Try this. it works for me. Here MonitoringService is my service class. I have two action, which indicate service to stop or start. I send that value from my broadcast receiver depend on AIRPLANE_MODE_CHANGED.

    @Override
    public void onReceive(Context context, Intent intent) {   
        String action = intent.getAction();
    
        if(Intent.ACTION_AIRPLANE_MODE_CHANGED.equalsIgnoreCase(action)){
             boolean isOn = intent.getBooleanExtra("state", false);
             String serviceAction = isOn? MonitoringService.StopAction : MonitoringService.StartAction;
             Intent serviceIntent = new Intent(context, MonitoringService.class);
             serviceIntent.setAction(serviceAction);
             context.startService(serviceIntent);
        }
    }
    

    NOTE: I add following code to trigger my broadcast receiver named: ManageLocationListenerReceiver.

    
         
             
         
    
    

提交回复
热议问题