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;          

                                                                                     }       

                                                                    }       

                                                                           return false;    

                                        }

                        }

 

 

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章