回答
你必须为PhoneState实现一个监听器。我这样做在一个私有类:
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;
}
}
}
}
,你需要允许添加到清单,文件
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
你好krains,我想检查我可能重复当我的活动正在运行时的呼叫状态此时,我希望我的应用程序在后台运行并保持暂停状态,而不是在呼叫结束后,我的应用程序会在该级别暂停时自动恢复。 – 2012-03-31 11:44:07
我不确定它是否有效,但您可能会在这里查看[android dev page](http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_SINGLE_TOP),并只添加FLAG_ACTIVITY_SINGLE_TOP标志意图。 – krains 2012-03-31 11:56:23
private class EndCallListener extends PhoneStateListener {
private boolean active = false;
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if(TelephonyManager.CALL_STATE_RINGING == state) {
Log.i("EndCallListener", "RINGING, number: " + incomingNumber);
}
if(TelephonyManager.CALL_STATE_OFFHOOK == state) {
//wait for phone to go offhook (probably set a boolean flag) so you know your app initiated the call.
active = true;
Log.i("EndCallListener", "OFFHOOK");
}
if(TelephonyManager.CALL_STATE_IDLE == state) {
//when this state occurs, and your flag is set, restart your app
Log.i("EndCallListener", "IDLE");
if (active) {
active = false;
// stop listening
TelephonyManager mTM = (TelephonyManager) m_activity.getSystemService(Context.TELEPHONY_SERVICE);
mTM.listen(this, PhoneStateListener.LISTEN_NONE);
// restart the inbox activity
//Intent intent = new Intent(m_activity, MDInboxActivity.class);
//m_activity.startActivity(intent);
}
}
}
}
而且你可以通过调用下面的代码初始化上述类:
try {
EndCallListener callListener = new EndCallListener();
TelephonyManager mTM = (TelephonyManager) m_activity.getSystemService(Context.TELEPHONY_SERVICE);
mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);
} catch(Exception e) {
Log.e("callMonitor", "Exception: "+e.toString());
}
而不是重新启动收件箱活动,您可以启动您自己的活动。 – Ishu 2012-03-31 11:10:58
- 1. 如何在收到来电时暂停服务,并在通话结束后恢复服务?
- 2. Android应用暂停恢复
- 3. 应用程序暂停/恢复状态
- 4. 停电后恢复骆驼处理
- 5. 如何暂停并恢复在iPhone应用程序下载?
- 6. 恢复和暂停基于处理程序的计时器
- 7. 如何暂停/恢复处理程序/可运行?
- 8. 如何在打电话后恢复iPhone应用程序
- 9. 暂停和恢复AsyncTasks? (Android)
- 10. Android暂停/恢复服务
- 11. 暂停/恢复使用WebDAV在Android
- 12. 致电后恢复应用程序
- 13. Android暂停和恢复教程
- 14. 的Android,暂停和恢复处理回调
- 15. 暂停/恢复线程?
- 16. 暂停/恢复Java小程序
- 17. iPhone SDK:在通话结束后启动应用程序
- 18. 如何在通话结束后启动应用程序?
- 19. 在iOS上暂停后恢复Spotify
- 20. 想要暂停排序程序,然后再恢复
- 21. Android:在开放通知推送后恢复应用程序
- 22. 从iPhone应用程序中暂停/恢复Pandora
- 23. 如何将暂停/恢复功能添加到应用程序?
- 24. Android:暂停并恢复活动中的线程
- 25. 在Android中暂停应用程序
- 26. 暂停在故障处理程序
- 27. Android:恢复应用程序
- 28. 通话结束后如何恢复我的游戏
- 29. 如何在电话和应用程序在后台处理asynctask android
- 30. 从iPhone应用程序拨打电话编程和结束通话
的[这](http://stackoverflow.com/a/5949116/1250370) – Deepak 2012-03-31 10:56:28