目前,当我运行我的应用程序时,如果手机响起,手机会获取首选项,并且我的应用程序被终止。有没有什么办法可以让我的应用程序获得一个首选项,即让电话打到语音邮件或短时间将我的应用程序切换到背景,直到用户接听电话,并在完成后回到前台。谢谢如何让应用程序在后台运行时在手机上响铃android
3
A
回答
1
我认为这是Android的默认功能,任何应用程序 如果传入呼叫处于活动状态将变为非活动状态。我们不能改变这一点。
当用户在打电话,虽然,他们只需按home键和 开始另一个应用程序的主屏幕切换到另一个 的应用程序,或者通过双按home键并切换到另包括你的应用程序。
+1
我的应用程序正在计算几个数字。当您使应用程序处于非活动状态时,所有会话特定变量都将重新初始化,因此数据会变得松散任何想法。 – Namit
2
你可以做一件事。您可以在来电时暂停应用程序,然后从同一地方恢复应用程序。我知道这不是你问题的确切解决方案,但不知何故,它会减少你的工作量。希望这会有所帮助。
private class PhoneCallListener extends PhoneStateListener {
private boolean isPhoneCalling = false;
// needed for logging
String TAG = "PhoneCallListener";
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if (TelephonyManager.CALL_STATE_RINGING == state) {
// phone ringing
Log.i(TAG, "RINGING, number: " + incomingNumber);
}
if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
// active
Log.i(TAG, "OFFHOOK");
isPhoneCalling = true;
}
if (TelephonyManager.CALL_STATE_IDLE == state) {
// run when class initial and phone call ended,
// need detect flag from CALL_STATE_OFFHOOK
Log.i(TAG, "IDLE");
if (isPhoneCalling) {
Log.i(TAG, "restart app");
// restart call application
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(
getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
isPhoneCalling = false;
}
}
}
}
,并添加此权限Manifest.xml文件
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
1
我曾经碰到过类似的问题,通过重写的onPause()和的onResume()方法解决了这个,保存所有必需的变量在onPause()中并在onResume()中恢复它们。
@Override
protected void onResume(){
super.onResume();
load();
}
@Override
protected void onPause(){
super.onPause();
save();
}
private void save() {
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("DeviceName", deviceName);
editor.putString("ConnectOption", connectOption.toString());
editor.commit();
}
private void load() {
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
deviceName = sharedPreferences.getString("DeviceName","");
String connectop = sharedPreferences.getString("ConnectOption","USB"); //You could provide a default value here
}
相关问题
- 1. 如何在手机开机时在后台永久运行Android应用程序?
- 2. 如何让流星手机应用继续在后台运行?
- 3. 我如何让我的Android应用程序在后台运行
- 4. 如何让应用程序在Android的后台运行?
- 5. 让应用程序在Android上的后台运行
- 6. 如何让程序在后台运行?
- 7. 如何让程序在后台运行
- 8. 如何在手机上运行简单的android应用程序?
- 9. 如何在手机在Android手机中响铃?
- 10. 暂停应用程序,如果手机正在响铃
- 11. Android应用程序在后台运行
- 12. Android,在后台运行应用程序
- 13. Android在后台运行应用程序
- 14. 在后台运行android应用程序?
- 15. 在后台运行应用程序android
- 16. 在后台运行的Windows手机应用程序
- 17. 如何让屏幕睡眠时在后台运行android应用程序?
- 18. 在后台运行时响应的iPhone应用程序
- 19. 让Android应用程序在后台运行
- 20. 让Android应用程序在后台运行
- 21. 让Android应用程序在后台运行
- 22. 如何让应用程序在后台运行?
- 23. 如何让应用程序在后台运行?
- 24. 如何让应用程序无限期地在后台运行?
- 25. 如何让我的应用程序在后台运行?
- 26. 如何让我的应用程序在后台运行
- 27. 如何让数字应用程序在后台运行
- 28. 如何让SVG在手机上运行?
- 29. 如何在Android中在后台运行我的应用程序?
- 30. iOS Flash和应用程序在后台运行时响起
+1停止我的GPS过 – rpax
名单phonestate在接收时,打电话给你上暂停活动()或做任何你需要 – Saqib
saqib - 你能解释一下这种方法 – Namit