赞
踩
通过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; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。