关闭

Android中ActivityManager的使用案例

5271人阅读 评论(1) 收藏 举报
分类:

通过ActivityManager我们可以获得系统里正在运行的activities,包括进程(Process)等、应用程序/包、服务(Service)、任务(Task)信息。

1、判断某个Service是否正在运行

private boolean isFlightServiceRunning(){
  ActivityManager manager=(ActivityManager)getSystemService(ACTIVITY_SERVICE);
  for (  RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
    if ("com.barbermot.pilot.ui.FlightService".equals(service.service.getClassName())) {
      return true;
    }
  }
  return false;
}

2、获取某个正在运行的Service信息

private ActivityManager.RunningServiceInfo liveServiceInfo(){
  ActivityManager manager=(ActivityManager)context().getSystemService(Context.ACTIVITY_SERVICE);
  List<ActivityManager.RunningServiceInfo> infos=manager.getRunningServices(100);
  for (  ActivityManager.RunningServiceInfo info : infos) {
    if (info.service.getClassName().equals(LiveShowService.class.getName())) {
      return info;
    }
  }
  return null;
}

3、判断是否有应用访问外部存储

private boolean hasAppsAccessingStorage() throws RemoteException {
  ActivityManager am=(ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
  List<ApplicationInfo> list=am.getRunningExternalApplications();
  if (list != null && list.size() > 0) {
    return true;
  }
  return false;
}

4、杀掉某个进程

private void killProcesses(){
  if (mPackages == null)   return;
  ActivityManager am=(ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
  for (int i=0; i < mPackages.length; i++) {
    am.forceStopPackage(mPackages[i]);
  }
}

5、判断是否低内存

private static boolean isLowMemory(Context context){
  if (null == context) {
    return false;
  }
  ActivityManager am=(ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
  ActivityManager.MemoryInfo outInfo=new ActivityManager.MemoryInfo();
  am.getMemoryInfo(outInfo);
  return outInfo.lowMemory;
}

6、清理用户数据

private void initiateClearUserData(){
  mClearDataButton.setEnabled(false);
  String packageName=mAppEntry.info.packageName;
  Log.i(TAG,"Clearing user data for package : " + packageName);
  if (mClearDataObserver == null) {
    mClearDataObserver=new ClearUserDataObserver();
  }
  ActivityManager am=(ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
  boolean res=am.clearApplicationUserData(packageName,mClearDataObserver);
  if (!res) {
    Log.i(TAG,"Couldnt clear application user data for package:" + packageName);
    showDialogInner(DLG_CANNOT_CLEAR_DATA);
  }
 else {
    mClearDataButton.setText(R.string.recompute_size);
  }
}

7、获取内存信息

private void update_labels(){
  MemoryInfo mem_info;
  double mem_size;
  mem_info=new ActivityManager.MemoryInfo();
  activity_man.getMemoryInfo(mem_info);
  mem_size=(mem_info.availMem / (CONVERT * CONVERT));
  availMem_label.setText(String.format("Available memory:\t %.2f Mb",mem_size));
  numProc_label.setText("Number of processes:\t " + display_process.size());
}

8、指定pid是否正在运行

private boolean isPidRunning(int pid){
  if (mActivityManager == null)   mActivityManager=(ActivityManager)mContext.getSystemService(Context.ACTIVITY_SERVICE);
  List<RunningAppProcessInfo> l=mActivityManager.getRunningAppProcesses();
  Iterator<RunningAppProcessInfo> i=l.iterator();
  RunningAppProcessInfo info;
  while (i.hasNext()) {
    info=i.next();
    if (info.pid == pid)     return true;
  }
  return false;
}

9、获取指定App的顶部Activity

private ComponentName getActivityForApp(RunningAppProcessInfo target){
  ComponentName result=null;
  ActivityManager.RunningTaskInfo info;
  if (target == null)   return null;
  if (mActivityManager == null)   mActivityManager=(ActivityManager)mContext.getSystemService(Context.ACTIVITY_SERVICE);
  List<ActivityManager.RunningTaskInfo> l=mActivityManager.getRunningTasks(9999);
  Iterator<ActivityManager.RunningTaskInfo> i=l.iterator();
  while (i.hasNext()) {
    info=i.next();
    if (target.processName.contains(info.baseActivity.getPackageName())) {
      if (DEBUG)       Slog.i(TAG,"getActivityForApp(" + target.processName + ") found the following activity (topActivity /// baseActivity): "+ info.topActivity.toString()+ " /// "+ info.baseActivity.toString());
      result=info.topActivity;
      break;
    }
  }
  return result;
}

原文:
Java Code Examples for android.app.ActivityManager

可参考文章:
1、Android中获取系统内存信息以及进程信息

2、Android中获取正在运行的应用程序

3、Android中获取正在运行的服务

1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:449082次
    • 积分:7581
    • 等级:
    • 排名:第3236名
    • 原创:278篇
    • 转载:45篇
    • 译文:22篇
    • 评论:63条
    成长轨迹
    目前研究方向:Android,React Native,机器学习

      微信公众号:DroidMind
      (欢迎关注)