Android中獲得正在啟動並執行程式和系統服務的方法_Android

來源:互聯網
上載者:User

ActivityManager.RunningAppProcessInfo類與擷取正在啟動並執行應用程式
每一個應用程式都會運行在它獨立的進程裡,但是為了節省資源或者這些應用程式是為了完成某一共同工作,它們
也可能會運行在一個進程裡。

 知識點介紹:
ActivityManager.RunningAppProcessInfo類
說明: 封裝了正在啟動並執行進程資訊
常用欄位:
int   pid    進程ID
int   uid    進程所在的使用者ID
String   processName 進程名,預設是包名或者由android:process=””屬性指定
String [ ]   pkgList      運行在該進程下的所有應用程式套件組合名

Demo說明:
我們利用ActivityManager擷取所有正在啟動並執行進程資訊後,也就是擷取了每個進程裡正在啟動並執行應用程式套件組合名(pkgname),那麼通過這些包名(pkgname),直接調用PackageManager類提供的方法,可以擷取這些應用程式的資訊了。
一些資源檔就不貼了,直接貼出了主工程邏輯。需要注意的在這兒我們一次性擷取了所有應用程式資訊,然後對這些應用程式進行過濾,得到我們需要的對象。 讀者可以使用PackageManager類提供的方法,進行迴圈遍曆所有包名(pkgname),但是這樣效率會比較低。
截圖如下:
點擊某一進程後

查看某一進程啟動並執行應用程式資訊、所有正在啟動並執行進程資訊:

