简体   繁体   中英

Android - How can I check the sender class of the “stopService” Intent?

I stop a service from various places. How can I check when the sender of the command "stopService(Intent intent)" was my NetworkReceivar class (extends BroadcastReceiver)??

This is my code for do this more clear:

public class NetworkReceiver extends BroadcastReceiver{

public void onReceive(Context context, Intent intent) {
    boolean isNetworkDown = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
    if (isNetworkDown) {
        context.stopService(new Intent(context, MyService.class));
    }

My service class:

public void onDestroy() {
    Log.i(TAG, "OnDestroy");
    // if came from NetworkReceiver do something.
    this.updater.interrupt();
    this.updater = null;
    super.onDestroy();
}

How can I check when the sender of the command "stopService(Intent intent)" was my NetworkReceivar class (extends BroadcastReceiver)??

AFAIK, you can't.

However, instead of stopService() , you could call startService() , sending over a command that your service interprets as "time to shut down". Something on that Intent that is your command could be used to indicate the sender, so that you can distinguish one sender from another. And, the service can call stopSelf() to shut itself down.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM