如果已经在运行android,如何防止服务再次运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10739413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:48:42  来源:igfitidea点击:

how to prevent service to run again if already running android

androidserviceandroid-service

提问by Atul Bhardwaj

On click of a button I want to start service using method startService(new Intent(currentActivity.this,MyService.class))but if service is running I don't want to call this method to avoid run service that is already running.How this is possible.I am using both Intent serviceand Servicein same project and want to apply same condition for both.

在点击按钮我想用方法来启动服务startService(new Intent(currentActivity.this,MyService.class)),但如果服务正在运行,我不想调用此方法来避免运行服务,已经running.How这possible.I正在同时使用意向的服务服务在同一项目并希望对两者应用相同的条件。

回答by Ion Aalbers

A service will only run once, so you can call startService(Intent)multiple times.

一个服务只会运行一次,所以你可以调用startService(Intent)多次。

You will receive an onStartCommand()in the service. So keep that in mind.

您将onStartCommand()在服务中收到一个。所以记住这一点。

Source: Note that multiple calls to Context.startService()do not nest (though they do result in multiple corresponding calls to onStartCommand()), so no matter how many times it is started a service will be stopped once Context.stopService()or stopSelf()is called; however, services can use their stopSelf(int)method to ensure the service is not stopped until started intents have been processed.

来源:注意多次调用Context.startService()do 不会嵌套(尽管它们确实会导致多次调用onStartCommand()),因此无论启动多少次,服务都会停止一次Context.stopService()或被stopSelf()调用;但是,服务可以使用它们的stopSelf(int)方法来确保在处理启动的意图之前服务不会停止。

At: http://developer.android.com/reference/android/app/Service.htmlon topic: Service Lifecycle

在:http: //developer.android.com/reference/android/app/Service.html主题:服务生命周期

回答by Thijs Limmen

Use startService(). Start Service will call onStartCommand()If the Service isn't started yet it will Call onCreate(). Initialize your variables and/or start a Thread in onCreate().

使用startService(). 启动服务将调用onStartCommand()如果服务尚未启动,它将调用onCreate()。初始化您的变量和/或在onCreate().

回答by Samuel

Bind your service; when starting call:

绑定你的服务;开始通话时:

Intent bindIntent = new Intent(this,ServiceTask.class);
    startService(bindIntent);
    bindService(bindIntent,mConnection,0);

Then to check if your service is working, use a method like:

然后要检查您的服务是否正常工作,请使用以下方法:

    public static boolean isServiceRunning(String serviceClassName){ 
             final ActivityManager activityManager = (ActivityManager)Application.getContext().getSystemService(Context.ACTIVITY_SERVICE);     
             final List<RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE);      
           for (RunningServiceInfo runningServiceInfo : services) {         
             if (runningServiceInfo.service.getClassName().equals(serviceClassName)){             
                return true;         
             }     
           }     
         return false; 
        }

回答by Krrish

Whenever we start any service from any activity , Android system calls the service's onStartCommand() method And If the service is not already running, the system first calls onCreate(), and then it calls onStartCommand().

每当我们从任何活动启动任何服务时,Android 系统都会调用该服务的 onStartCommand() 方法,如果该服务尚未运行,则系统首先调用 onCreate(),然后调用 onStartCommand()。

So mean to say is that android service start's only once in its lifecycle and keep it running till stopped.if any other client want to start it again then only onStartCommand() method will invoked all the time.

所以意思是说android服务在其生命周期中只启动一次并保持运行直到停止。如果任何其他客户端想要再次启动它那么只有onStartCommand()方法将一直被调用。