回答
你必須爲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