極光推送App各種狀態處理
阿新 • • 發佈:2019-01-22
/**
* 主瀏覽處於棧頂
* 1.應用處於前臺 重新整理主頁面
* 2.應用處於後臺 開啟應用 重新整理主頁面
*
* 主瀏覽器不在棧頂
* 1.應用處於前臺 隱藏登入頁 重新整理主頁面
* 2.應用處於後臺 開啟應用 隱藏登入頁面 重新整理主頁面
*/
【MyReceiver】
if (AppUtil.isApplicationBroughtToBackground(context)) {//後臺
Intent resultIntent = new Intent(context, MainActivity.class );
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
resultIntent.putExtra("url", url);
resultIntent.putExtra("push", "push");
context.startActivity(resultIntent);
EventBus.getDefault().post(new EventBusBean("push" , "", "", "", url));
} else {//前臺
if (AppUtil.isActivityTop(MainActivity.class, context)) {
//棧頂 重新整理主頁面
EventBus.getDefault().post(new EventBusBean("push", "", "", "", url));
} else {
//關閉登入頁面 重新整理主頁面
//存在的問題 比如主頁面之前開啟過別的頁面 比如手機登入 這時候點選返回健會有記錄 體驗不太好
EventBus.getDefault ().post(new EventBusBean("push", "", "", "", url));
EventBus.getDefault().post(new EventBusBean("finish"));
}
}
LogUtil.e(TAG, "處於後臺?=" + AppUtil.isApplicationBroughtToBackground(context) + "程式執行中?=" + AppUtil.isRunning(context) + "棧頂?=" + AppUtil.isActivityTop(MainActivity.class, context) + "url:" + url);
【AppUtil】判斷app的各種狀態
import android.app.Activity;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import java.util.List;
/**
* <pre>
* author :
* e-mail :
* time : 2017/11/15
* desc : 程式是否執行 處於前臺還是後臺 等app狀態
* version: 1.0
* </pre>
*/
public class AppUtil {
/**
* 判斷當前應用程式處於前臺還是後臺
*/
public static boolean isApplicationBroughtToBackground(final Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
if (!tasks.isEmpty()) {
ComponentName topActivity = tasks.get(0).topActivity;
if (!topActivity.getPackageName().equals(context.getPackageName())) {
return true;
}
}
return false;
}
/**
* 後臺轉前臺
*
* @param context
*/
public static void notifyAppToFront(Context context, Activity activity) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskList = activityManager.getRunningTasks(100);
for (ActivityManager.RunningTaskInfo task : taskList) {
//找到當前應用的task,並啟動task的棧頂activity,達到程式切換到前臺
if (task.topActivity.getPackageName().equals(context.getPackageName())) {
try {
Intent resultIntent = new Intent(context, Class.forName(task.topActivity.getClassName()));
resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(resultIntent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return;
}
}
//若沒有找到執行的task,使用者結束了task或被系統釋放,則重新啟動activity
Intent resultIntent = new Intent(context, activity.getClass());
resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(resultIntent);
}
/**
* 判斷某activity是否處於棧頂
* 如果當前應用在前臺好用 後臺有些不靈敏
*
* @return true在棧頂 false不在棧頂
*/
public static boolean isActivityTop(Class cls, Context context) {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
String name = manager.getRunningTasks(1).get(0).topActivity.getClassName();
return name.equals(cls.getName());
}
/**
* 程式是否正在執行
*
* @param context
* @return
*/
public static boolean isRunning(Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(100);
for (ActivityManager.RunningTaskInfo info : list) {
if (info.topActivity.getPackageName().equals("com.tailin.webapps") && info.baseActivity.getPackageName().equals("com.tailin.webapps")) {
return true;
}
}
return false;
}
}