简易Android专注模式的实现
这里的专注模式主要是通过将后台应用唤醒到前台以及界面延时切换实现的,实现效果如下:
接下来是具体的实现过程。
一、将后台应用唤醒到前台
1.新建一个项目,在原有的MainActivity的基础上再新建一个Activity叫做WarningActivity继承自AppCompatActivity
2.写一个帮助类来监听应用前后台状态
import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
public class AppFrontBackHelper {
private OnAppStatusListener mOnAppStatusListener;
public AppFrontBackHelper() {
}
public void register(Application application, OnAppStatusListener listener) {
mOnAppStatusListener = listener;
application.registerActivityLifecycleCallbacks(activityLifecycleCallbacks);
}
public void unRegister(Application application) {
application.unregisterActivityLifecycleCallbacks(activityLifecycleCallbacks);
}
private Application.ActivityLifecycleCallbacks activityLifecycleCallbacks = new Application.ActivityLifecycleCallbacks() {
private int activityStartCount = 0;
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
}
@Override
public void onActivityStarted(Activity activity) {
activityStartCount++;
if (activityStartCount == 1) {
if (mOnAppStatusListener != null) {
mOnAppStatusListener.onFront();
}
}
}
@Override
public void onActivityResumed(Activity activity) {
}
@Override
public void onActivityPaused(Activity activity) {
}
@Override
public void onActivityStopped(Activity activity) {
activityStartCount--;
if (activityStartCount == 0) {
if (mOnAppStatusListener != null) {
mOnAppStatusListener.onBack();
}
}
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
}
@Override
public void onActivityDestroyed(Activity activity) {
}
};
public interface OnAppStatusListener {
void onFront();
void onBack();
}
}
3.在Application中使用
public class MyApp extends Application {
public static MyApp sInstance;
@Override
public void onCreate() {
super.onCreate();
sInstance = this;
AppFrontBackHelper helper = new AppFrontBackHelper();
helper.register(MyApp.this, new AppFrontBackHelper.OnAppStatusListener() {
@Override
public void onFront() {
Log.i("1","在前台");
}
@Override
public void onBack() {
Log.i("2","在后台");
isRunningForegroundToApp1(sInstance, MainActivity.class);
}
});
}
}
其实就是使Application注册ActivityLifecycleCallbacks,这样,当app里每一个activity的生命周期发生时,Application都能监听到。根据activity的public void onActivityStarted(Activity activity) 和public void onActivityStopped(Activity activity)的次数来判断app是否处于前台。因为当app处于前台时,必定有一个activity执行了onActivityStarted而没有执行onActivityStopped,所以app内打开的Activity数量统计必定为1,当app切换到后台时,activityStartCount 就会为0.
(这句话并不是本人写的,详细可查看下面的链接)
4.然后再往MyApp中添加isRunningForegroundToApp1方法,这个方法将应用唤起到前台
@SuppressLint("NewApi")
public 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) {
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 intent1 = new Intent(context, WarningActivity.class);
context.startActivity(intent1);
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;
}
}
}
这里唤醒警告界面实现在唤醒主界面之前,为了应用切换到后台后先弹出警告界面,再弹出主界面
5.最后,修改AndroidManifest.xml文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.finalwork">
<uses-permission android:name="android.permission.REORDER_TASKS" />
<application
android:name=".MyApp"
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/NoTitle">
<activity
android:name=".MainActivity"
android:exported="true"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".WarningActivity">
</activity>
</application>
</manifest>
这里要声明MainActivity和WarningActivity的启动顺序,将intent-filter设在主界面MainActivity中,这样应用第一次出来时,显示的就是主界面
到此,将后台应用唤醒到前台就实现了。
下面是界面延时切换的具体实现过程。
二、界面延时切换
在这里用到的核心函数为Handler.sendEmptyMessageDelayed,主要用来发送延迟消息
首先新建一个消息处理对象,负责发送与处理消息
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
}
};
在handleMessage方法中处理消息,在这里接收到消息不做复杂处理以后直接执行跳转操作
界面延时切换功能主要是在WarningActivity中实现,代码如下:
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.appcompat.app.AppCompatActivity;
public class WarningActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_warning);
handler.sendEmptyMessageDelayed(0,3000);
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
getHome();
super.handleMessage(msg);
}
};
public void getHome(){
Intent intent = new Intent(WarningActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
}
这样,一个简易的Android专注模式就实现了
作者:王政
|