public static boolean isNewTopActivity(String name,Context context) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
UsageStatsManager usm = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
long time = System.currentTimeMillis();
List<UsageStats> appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000 * 1000, time);
if (appList != null && appList.size() > 0) {
SortedMap<Long, UsageStats> mySortedMap = new TreeMap<Long, UsageStats>();
for (UsageStats usageStats : appList) {
mySortedMap.put(usageStats.getLastTimeUsed(), usageStats);
}
if (mySortedMap != null && !mySortedMap.isEmpty()) {
String currentApp = mySortedMap.get(mySortedMap.lastKey()).getPackageName();
//Log.d("TouchOnlineReceiver","currentApp = " + currentApp);
return currentApp.contains(name);
}
}
return false;
} else {
//android 5.0 以上 获取自生顶层应用包名
ActivityManager am = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfos = am.getRunningTasks(1);
ComponentName cn = null;
if(taskInfos.size()>0){
cn = taskInfos.get(0).topActivity;
}else {
return false;
}
Log.e("=====","classname = " + cn.getClassName());
return cn.getClassName().contains(name);
}
}
/** 判断服务是否在运行
?with Android O(8.0) this is now deprecated and will only return information about the calling apps services
* @param context
* @param serviceName
* @return
*/
public boolean isRunService(Context context,String serviceName) {
ActivityManager manager = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
//Log.e(TAG,service.service.getClassName()+" service name");
if (service.service.getClassName().contains(serviceName)) {
return true;
}
}
return false;
}
|