1. 判斷應用是否在最前端,前端直接將推送在頁面顯示,不在前端,推送到通知欄
public static boolean isForegroundDisplay(Context context) { String packageName = context.getPackageName(); ActivityManager am = (ActivityManager)context.getSystemService("activity"); List tasksInfo; if(VERSION.SDK_INT > 20) { tasksInfo = am.getRunningAppProcesses(); Iterator var4 = tasksInfo.iterator(); while(true) { RunningAppProcessInfo processInfo; do { if(!var4.hasNext()) { return false; } processInfo = (RunningAppProcessInfo)var4.next(); } while(processInfo.importance != 100); String[] var6 = processInfo.pkgList; int var7 = var6.length; for(int var8 = 0; var8 < var7; ++var8) { String activeProcess = var6[var8]; if(activeProcess.equals(packageName)) { return true; } } } } else { tasksInfo = am.getRunningTasks(1); if(tasksInfo.size() > 0 && packageName.equals(((RunningTaskInfo)tasksInfo.get(0)).topActivity.getPackageName())) { return true; } else { return false; } } }
2. 判斷應用是否已經啓動,是,直接調起界面,否,調起應用
//判斷某一個類是否存在任務棧裏面 public static boolean isExistMainActivity(Context context, Class<?> activity){ Intent intent = new Intent(context, activity); ComponentName cmpName = intent.resolveActivity(context.getPackageManager()); boolean flag = false; if (cmpName != null) { // 說明系統中存在這個activity ActivityManager am = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE); List<ActivityManager.RunningTaskInfo> taskInfoList = am.getRunningTasks(10); //獲取從棧頂開始往下查找的10個activity for (ActivityManager.RunningTaskInfo taskInfo : taskInfoList) { if (taskInfo.baseActivity.equals(cmpName)) { // 說明它已經啓動了 flag = true; break; //跳出循環,優化效率 } } } return flag; }
Intent it = new Intent(context, Activity_Logo.class); it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT); context.startActivity(it);
Intent it = new Intent(context, Activity_Logo.class); it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); it.putExtra(Activity_Logo.INTENT_AFTER_LOGO_KEY, JSONObject.toJSONString(pushMsgVo)); context.startActivity(it);
3. 調起界面的時候,經常會重新進入閃屏頁,明明只需要調起
if (null != Manager_MyInfo.getInstance(this).getMyInfo() && (getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) { Log.i(TAG, "already started...... finished"); finish(); return; }
4. 應用啓動,順序A->B->C,喚起其他應用,當系統內存不足時,當前應用被殺掉,應用進程已經被殺掉了,在應用棧中存在,如果直接從棧點擊,會先調起C,這個時候很可能和服務器的連接信息等已經不存在了,所以最好登錄完成之後的界面都繼承一個BaseActivity,如果判斷到沒有登錄信息,沒有用戶信息,去重新進行登錄,或者重新調轉至閃屏頁:Application中在用戶登錄完成之後設置User信息,沒有User信息,說明當前進程是已經被殺掉的,直接跳轉至登錄頁,注意登錄頁不需要繼承BaseActivity中的判斷
5. 推送當時調了半天,最後發現被VIVO手機坑了,沒注意有個伸縮的箭頭,點擊的時候一直跳轉到主頁,後來才發現,點擊那個伸縮的進行,才真正出現通知內容,點擊時,才能調到對應debug內容