在来电和恢复通话结束后处理Android应用程序暂停 [英] Handling Android application pause on incoming call and resume after call end
本文介绍了在来电和恢复通话结束后处理Android应用程序暂停的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要暂停我的Android应用程序,当手机收到来电。通话结束后,我想我的应用程序自动恢复。
I want to pause my android application when the phone receives an incoming call. After the call ends, I want my applications to resume automatically.
如何在Android应用程序本实施?
How would this be implemented in an Android application?
推荐答案
您必须执行的监听器PhoneState。我在一个私有类这样做:
you have to implement a Listener for the PhoneState. I did this in a private Class:
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;
}
}
}
}
和你需要的权限添加到清单,文件
and you need to add the permission to the Manifest-File
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
这篇关于在来电和恢复通话结束后处理Android应用程序暂停的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文