@SuppressLint({"NewApi"}) public static boolean isAppOnForeground(Context mContext) { ActivityManager activityManager = (ActivityManager) mContext.getSystemService("activity"); String packageName = mContext.getPackageName(); LogInfo.log("PushReceiver", "packageName =" + packageName); List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses(); if (appProcesses == null) { LogInfo.log("PushReceiver", "------appProcesses == null-----"); return false; } for (RunningAppProcessInfo appProcess : appProcesses) { LogInfo.log("PushReceiver", "------appProcess.processName =" + appProcess.processName); if (appProcess.processName.equals(packageName) && appProcess.importance == 100) { for (RunningTaskInfo rti : activityManager.getRunningTasks(100)) { if (!(rti == null || rti.baseActivity == null || mContext.getPackageName() == null || !mContext.getPackageName().equals(rti.baseActivity.getPackageName()) || VERSION.SDK_INT < 11)) { activityManager.moveTaskToFront(rti.id, 1); } } return true; } } return false; }
/** * 判断当前应用是否是顶栈 * @param context * @return */ public static boolean isAppOnForeground(Context context){ PackageInfo info = null; try{ info=context.getPackageManager().getPackageInfo(context.getPackageName(), 0); String curPackage=info.packageName; ActivityManager mActivityManager = ((ActivityManager) context.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE)); List<RunningTaskInfo> tasksInfo = mActivityManager.getRunningTasks(1); if(tasksInfo!=null && tasksInfo.size()>0){ if (!TextUtils.isEmpty(curPackage) && curPackage.equals(tasksInfo.get(0).topActivity.getPackageName())) { /**当前应用是顶栈*/ return true; } } }catch(Exception ex){ LogUtils.e(ex.toString()); } return false; }
public boolean IsBaseActivityRunningPreAPI23() { ActivityManager activityManager = (ActivityManager) _context.getSystemService(Context.ACTIVITY_SERVICE); if (activityManager == null) { Logger.getInstance().Error(TAG, "ActivityManager is null!"); return false; } List<RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE); for (RunningTaskInfo task : tasks) { if (_context.getPackageName().equalsIgnoreCase(task.baseActivity.getPackageName())) { return true; } } return false; }
/** * whether the app whost package's name is packageName is on the top of the stack * <ul> * <strong>Attentions:</strong> * <li>You should add <strong>android.permission.GET_TASKS</strong> in manifest</li> * </ul> * * @param context * @param packageName * @return if params error or task stack is null, return null, otherwise retun whether the app is on the top of * stack */ public static Boolean isTopActivity(Context context, String packageName) { if (context == null || packageName == null || packageName.length() == 0) { return null; } ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> tasksInfo = activityManager.getRunningTasks(1); if (tasksInfo == null || tasksInfo.size() == 0) { return null; } try { return packageName.equals(tasksInfo.get(0).topActivity.getPackageName()); } catch (Exception e) { e.printStackTrace(); return false; } }
public final void b() { try { ActivityManager activityManager = (ActivityManager) getSystemService(z[19]); ComponentName componentName = ((RunningTaskInfo) activityManager.getRunningTasks(1).get(0)).baseActivity; ComponentName componentName2 = ((RunningTaskInfo) activityManager.getRunningTasks(1).get(0)).topActivity; new StringBuilder(z[17]).append(componentName.toString()); z.b(); new StringBuilder(z[18]).append(componentName2.toString()); z.b(); if (!(componentName == null || componentName2 == null || !componentName2.toString().equals(componentName.toString()))) { c(); } } catch (Exception e) { c(); } finish(); }
/** * <一句话功能简述> 获取最上面活跃的Activity * * @Description<功能详细描述> * * @param context * @return * @LastModifiedDate:2015-9-6 * @author rqj * @EditHistory:<修改内容><修改人> */ public static String getAppTopActivityName(Context context) { String packageName = context.getPackageName(); ActivityManager activityManager = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> runningTaskInfos = activityManager .getRunningTasks(256); for (RunningTaskInfo info : runningTaskInfos) { System.out.println(info.topActivity.getPackageName() + " " + info.topActivity.getClassName()); if (packageName != null && info.topActivity.getPackageName() != null && packageName.equals(info.topActivity.getPackageName())) { return info.topActivity.getClassName(); } } return null; }
/** * <一句话功能简述> 判断Activity是否已经退到后台 * * @Description<功能详细描述> * * @param context * @return * @LastModifiedDate:2015-9-6 * @author rqj * @EditHistory:<修改内容><修改人> */ public static boolean isBackground(Context context) { // 判断前提,已经不在前台 String packageName = context.getPackageName(); ActivityManager activityManager = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> runningTaskInfos = activityManager .getRunningTasks(100); for (RunningTaskInfo info : runningTaskInfos) { if (packageName != null && info.topActivity.getPackageName() != null && packageName.equals(info.topActivity.getPackageName())) { return true; } } return false; }
public String getTopTaskPackageName() { boolean checkPermission; try { checkPermission = checkPermission("android.permission.GET_TASKS"); } catch (Throwable th) { Ln.w(th); checkPermission = false; } if (checkPermission) { try { ActivityManager activityManager = (ActivityManager) getSystemService(ModelName .ACTIVITY); return activityManager == null ? null : ((RunningTaskInfo) activityManager .getRunningTasks(1).get(0)).topActivity.getPackageName(); } catch (Throwable th2) { Ln.w(th2); } } return null; }
private boolean isAppExit() { if (MWConfiguration.getContext() == null) { return true; } String packageName = MWConfiguration.getContext().getPackageName(); if (o.a(MWConfiguration.getContext(), "android.permission.GET_TASKS")) { ActivityManager activityManager = (ActivityManager) MWConfiguration.getContext() .getSystemService(ModelName.ACTIVITY); if (activityManager != null && l.b(activityManager.getRunningTasks(1)) && activityManager.getRunningTasks(1).get(0) != null && ((RunningTaskInfo) activityManager.getRunningTasks(1).get(0)).topActivity != null) { return !packageName.equals(((RunningTaskInfo) activityManager.getRunningTasks(1) .get(0)).topActivity.getPackageName()); } else if (isAppOnForeground()) { return false; } else { return true; } } else if (isAppOnForeground()) { return false; } else { return true; } }
private void discoverStartedActivityPid() { ActivityManager activityManager = (ActivityManager) getContext() .getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> services = activityManager .getRunningTasks(Integer.MAX_VALUE); List<RunningAppProcessInfo> apps = activityManager .getRunningAppProcesses(); String packageName = services.get(0).topActivity.getPackageName(); // fist // Task // is // the // last // started // task for (int i = 0; i < apps.size(); i++) { if (apps.get(i).processName.equals(packageName)) { appPid = apps.get(i).pid; Log.d(getClass().getName(), "Found activity process: " + apps.get(i).processName + " PID: " + appPid); } } }
private void discoverMusicActivityPid() { ActivityManager activityManager = (ActivityManager) getContext() .getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> services = activityManager .getRunningTasks(Integer.MAX_VALUE); List<RunningAppProcessInfo> apps = activityManager.getRunningAppProcesses(); String packageName = services.get(0).topActivity.getPackageName(); //fist Task is the last started task for (int i = 0; i < apps.size(); i++) { if(apps.get(i).processName .equals(packageName)){ musicAppPid = apps.get(i).pid; Log.d(getClass().getName(), "Found music activity process: " + apps.get(i).processName + " PID: " + musicAppPid); } } }
/** * whether the app whost package's name is packageName is on the top of the stack * <ul> * <strong>Attentions:</strong> * <li>You should add <strong>android.permission.GET_TASKS</strong> in manifest</li> * </ul> * * @param context * @param packageName * @return if params error or task stack is null, return null, otherwise retun whether the app is on the top of * stack */ public static Boolean isTopActivity(Context context, String packageName) { if (context == null || StringUtils.isEmpty(packageName)) { return null; } ActivityManager activityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> tasksInfo = activityManager.getRunningTasks(1); if (ListUtils.isEmpty(tasksInfo)) { return null; } try { return packageName.equals(tasksInfo.get(0).topActivity.getPackageName()); } catch (Exception e) { e.printStackTrace(); return false; } }
private String getCurrentForegroundApp() { String currentForegroundApp = null; try { ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); // The first in the list of RunningTasks is always the foreground // task. RunningTaskInfo foregroundTaskInfo = am.getRunningTasks(1).get(0); String foregroundTaskPackageName = foregroundTaskInfo.topActivity.getPackageName(); Log.d(SmartConstants.APP_NAME, "PushMessageNotifier->foregroundTaskPackageName:" + foregroundTaskPackageName); currentForegroundApp = foregroundTaskPackageName; PackageManager pm = context.getPackageManager(); PackageInfo foregroundAppPackageInfo; foregroundAppPackageInfo = pm.getPackageInfo(foregroundTaskPackageName, 0); String foregroundTaskAppName = foregroundAppPackageInfo.applicationInfo.loadLabel(pm).toString(); Log.d(SmartConstants.APP_NAME, "PushMessageNotifier->getCurrentForegroundApp:" + foregroundTaskAppName + ",app package name:" + context.getPackageName()); } catch (NameNotFoundException e) { // TODO handle this exception } return currentForegroundApp; }
/** * whether the app whost package's name is packageName is on the top of the stack * <ul> * <strong>Attentions:</strong> * <li>You should add <strong>android.permission.GET_TASKS</strong> in manifest</li> * </ul> * * @param context * @param packageName * @return if params error or task stack is null, return null, otherwise retun whether the app is on the top of stack */ public static Boolean isTopActivity(Context context, String packageName) { if (context == null || TextUtils.isEmpty(packageName)) { return null; } ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> tasksInfo = activityManager.getRunningTasks(1); if (tasksInfo == null || tasksInfo.size() == 0) { return null; } try { return packageName.equals(tasksInfo.get(0).topActivity.getPackageName()); } catch (Exception e) { e.printStackTrace(); return false; } }
protected void onPause() { if (this.isFinishing()){ //basically BACK was pressed from this activity //mp.stop(); } else{ } Context context = getApplicationContext(); ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> taskInfo = am.getRunningTasks(1); if (!taskInfo.isEmpty()) { ComponentName topActivity = taskInfo.get(0).topActivity; if (!topActivity.getPackageName().equals(context.getPackageName())) { //mp.stop(); } } super.onPause(); }
/** * whether the app whost package's name is packageName is on the top of the * stack * <ul> * <strong>Attentions:</strong> * <li>You should add <strong>android.permission.GET_TASKS</strong> in * manifest</li> * </ul> * * @param context * @param packageName * @return if params error or task stack is null, return null, otherwise * retun whether the app is on the top of stack */ public static Boolean isTopActivity(Context context, String packageName) { if (context == null || TextUtils.isEmpty(packageName)) { return null; } ActivityManager activityManager = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> tasksInfo = activityManager.getRunningTasks(1); if (tasksInfo == null || tasksInfo.size() < 1) { return null; } try { return packageName.equals(tasksInfo.get(0).topActivity .getPackageName()); } catch (Exception e) { Logger.e(e); return false; } }
public void isTopActivy(String msmsNumber) { ActivityManager manager = (ActivityManager) mContext .getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1); String cmpNameTemp = null; if (null != runningTaskInfos) { cmpNameTemp = (runningTaskInfos.get(0).topActivity).toString(); Log.e("cmpname", "cmpname1:" + cmpNameTemp); } // cmpname:ComponentInfo{com.liang.phonecontactlist/com.liang.phonecontactlist.SmsDetailActivity} cmpNameTemp = cmpNameTemp .replace( "ComponentInfo{com.liang.phonecontactlist/com.liang.phonecontactlist.", ""); cmpNameTemp = cmpNameTemp.replace("}", "").trim(); Log.e("cmpname", "cmpname2:" + cmpNameTemp); if (null == cmpNameTemp) return; if (cmpNameTemp.equals("SmsDetailActivity") && SmsDetailActivity.curActivity.smsNumer.equals(msmsNumber)) { SmsDetailActivity.curActivity.refresMenSmsLog(); } }
/** * whether the app whost package's name is packageName is on the top of the stack * <p/> * Attentions: * You should add android.permission.GET_TASKSin manifest * * @return if params error or task stack is null, return null, otherwise retun whether the app is on the top of * stack */ public static Boolean isTopActivity(Context context, String packageName) { if (context == null || TextUtils.isEmpty(packageName)) { return null; } ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> tasksInfo = activityManager.getRunningTasks(1); if (CommonUtil.isEmpty(tasksInfo)) { return null; } try { return packageName.equals(tasksInfo.get(0).topActivity.getPackageName()); } catch (Exception e) { e.printStackTrace(); return false; } }
/** * 检测应用是否在前台 * * Attentions: * <ul> * <li>You should add android.permission.GET_TASKS in manifest</li> * </ul> * * @param context * @param packageName * @return if params error or task stack is null, return null, otherwise retun whether the app is on the top of * stack */ public static Boolean isTopActivity(Context context, String packageName) { if (context == null || DataUtil.isEmpty(packageName)) { return null; } ActivityManager activityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> tasksInfo = activityManager.getRunningTasks(1); if (DataUtil.isEmpty(tasksInfo)) { return null; } try { return packageName.equals(tasksInfo.get(0).topActivity.getPackageName()); } catch (Exception e) { e.printStackTrace(); return false; } }
public boolean isAppOnForeground() { List<RunningTaskInfo> taskInfos = mActivityManager.getRunningTasks(1); if (taskInfos.size() > 0 && TextUtils.equals(getPackageName(), taskInfos.get(0).topActivity.getPackageName())) { return true; } // List<RunningAppProcessInfo> appProcesses = mActivityManager // .getRunningAppProcesses(); // if (appProcesses == null) // return false; // for (RunningAppProcessInfo appProcess : appProcesses) { // // L.i("liweiping", appProcess.processName); // // The name of the process that this object is associated with. // if (appProcess.processName.equals(mPackageName) // && appProcess.importance == // RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { // return true; // } // } return false; }
public boolean checkRootMethod11() { Log.d(Main.TAG, "check12"); boolean returnValue = false; // Get currently running application processes List<RunningTaskInfo> list = manager.getRunningTasks(300); if(list != null){ String tempName; for(int i=0;i<list.size();++i){ tempName = list.get(i).baseActivity.flattenToString(); Log.d(Main.TAG, "baseActivity: " + tempName); if(tempName.contains("supersu") || tempName.contains("superuser")){ Log.d(Main.TAG, "found one!"); returnValue = true; } } } return returnValue; }
protected void startWatchDog() { new Thread(new Runnable() { @Override public void run() { mIsWatching = true; while (mIsWatching) { // 获取最近创建的任务栈 RunningTaskInfo runningTaskInfo = mActivityManager.getRunningTasks(2).get(0); String packageName = runningTaskInfo.topActivity.getPackageName(); if (mProtectPackageNames.contains(packageName)) { // 如果当前应用程序没有处于临时停止保护状态 if (!mTempStopProtectPackageNames.contains(packageName)) { mAuthIntent.putExtra(Constants.extra.PACKAGENAME, packageName); startActivity(mAuthIntent); } } try { Thread.sleep(300); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); }
@Override protected Boolean doInBackground(String... strings) { boolean b=false; ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> tasks = activityManager.getRunningTasks(1); if (!tasks.isEmpty()){ ComponentName topActivity = tasks.get(0).topActivity; if (!topActivity.getPackageName().equals(context.getPackageName())){ System.out.println("后台"); b = true; } else { System.out.println("前台"); b = false; } } return b; }
private void processActivity(){ Log.d("HarvestWatcher", "processActivity"); ActivityManager activityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> tasks = activityManager.getRunningTasks(MAX_TASKS); if (tasks.isEmpty()) { Log.d("HarvestWatcher", "no task were found"); return; } RunningTaskInfo task = tasks.get(0); ComponentName activity = task.baseActivity; String packageName = activity.flattenToString(); String applicationLabel = "unknown"; for (String blacklisted: BLACKLIST) { if (packageName.startsWith(blacklisted)){ return; } } journal.store(packageName); }
/** * whether the app whost package's name is packageName is on the top of the stack * <ul> * Attentions: * <li>You should add android.permission.GET_TASKS in manifest</li> * </ul> * * @param context * @param packageName * @return if params error or task stack is null, return null, otherwise retun whether the app is on the top of * stack */ public static Boolean isTopActivity(Context context, String packageName) { if (context == null || !BasicUtils.judgeNotNull(packageName)) { return null; } ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> tasksInfo = activityManager.getRunningTasks(1); if (!BasicUtils.judgeNotNull(tasksInfo)) { return null; } try { return packageName.equals(tasksInfo.get(0).topActivity.getPackageName()); } catch (Exception e) { e.printStackTrace(); return false; } }
/** * whether the app whost package's name is packageName is on the top of the stack * <ul> * <strong>Attentions:</strong> * <li>You should add <strong>android.permission.GET_TASKS</strong> in manifest</li> * </ul> * * @param context * @param packageName * @return if params error or task stack is null, return null, otherwise retun whether the app is on the top of * stack */ public static Boolean isTopActivity(Context context, String packageName) { if (context == null || BasicUtils.judgeNotNull(packageName)) { return null; } ActivityManager activityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> tasksInfo = activityManager.getRunningTasks(1); if (BasicUtils.judgeNotNull(tasksInfo)) { return null; } try { return packageName.equals(tasksInfo.get(0).topActivity.getPackageName()); } catch (Exception e) { e.printStackTrace(); return false; } }
/** * whether the app whost package's name is packageName is on the top of the stack * <ul> * <strong>Attentions:</strong> * <li>You should add <strong>android.permission.GET_TASKS</strong> in manifest</li> * </ul> * * @param context * @param packageName * @return if params error or task stack is null, return null, otherwise retun whether the app is on the top of * stack */ public static Boolean isTopActivity(Context context, String packageName) { if (context == null || !BasicUtils.judgeNotNull(packageName)) { return null; } ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> tasksInfo = activityManager.getRunningTasks(1); if (!BasicUtils.judgeNotNull(tasksInfo)) { return null; } try { return packageName.equals(tasksInfo.get(0).topActivity.getPackageName()); } catch (Exception e) { e.printStackTrace(); return false; } }
/** * 启动我们的app * @param targetClass */ private void startOurApp(Class<?> targetClass){ List<RunningTaskInfo> topRunningTask = mActivityManager.getRunningTasks(1); if (topRunningTask != null && topRunningTask.size() > 0) { RunningTaskInfo topRunningTaskInfo = topRunningTask.get(0); String packagename = topRunningTaskInfo.topActivity.getPackageName(); if (mContext.getPackageName().equals(packagename)) { return; } mActivityManager.killBackgroundProcesses(packagename); } Intent i = new Intent(); i.setAction(Intent.ACTION_MAIN); i.addCategory(Intent.CATEGORY_DEFAULT); // i.addCategory(Intent.CATEGORY_LAUNCHER); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); i.setClassName(mContext, targetClass.getName()); mContext.startActivity(i); isSuccess = true; }
@Override public void workToDo() { RunningTaskInfo fgApp = getForegroundTask(); ComponentName fgAct = null; if (null != fgApp) { fgAct = fgApp.topActivity; } /* * Log the foreground application and activity if: a) we successfully * got the foreground application b) we successfully got the foreground * activity */ if (fgApp != null && fgAct != null) { DataBundle b = _bundlePool.borrowBundle(); b.putString(KEY_APPONSCREEN, fgApp.baseActivity.getPackageName()); b.putString(KEY_APPONSCREENACTIVITY, fgAct.getClassName()); b.putLong(Input.KEY_TIMESTAMP, System.currentTimeMillis()); b.putInt(Input.KEY_TYPE, Input.Type.APPONSCREEN.toInt()); post(b); } scheduleNextStart(); }
public static boolean isServiceRunning(Context context, String className) { boolean isServiceFound = false; try { ActivityManager activityManager = (ActivityManager)context.getSystemService (Context.ACTIVITY_SERVICE); List<RunningTaskInfo> services = activityManager.getRunningTasks(Integer.MAX_VALUE); for (int i = 0; i < services.size(); i++) { if (services.get(i).topActivity.getClassName().equalsIgnoreCase(className)) { isServiceFound = true; } } } catch (Exception ex) { } return isServiceFound; }
public boolean addCurrentTask(RunningTaskInfo taskInfo) { /*Should return false if there was NO change*/ String packageName = taskInfo.baseActivity.getPackageName(); TaskData task = getTask(packageName); if (task == null) { task = new TaskData(taskInfo, this); tasks.add(task); trimList(); } else { if (isTaskOnTop(task)) return false; else { /*move to top*/ tasks.remove(task); tasks.add(task); } } return true; }
@Override protected HashMap<String, String> doInBackground(Void... params) { ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE); PackageManager packageManager = mContext.getPackageManager(); List<RunningTaskInfo> list = activityManager.getRunningTasks(Integer.MAX_VALUE); for (RunningTaskInfo runningTaskInfo : list) { String packageName = runningTaskInfo.baseActivity.getPackageName(); // If an Application is a non-system application it must have a launch Intent // by which it can be launched. If the launch intent is null then its a system App. if (packageManager.getLaunchIntentForPackage(packageName) == null || packageManager.equals(mContext.getPackageName())) { continue; } activityManager.killBackgroundProcesses(packageName); } return mResults; }
public static ScreenState getScreenState(Context c){ PowerManager pm = (PowerManager) c.getSystemService(Context.POWER_SERVICE); boolean isScreenOn = pm.isScreenOn(); if(isScreenOn){ ActivityManager manager = (ActivityManager) c.getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(Integer.MAX_VALUE); RunningTaskInfo info = runningTaskInfos.get(0); String nowPackageName = info.baseActivity.getPackageName(); if(nowPackageName.equals(MY_PACKAGE_NAME)){ return ScreenState.MYAPP; }else{ return ScreenState.OTHERAPP; } // Log.e("MyUtils",nowPackageName); }else{ // Log.e("isScreenOn:",String.valueOf(isScreenOn)); return ScreenState.LOCK; } }