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
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:
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);
}
};
};
}