简体   繁体   English

Android检查服务是否正在从其他应用程序运行

[英]Android check if service is running from another application

I'm writing two applications that cooperate together one collecting data (using a service) and the other is a custom keyboard getting the data from the service and writing them to whatever text field is selected. 我正在编写两个相互配合的应用程序,一个收集数据(使用服务),另一个是自定义键盘,该键盘从服务中获取数据并将其写入所选的任何文本字段。 All this is working fine my only problem is the service binding; 所有这些都工作正常,我唯一的问题是服务绑定。 right now the service will be started from any app when they call bind. 现在,当他们调用bind时,该服务将从任何应用程序启动。 That is not the behavior i want to have; 那不是我想要的行为; The data collection app should be the only one starting / stopping the service via an activity, the keyboard will only bind if the service is running. 数据收集应用程序应该是唯一通过活动启动/停止服务的应用程序,只有在服务正在运行时,键盘才会绑定。 what i need to do that is check if the service is running before calling the bind. 我需要做的是在调用绑定之前检查服务是否正在运行。 is there anyway for me to check the status from the keyboard app ? 无论如何,我可以通过键盘应用程序检查状态吗? is there some kind of lock to signal the presence of the service ? 是否有某种锁来表示服务的存在? all i find right now is methods that require the class name and context from where the service was started and that doesn't work for me. 我现在发现的所有方法都需要从服务启动位置开始的类名和上下文,而这些方法对我不起作用。

basically ended with three possibilities hope this helps someone else in the future. 基本上以三种可能性结束,希望这对以后的人有所帮助。

Method 1: (what i went with event though method 2 is better) 方法1 :(尽管方法2更好,但我对事件进行了处理)

    public static boolean isServiceRunning(Context queryingContext) {
    try {
        ActivityManager manager = (ActivityManager) queryingContext.getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if ("com.example.myservice".equals(service.service.getClassName()))
                return true;
        }
    } catch (Exception ex) {
        Toast.makeText(queryingContext, "Error checking service status", Toast.LENGTH_SHORT).show();
    }
    return false;
}

Method 2: 方法2:

like pskink suggested above use bindService with flag 0. only issue with this is the method will always return true whether it got connected or not, you'll have to find another way to know what happened (you won't receive the service handle if it failed) 就像上面建议的bindService一样,将bindService与标志0一起使用。唯一的问题是,无论方法是否连接,该方法将始终返回true,您将不得不寻找另一种方式来了解发生了什么(如果发生错误,您将不会收到服务句柄它失败了)

Method 3: 方法3:

suggested by yosriz use SharedPreference to stop a status flag that the other application will check and know the status of the service. yosriz建议,使用SharedPreference停止其他应用程序将检查并知道服务状态的状态标志。

Service cannot be checked to see if it was started already, Android Services are designed to handle multiple client, and when bind method will create the service if not running already. 无法检查服务以查看其是否已经启动,Android服务旨在处理多个客户端,并且如果bind方法尚未运行,则bind方法将创建该服务。

So you can either start it explicitly or making Bound Service connection with bind() that will create new if not exist. 因此,您可以显式启动它,也可以使用bind()建立绑定服务连接,如果不存在,它将创建新的绑定。

Nevertheless, you can achieve this functionality in many ways, for instance save the service 'is running' state in some shared preference and check this pref before binding to the service. 但是,您可以通过多种方式实现此功能,例如,以某种共享的首选项保存服务“正在运行”状态,并在绑定到服务之前检查此首选项。 (SharedPreference can be shared between 2 process of the same user id) (SharedPreference可以在具有相同用户标识的2个进程之间共享)

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

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