简体   繁体   English

Android-如何检查“ stopService”意图的发送者类?

[英]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)?? 如何检查命令“ stopService(Intent intent)”的发送者是我的NetworkReceivar类(扩展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)?? 如何检查命令“ stopService(Intent intent)”的发送者是我的NetworkReceivar类(扩展BroadcastReceiver)?

AFAIK, you can't. AFAIK,您不能。

However, instead of stopService() , you could call startService() , sending over a command that your service interprets as "time to shut down". 但是,您可以调用startService()而不是stopService() ,以发送一条您的服务将其解释为“关闭时间”的命令。 Something on that Intent that is your command could be used to indicate the sender, so that you can distinguish one sender from another. Intent上的命令(您的命令)可以用来指示发件人,以便您可以区分一个发件人和另一个发件人。 And, the service can call stopSelf() to shut itself down. 并且,该服务可以调用stopSelf()关闭自身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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