且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

活动弹出窗口在来电屏幕

更新时间:2023-02-20 14:25:32

在手机屏幕是关闭的,有来电,还有更多的工作要做(醒来,处理键盘锁认为...),所以在中调用活动需要较长时间才能显示出来,这导致的情况下,您呼叫的活动早于在通话活动开始启动 - >将在调用活动是在上面
还有就是在调用活动需要显示器(你已经尝试过,看到700毫秒是不够的)的
没有确切时间 我的解决办法:保持跟踪呼叫活动的状态:

When the phone screen is off and there is an incoming call, there are more work to do (waking up, dealing with the keyguard view...) so the in-call activity take longer to show up and this lead to the case your Call activity starts earlier than the in-call activity starts --> the in-call activity is on top
There is no exact time that the in-call activity need to displays (you have tried and see that 700 miliseconds is not enough)

My solution: keep tracking the state of Call activity:

  • 如果它仍然在顶部,用户没有关闭它(或任何条件 解雇),只要保持跟踪使用handler
  • 如果有任何其他活动,去前台然后呼叫活动 试图让回到顶部
  • If it's still on top and user has not dismiss it (or any condition to dismiss), just keep tracking using the handler
  • If there is any other activity go to foreground then Call activity try to get the back to top

我的示例活动:

public class MainActivity extends Activity {
    private ActivityManager mActivityManager;
    private boolean mDismissed = false;

    private static final int MSG_ID_CHECK_TOP_ACTIVITY = 1;
    private static final long DELAY_INTERVAL = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

        mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        mHandler.sendEmptyMessageDelayed(MSG_ID_CHECK_TOP_ACTIVITY,
                DELAY_INTERVAL);
    }

    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (msg.what == MSG_ID_CHECK_TOP_ACTIVITY && !mDismissed) {
                List<RunningTaskInfo> tasks = mActivityManager
                        .getRunningTasks(1);
                String topActivityName = tasks.get(0).topActivity
                        .getClassName();
                if (!topActivityName.equals(MainActivity.this
                        .getComponentName().getClassName())) {
                    // Try to show on top until user dismiss this activity
                    Intent i = new Intent();
                    i.setClass(MainActivity.this, MainActivity.class);
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    i.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
                    startActivity(i);
                }
                sendEmptyMessageDelayed(MSG_ID_CHECK_TOP_ACTIVITY,
                        DELAY_INTERVAL);
            }
        };
    };

}