Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android監測本地服務

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;         

                                                                                     }      

                                                                    }       

                                                                           return false;   

                                        }

                        }

 

 

 

 

 

 

 

 

 

 

 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved