简化开发

监听多任务,给app多任务窗口模糊处理

2022-09-20  本文已影响0人  霁逸lei

监听进多任务,进入多任务给当前Activity的DecorView添加ImageView蒙层

1.添加权限
<uses-permission android:name="android.permission.REORDER_TASKS" />
2.广播监听
public void onCreate(Bundle savedInstanceState) {
  receiver = new RecentAppsReceiver();
  IntentFilter homeFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
  registerReceiver(receiver, homeFilter);
}

protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(receiver);
}

class RecentAppsReceiver extends BroadcastReceiver {

    private final String SYSTEM_DIALOG_REASON_KEY = "reason";
    private final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
    private final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
            String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);

            if (reason == null)
                return;

            // Home键
            if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) {
                Log.d("MyReceiver", "按了Home键");
            }

            // 最近任务列表键
            if (reason.equals(SYSTEM_DIALOG_REASON_RECENT_APPS)) {
                Log.d("MyReceiver", "按了最近任务列表");
                Activity activity = tdxAppFuncs.getInstance().getMainActivity();
                ViewGroup rootView = activity.getWindow().getDecorView().findViewById(android.R.id.content);
                View blurView = rootView.findViewById(R.id.view_recent_apps_blur);
                if (blurView != null){
                    return;
                }
                ImageView imageView = new ImageView(activity);
                imageView.setImageResource(R.drawable.shape_blur_bg);
                imageView.setId(R.id.view_recent_apps_blur);
                rootView.addView(imageView,new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            }
        }
    }
}

onStart内移除该View

registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
    }

    @Override
    public void onActivityStarted(Activity activity) {
        ViewGroup rootView = activity.getWindow().getDecorView().findViewById(android.R.id.content);
        View blurView = rootView.findViewById(R.id.view_recent_apps_blur);
        if (blurView != null){
            rootView.removeView(blurView);
        }
    }

    @Override
    public void onActivityResumed(Activity activity) {
    }

    @Override
    public void onActivityPaused(Activity activity) {
    }

    @Override
    public void onActivityStopped(Activity activity) {
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
    }

    @Override
    public void onActivityDestroyed(Activity activity) {
    }
});
上一篇下一篇

猜你喜欢

热点阅读