更新时间: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:
我的示例活动:
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);
}
};
};
}