1.根据包名启动APP
Intent intent = getActivity().getPackageManager().getLaunchIntentForPackage(“app包名”);
startActivity(intent);
2. 判断应用是否在运行
public boolean isRun(Context context, String MY_PKG_NAME) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> processInfos = am.getRunningServices(200);
boolean isAppRunning = false;
int myPid = android.os.Process.myPid();
for (ActivityManager.RunningServiceInfo info : processInfos) {
Log.i(“555—–”, info.service.getClassName() + ” info.pid=” + info.service.getPackageName());
}
return false;
}
3. 6.0版本获取相应的进程信息
public static void getProcess6Info(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
UsageStatsManager m = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
if (m != null) {
long now = System.currentTimeMillis();
//获取60秒之内的应用数据
List stats = m.queryUsageStats(UsageStatsManager.INTERVAL_BEST, now – 60 * 1000, now);
ActivityManager systemService = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
Log.i(“555—-”, “Running app number in last 60 seconds : ” + stats.size());
//取得最近运行的一个app,即当前运行的app
if ((stats != null) && (!stats.isEmpty())) {
for (int i = 0; i < stats.size(); i++) {
String processName = stats.get(i).getPackageName();
Log.i(“555—-”, “top running app is : ” + processName);
PackageManager PM = context.getPackageManager();
}
}
}
}
return;
}
4. 当本应用位于后台时,则将它切换到最前端
public static void setTopApp(Context context) {
//获取ActivityManager
ActivityManager activityManager = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
//获得当前运行的task(任务)
List<ActivityManager.RunningTaskInfo> taskInfoList = activityManager.getRunningTasks(100);
for (ActivityManager.RunningTaskInfo taskInfo : taskInfoList) {
//找到本应用的 task,并将它切换到前台
if (taskInfo.topActivity.getPackageName().equals(context.getPackageName())) {
activityManager.moveTaskToFront(taskInfo.id, 0);
break;
}
}
}