当前位置:   article > 正文

Android中ActivityManager的使用案例_使用系统服务重启应用:可以使用系统服务activitymanager来获取当前运行的activity

使用系统服务重启应用:可以使用系统服务activitymanager来获取当前运行的activity

通过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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

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]);
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

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);
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

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());
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/472004
推荐阅读
  

闽ICP备14008679号