顯示正在運行應用程式的工程代碼如下:

 package com.qin.ammp;  import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map;  import android.app.Activity; import android.app.ActivityManager; import android.content.Context; import android.content.Intent; import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.TextView; import android.widget.AdapterView.OnItemClickListener;  public class BrowseRunningAppActivity extends Activity {    private static String TAG = "BrowseRunningAppActivity";    private ListView listview = null;    private List<RunningAppInfo> mlistAppInfo = null;   private TextView tvInfo = null ;      private PackageManager pm;    @Override   public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.browse_app_list);      listview = (ListView) findViewById(R.id.listviewApp);     tvInfo = (TextView)findViewById(R.id.tvInfo) ;          mlistAppInfo = new ArrayList<RunningAppInfo>();      // 查詢某一特定進程的所有應用程式     Intent intent = getIntent();     //是否查詢某一特定pid的應用程式     int pid = intent.getIntExtra("EXTRA_PROCESS_ID", -1);          if ( pid != -1) {       //某一特定經常裡所有正在啟動並執行應用程式       mlistAppInfo =querySpecailPIDRunningAppInfo(intent, pid);     }     else{       // 查詢所有正在啟動並執行應用程式資訊: 包括他們所在的進程id和進程名       tvInfo.setText("所有正在啟動並執行應用程式有-------");       mlistAppInfo = queryAllRunningAppInfo();      }     BrowseRunningAppAdapter browseAppAdapter = new BrowseRunningAppAdapter(this, mlistAppInfo);     listview.setAdapter(browseAppAdapter);   }    // 查詢所有正在啟動並執行應用程式資訊: 包括他們所在的進程id和進程名   // 這兒我直接擷取了系統裡安裝的所有應用程式,然後根據報名pkgname過濾擷取所有真正啟動並執行應用程式   private List<RunningAppInfo> queryAllRunningAppInfo() {     pm = this.getPackageManager();     // 查詢所有已經安裝的應用程式     List<ApplicationInfo> listAppcations = pm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);     Collections.sort(listAppcations,new ApplicationInfo.DisplayNameComparator(pm));// 排序      // 儲存所有正在啟動並執行包名 以及它所在的進程資訊     Map<String, ActivityManager.RunningAppProcessInfo> pgkProcessAppMap = new HashMap<String, ActivityManager.RunningAppProcessInfo>();      ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);     // 通過調用ActivityManager的getRunningAppProcesses()方法獲得系統裡所有正在啟動並執行進程     List<ActivityManager.RunningAppProcessInfo> appProcessList = mActivityManager         .getRunningAppProcesses();      for (ActivityManager.RunningAppProcessInfo appProcess : appProcessList) {       int pid = appProcess.pid; // pid       String processName = appProcess.processName; // 進程名       Log.i(TAG, "processName: " + processName + " pid: " + pid);        String[] pkgNameList = appProcess.pkgList; // 獲得運行在該進程裡的所有應用程式套件組合        // 輸出所有應用程式的包名       for (int i = 0; i < pkgNameList.length; i++) {         String pkgName = pkgNameList[i];         Log.i(TAG, "packageName " + pkgName + " at index " + i+ " in process " + pid);         // 加入至map對象裡         pgkProcessAppMap.put(pkgName, appProcess);       }     }     // 儲存所有正在啟動並執行應用程式資訊     List<RunningAppInfo> runningAppInfos = new ArrayList<RunningAppInfo>(); // 儲存過濾查到的AppInfo      for (ApplicationInfo app : listAppcations) {       // 如果該包名存在 則構造一個RunningAppInfo對象       if (pgkProcessAppMap.containsKey(app.packageName)) {         // 獲得該packageName的 pid 和 processName         int pid = pgkProcessAppMap.get(app.packageName).pid;         String processName = pgkProcessAppMap.get(app.packageName).processName;         runningAppInfos.add(getAppInfo(app, pid, processName));       }     }      return runningAppInfos;    }   // 某一特定經常裡所有正在啟動並執行應用程式   private List<RunningAppInfo> querySpecailPIDRunningAppInfo(Intent intent , int pid) {       String[] pkgNameList = intent.getStringArrayExtra("EXTRA_PKGNAMELIST");     String processName = intent.getStringExtra("EXTRA_PROCESS_NAME");          //update ui     tvInfo.setText("進程id為"+pid +" 啟動並執行應用程式共有 : "+pkgNameList.length);              pm = this.getPackageManager();        // 儲存所有正在啟動並執行應用程式資訊     List<RunningAppInfo> runningAppInfos = new ArrayList<RunningAppInfo>(); // 儲存過濾查到的AppInfo      for(int i = 0 ; i<pkgNameList.length ;i++){       //根據包名查詢特定的ApplicationInfo對象       ApplicationInfo appInfo;      try {       appInfo = pm.getApplicationInfo(pkgNameList[i], 0);       runningAppInfos.add(getAppInfo(appInfo, pid, processName));      }      catch (NameNotFoundException e) {       // TODO Auto-generated catch block       e.printStackTrace();      } // 0代表沒有任何標記;     }     return runningAppInfos ;   }         // 構造一個RunningAppInfo對象 ,並賦值   private RunningAppInfo getAppInfo(ApplicationInfo app, int pid, String processName) {     RunningAppInfo appInfo = new RunningAppInfo();     appInfo.setAppLabel((String) app.loadLabel(pm));     appInfo.setAppIcon(app.loadIcon(pm));     appInfo.setPkgName(app.packageName);      appInfo.setPid(pid);     appInfo.setProcessName(processName);      return appInfo;   } } 

ActivityManager.RunningServiceInfo類擷取正在啟動並執行服務
ActivityManager.RunningServiceInfo類:  封裝了正在啟動並執行服務資訊
 
擷取系統裡所有真正啟動並執行服務是通過調用ActivityManager方法來得到的,具體方法如下:
 
List<ActivityManager.RunningServiceInfo> getRunningServices (int maxNum)
功能:返回所有正在啟動並執行服務
參數:   maxNum 代表我們希望返回的服務數目大小,一般給個稍大的值即可, 例如,50 。
                              
ActivityManager.RunningServiceInfo 類
  常用欄位:
 
long   activeSince        服務第一次被啟用的時間, 包括啟動和綁定方式
int      clientCount          如果該Service是通過Bind方法方式串連,則clientCount代表了service串連用戶端的數目
int      crashCount          服務運行期間,出現死機的次數
boolean   foreground   若為true,則該服務在後台執行
int        pid                          如果不為0,表示該service所在的進程ID號( PS:為0的話我也不清楚 - - 求指點)
int        uid                          使用者ID 類似於Linux的使用者權限,例如root等                   
String   process                 進程名,預設是包名或者由屬性android:process指定
ComponentName  service          獲得該Service的組件資訊 包含了pkgname / servicename資訊
 
