我已经开发了一个小型的计时器应用程序,并且每当我接到电话时,它就会启动。例如,在接收到呼入电话后,我会启动我的计时器应用并在前台显示计时器。
但是它只能在 lollipop 5.1 版本中在后台运行,而在低端版本中它却在后台运行。
我需要在所有设备上都将应用程序运行在前台,请问如何实现?
我的代码:
Intent it = new Intent("intent.my.action");
it.setComponent(new ComponentName(context.getPackageName(), timer.class.getName()));
it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.getApplicationContext().startActivity(it);
我的接收器:
public class CallerToActivity extends BroadcastReceiver {
static boolean wasRinging = false;
static boolean finish = false;
SessionManager session;
private boolean enable;
public void onReceive(Context context, Intent intent) {
session = new SessionManager(context);
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
enable = session.Is_Enabled();
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
Log.d("Status", "Phone is Ringing");
} else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
Intent it = new Intent("intent.my.action");
it.putExtra("Call", "true");
it.setComponent(new ComponentName(context.getPackageName(), timer.class.getName()));
it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.getApplicationContext().startActivity(it);
} else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
// Call Dropped or rejected
Toast.makeText(context, "phone is neither ringing nor in a call",
Toast.LENGTH_SHORT).show();
// wasRinging = false;
finish = true;
System.exit(0);
Log.d("Status", "Phone is dropped");
}
}
}