赞
踩
在我们开发的过程中,经常遇到app进入后台,或者切换到前台的情况。
比如:
如图显示:压后台10秒钟,自动弹出
实现主要分为两个步骤:
第一步通过ActivityLifecycleCallbacks判断是否在前后台显示
private void registerActivityLifecycleCallbacks() { registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() { @Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) { } @Override public void onActivityStarted(Activity activity) { mCount++; if (!mFront) { mFront = true; Log.e("", "AppContext------->处于前台"); } } @Override public void onActivityResumed(Activity activity) { } @Override public void onActivityPaused(Activity activity) { } @Override public void onActivityStopped(Activity activity) { mCount--; if (mCount == 0) { mFront = false; Log.e("", "AppContext------->处于后台"); timerTask(); } } @Override public void onActivitySaveInstanceState(Activity activity, Bundle outState) { } @Override public void onActivityDestroyed(Activity activity) { } }); }
第二部通过
List<ActivityManager.RunningTaskInfo> taskInfoList = activityManager.getRunningTasks(20)
的RunningTasks,
ActivityManager的moveTaskToFront(rti.id, ActivityManager.MOVE_TASK_WITH_HOME);
方法将应用唤起到前台
@SuppressLint("NewApi") public static void isRunningForegroundToApp1(Context context, final Class Class) { ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningTaskInfo> taskInfoList = activityManager.getRunningTasks(20); /**枚举进程*/ for (ActivityManager.RunningTaskInfo taskInfo : taskInfoList) { //*找到本应用的 task,并将它切换到前台 if (taskInfo.baseActivity.getPackageName().equals(context.getPackageName())) { Log.e("timerTask", "timerTask pid " + taskInfo.id); Log.e("timerTask", "timerTask processName " + taskInfo.topActivity.getPackageName()); Log.e("timerTask", "timerTask getPackageName " + context.getPackageName()); activityManager.moveTaskToFront(taskInfo.id, ActivityManager.MOVE_TASK_WITH_HOME); Intent intent = new Intent(context, Class); intent.addCategory(Intent.CATEGORY_LAUNCHER); intent.setAction(Intent.ACTION_MAIN); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); context.startActivity(intent); break; } } }
然后通过Intent 跳转到对应界面 Class的启动模式一定是singleTask
Intent intent = new Intent(context, Class);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setAction(Intent.ACTION_MAIN);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
context.startActivity(intent);
完成代码如下:
package com.click.backdesk; import android.annotation.SuppressLint; import android.app.Activity; import android.app.ActivityManager; import android.app.Application; import android.content.Context; import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.util.Log; import androidx.annotation.RequiresApi; import java.util.List; import java.util.Timer; import java.util.TimerTask; public class MyAppliction extends Application { public static MyAppliction sInstance; public static int mCount; public static boolean mFront;//是否前台 private static Timer mTimer; static int cntStart = 0; static int waitTime = 10; static TimerTask mTask = null; protected void attachBaseContext(Context base) { super.attachBaseContext(base); } @Override public void onCreate() { super.onCreate(); sInstance = this; registerActivityLifecycleCallbacks(); } private void registerActivityLifecycleCallbacks() { registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() { @Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) { } @Override public void onActivityStarted(Activity activity) { mCount++; if (!mFront) { mFront = true; Log.e("", "AppContext------->处于前台"); } } @Override public void onActivityResumed(Activity activity) { } @Override public void onActivityPaused(Activity activity) { } @Override public void onActivityStopped(Activity activity) { mCount--; if (mCount == 0) { mFront = false; Log.e("", "AppContext------->处于后台"); timerTask(); } } @Override public void onActivitySaveInstanceState(Activity activity, Bundle outState) { } @Override public void onActivityDestroyed(Activity activity) { } }); } private static void timerTask() { if (mTimer == null && mTask == null) { mTimer = new Timer(); mTask = new TimerTask() { @RequiresApi(api = Build.VERSION_CODES.Q) @Override public void run() { cntStart++; Log.e("timerTask", "timerTask " + cntStart); if (waitTime == cntStart) { // go(); isRunningForegroundToApp1(sInstance.getBaseContext(), FristActivity.class); cancelTimer(); Log.e("timerTask", "timerTask 30 " + cntStart); } } }; mTimer.schedule(mTask, 0, 1000); } } @SuppressLint("NewApi") public static void isRunningForegroundToApp1(Context context, final Class Class) { ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningTaskInfo> taskInfoList = activityManager.getRunningTasks(20); /**枚举进程*/ for (ActivityManager.RunningTaskInfo taskInfo : taskInfoList) { //*找到本应用的 task,并将它切换到前台 if (taskInfo.baseActivity.getPackageName().equals(context.getPackageName())) { Log.e("timerTask", "timerTask pid " + taskInfo.id); Log.e("timerTask", "timerTask processName " + taskInfo.topActivity.getPackageName()); Log.e("timerTask", "timerTask getPackageName " + context.getPackageName()); activityManager.moveTaskToFront(taskInfo.id, ActivityManager.MOVE_TASK_WITH_HOME); Intent intent = new Intent(context, Class); intent.addCategory(Intent.CATEGORY_LAUNCHER); intent.setAction(Intent.ACTION_MAIN); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); context.startActivity(intent); break; } } } private static void cancelTimer() { cntStart = 0; if (mTimer != null) { mTimer.cancel(); mTimer = null; } if (mTask != null) { mTask.cancel(); mTask = null; } } }
<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" /> <uses-permission android:name="android.permission.REORDER_TASKS" /> <application android:name=".MyAppliction" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".FristActivity" android:launchMode="singleTask" /> </application>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。