PackageManger類
說明: 封裝了對應用程式資訊的操作
獲得應用程式資訊的的方法如下:
public abstractApplicationInfo  getApplicationInfo(String  packageName, int flags)
參數:packagename 包名
flags 該ApplicationInfo是此flags標記,通常可以直接賦予常數0即可
功能:返回ApplicationInfo對象
Demo說明:
我們擷取了系統裡正在啟動並執行服務資訊,包括包名,表徵圖,service類名等。為了達到Settings下應用程式模組中的正在運行服務的效果,我們點擊某一服務後,理論上來說是可以停止該服務的,但是由於許可權permissions不夠,可能報SecurityException異常,導致應用程式發生異常。
 
關於許可權不夠的問題,可以分為兩種:
1、 在AndroidManifest.xml檔案中,為<activity/>或<service/>節點指定android:permission屬性時,在其他進程中操作時,需要聲明該permission許可權 。
2、 系統許可權,這個咱就沒什麼話說了。
截圖如下:

主工程邏輯如下:

package com.qin.runservice;  import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List;  import android.app.Activity; import android.app.ActivityManager; import android.app.AlertDialog; import android.app.Dialog; import android.content.ComponentName; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.os.Bundle; import android.os.Debug; import android.util.Log; import android.view.ContextMenu; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ContextMenu.ContextMenuInfo; import android.widget.AdapterView; import android.widget.ListView; import android.widget.TextView; import android.widget.AdapterView.OnItemClickListener;  public class BrowseRunningServiceActivity extends Activity implements     OnItemClickListener {    private static String TAG = "RunServiceInfo";    private ActivityManager mActivityManager = null;   // ProcessInfo Model類 用來儲存所有進程資訊   private List<RunSericeModel> serviceInfoList = null;    private ListView listviewService;   private TextView tvTotalServiceNo;    public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);      setContentView(R.layout.browse_service_list);      listviewService = (ListView) findViewById(R.id.listviewService);     listviewService.setOnItemClickListener(this);      tvTotalServiceNo = (TextView) findViewById(R.id.tvTotalServiceNo);      // 獲得ActivityManager服務的對象     mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);      // 獲得正在啟動並執行Service資訊     getRunningServiceInfo();     // 對集合排序     Collections.sort(serviceInfoList, new comparatorServiceLable());      System.out.println(serviceInfoList.size() + "-------------");      // 為ListView構建配接器物件     BrowseRunningServiceAdapter mServiceInfoAdapter = new           BrowseRunningServiceAdapter(BrowseRunningServiceActivity.this, serviceInfoList);      listviewService.setAdapter(mServiceInfoAdapter);      tvTotalServiceNo.setText("當前正在啟動並執行服務共有:" + serviceInfoList.size());   }   // 獲得系統正在啟動並執行進程資訊   private void getRunningServiceInfo() {      // 設定一個預設Service的數量大小     int defaultNum = 20;     // 通過調用ActivityManager的getRunningAppServicees()方法獲得系統裡所有正在啟動並執行進程     List<ActivityManager.RunningServiceInfo> runServiceList = mActivityManager         .getRunningServices(defaultNum);      System.out.println(runServiceList.size());      // ServiceInfo Model類 用來儲存所有進程資訊     serviceInfoList = new ArrayList<RunSericeModel>();      for (ActivityManager.RunningServiceInfo runServiceInfo : runServiceList) {        // 獲得Service所在的進程的資訊       int pid = runServiceInfo.pid; // service所在的進程ID號       int uid = runServiceInfo.uid; // 使用者ID 類似於Linux的許可權不同,ID也就不同 比如 root等       // 進程名,預設是包名或者由屬性android:process指定       String processName = runServiceInfo.process;         // 該Service啟動時的時間值       long activeSince = runServiceInfo.activeSince;        // 如果該Service是通過Bind方法方式串連,則clientCount代表了service串連用戶端的數目       int clientCount = runServiceInfo.clientCount;        // 獲得該Service的組件資訊 可能是pkgname/servicename       ComponentName serviceCMP = runServiceInfo.service;       String serviceName = serviceCMP.getShortClassName(); // service 的類名       String pkgName = serviceCMP.getPackageName(); // 包名        // 列印Log       Log.i(TAG, "所在進程id :" + pid + " 所在進程名:" + processName + " 所在進程uid:"           + uid + "\n" + " service啟動的時間值:" + activeSince           + " 用戶端綁定數目:" + clientCount + "\n" + "該service的組件資訊:"           + serviceName + " and " + pkgName);        // 這兒我們通過service的組件資訊,利用PackageManager擷取該service所在應用程式的包名 ,表徵圖等       PackageManager mPackageManager = this.getPackageManager(); // 擷取PackagerManager對象;        try {         // 擷取該pkgName的資訊         ApplicationInfo appInfo = mPackageManager.getApplicationInfo(             pkgName, 0);          RunSericeModel runService = new RunSericeModel();         runService.setAppIcon(appInfo.loadIcon(mPackageManager));         runService.setAppLabel(appInfo.loadLabel(mPackageManager) + "");         runService.setServiceName(serviceName);         runService.setPkgName(pkgName);         // 設定該service的組件資訊         Intent intent = new Intent();         intent.setComponent(serviceCMP);         runService.setIntent(intent);          runService.setPid(pid);         runService.setProcessName(processName);          // 添加至集合中         serviceInfoList.add(runService);        } catch (NameNotFoundException e) {         // TODO Auto-generated catch block         System.out.println("--------------------- error -------------");         e.printStackTrace();       }      }   }    // 觸摸可停止   @Override   public void onItemClick(AdapterView<?> arg0, View arg1, int position,       long arg3) {     // TODO Auto-generated method stub     final Intent stopserviceIntent = serviceInfoList.get(position)         .getIntent();      new AlertDialog.Builder(BrowseRunningServiceActivity.this).setTitle(         "是否停止服務").setMessage(         "服務只有在重新啟動後,才可以繼續運行。但這可能會給電子市場應用程式帶來意想不到的結果。")         .setPositiveButton("停止", new DialogInterface.OnClickListener() {            @Override           public void onClick(DialogInterface dialog, int which) {             // TODO Auto-generated method stub             // 停止該Service             //由於許可權不夠的問題,為了避免應用程式出現異常,捕獲該SecurityException ,並彈出對話方塊             try {               stopService(stopserviceIntent);             } catch (SecurityException sEx) {               //發生異常 說明許可權不夠                System.out.println(" deny the permission");               new AlertDialog.Builder(BrowseRunningServiceActivity.this).setTitle(               "許可權不夠").setMessage("對不起,您的許可權不夠,無法停止該Service").create().show();             }             // 重新整理介面             // 獲得正在啟動並執行Service資訊             getRunningServiceInfo();             // 對集合排序             Collections.sort(serviceInfoList, new comparatorServiceLable());             // 為ListView構建配接器物件             BrowseRunningServiceAdapter mServiceInfoAdapter = new BrowseRunningServiceAdapter(                 BrowseRunningServiceActivity.this,                 serviceInfoList);             listviewService.setAdapter(mServiceInfoAdapter);             tvTotalServiceNo.setText("當前正在啟動並執行服務共有:"                 + serviceInfoList.size());           }          }).setNegativeButton("取消",             new DialogInterface.OnClickListener() {                @Override               public void onClick(DialogInterface dialog,                   int which) {                 // TODO Auto-generated method stub                 dialog.dismiss(); // 取消對話方塊               }             }).create().show();   }    // 自訂排序 根據AppLabel排序   private class comparatorServiceLable implements Comparator<RunSericeModel> {      @Override     public int compare(RunSericeModel object1, RunSericeModel object2) {       // TODO Auto-generated method stub       return object1.getAppLabel().compareTo(object2.getAppLabel());     }    }  } 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.