Activity popups over incoming call screen

后端 未结 1 682
暗喜
暗喜 2020-12-30 13:53

I\'m trying to override the incoming call screen - I know I can\'t change it so I\'m trying to popup an activity ontop.

My code works fine except when the phone has

相关标签:
1条回答
  • 2020-12-30 14:22

    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:

    • 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

    My sample activity:

    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);
                }
            };
        };
    
    }
    
    0 讨论(0)
提交回复
热议问题