|
@@ -1,7 +1,10 @@
|
|
package com.wdkl.launch.runningApp;
|
|
package com.wdkl.launch.runningApp;
|
|
|
|
|
|
import android.app.ActivityManager;
|
|
import android.app.ActivityManager;
|
|
|
|
+import android.app.usage.UsageEvents;
|
|
|
|
+import android.app.usage.UsageStatsManager;
|
|
import android.content.Context;
|
|
import android.content.Context;
|
|
|
|
+import android.os.Build;
|
|
import android.util.Log;
|
|
import android.util.Log;
|
|
|
|
|
|
import com.wdkl.launch.ShareData;
|
|
import com.wdkl.launch.ShareData;
|
|
@@ -30,23 +33,61 @@ public class RunningUtil {
|
|
|
|
|
|
public static boolean isWdklAppRunning(Context context) {
|
|
public static boolean isWdklAppRunning(Context context) {
|
|
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
|
|
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
|
|
- List<ActivityManager.RunningTaskInfo> list;
|
|
|
|
if (am == null) {
|
|
if (am == null) {
|
|
return false;
|
|
return false;
|
|
- } else {
|
|
|
|
- list = am.getRunningTasks(100);
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- if (list == null || list.size() <= 0) {
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
- for (ActivityManager.RunningTaskInfo info : list) {
|
|
|
|
- Log.d("wzl", "running app name: " + info.baseActivity.getPackageName());
|
|
|
|
- if (info.baseActivity.getPackageName().contains("com.wdkl") && !info.baseActivity.getPackageName().equals("com.wdkl.launch")) {
|
|
|
|
- Log.d("wzl", "isWdklAppRunning return true");
|
|
|
|
- return true;
|
|
|
|
|
|
+ if (Build.VERSION.SDK_INT < 21) { //android系统小于5.0
|
|
|
|
+ List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(20);
|
|
|
|
+ if (list == null || list.size() <= 0) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ for (ActivityManager.RunningTaskInfo info : list) {
|
|
|
|
+ //Log.d("wzl", "1111111running app name: " + info.baseActivity.getPackageName());
|
|
|
|
+ if (info.baseActivity.getPackageName().contains("com.wdkl") && !info.baseActivity.getPackageName().equals("com.wdkl.launch")) {
|
|
|
|
+ //Log.d("wzl", "1111111isWdklAppRunning return true");
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } else if (Build.VERSION.SDK_INT == 21) { //android系统等于5.0
|
|
|
|
+ List<ActivityManager.RunningAppProcessInfo> processes = am.getRunningAppProcesses();
|
|
|
|
+ if (processes == null || processes.size() == 0) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ for (ActivityManager.RunningAppProcessInfo process : processes) {
|
|
|
|
+ //Log.d("wzl", "2222222running app name: " + process.processName);
|
|
|
|
+ if (process.processName.contains("com.wdkl") && !process.processName.equals("com.wdkl.launch")) {
|
|
|
|
+ //Log.d("wzl", "2222222isWdklAppRunning return true");
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } else { //android系统大于5.0
|
|
|
|
+ final long end = System.currentTimeMillis();
|
|
|
|
+ final UsageStatsManager usageStatsManager = (UsageStatsManager) context.getSystemService("usagestats");
|
|
|
|
+ if (null == usageStatsManager) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ final UsageEvents events = usageStatsManager.queryEvents((end - 60 * 1000), end);
|
|
|
|
+ if (null == events) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ UsageEvents.Event usageEvent = new UsageEvents.Event();
|
|
|
|
+ UsageEvents.Event lastMoveToFGEvent = null;
|
|
|
|
+ while (events.hasNextEvent()) {
|
|
|
|
+ events.getNextEvent(usageEvent);
|
|
|
|
+ //Log.d("wzl", "3333333running app name: " + usageEvent.getPackageName());
|
|
|
|
+ if (usageEvent.getEventType() == UsageEvents.Event.MOVE_TO_FOREGROUND) {
|
|
|
|
+ lastMoveToFGEvent = usageEvent;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (lastMoveToFGEvent != null) {
|
|
|
|
+ if (lastMoveToFGEvent.getPackageName().contains("com.wdkl") && !lastMoveToFGEvent.getPackageName().equals("com.wdkl.launch")) {
|
|
|
|
+ //Log.d("wzl", "3333333isWdklAppRunning return true");
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -60,7 +101,7 @@ public class RunningUtil {
|
|
public static boolean isAppRunning(Context context, String packageName) {
|
|
public static boolean isAppRunning(Context context, String packageName) {
|
|
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
|
|
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
|
|
List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(100);
|
|
List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(100);
|
|
- if (list.size() <= 0) {
|
|
|
|
|
|
+ if (list == null || list.size() <= 0) {
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
for (ActivityManager.RunningTaskInfo info : list) {
|
|
for (ActivityManager.RunningTaskInfo info : list) {
|