当前位置:操作系统 > 安卓/Android >>

android监测本地服务

一、

private boolean isMyServiceRunning(){   

                  ActivityManager manager= (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);  

              for(RunningServiceInfo service: manager.getRunningServices(Integer.MAX_VALUE))

                        {     

                              if(MyService.class.getName().equals(service.service.getClassName()))

                                       {        

                                              returntrue;      

                                       }  

                       }  

                            returnfalse;

          }

 

 

 

二、启动服务必须用startserver()

Intent bindIntent = new Intent(this,ServiceTask.class);  

startService(bindIntent);  

 bindService(bindIntent,mConnection,0);

 

 

                         public class ServiceTools {   

                                   private static String LOG_TAG = ServiceTools.class.getName();   

                                   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;         

                                                                                     }      

                                                                    }       

           &nb

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,