我有一個要求,其中我想檢測兩種Android中檢測呼出和通話掛機事件的Android
- 每當呼出由與呼叫無關的事件,我的應用程序應該知道這與被叫號碼
- 當電話掛了(由於成功/失敗)一起,我的應用程序應該知道這與掛斷
的原因沿着這是可能的Android中?
我有一個要求,其中我想檢測兩種Android中檢測呼出和通話掛機事件的Android
的原因沿着這是可能的Android中?
你應該創建一個BroadcastReceiver:
public class CallReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_RINGING)) {
// Phone number
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
// Ringing state
// This code will execute when the phone has an incoming call
} else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_IDLE)
|| intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_OFFHOOK)) {
// This code will execute when the call is answered or disconnected
}
}
}
你應該註冊你一個pplication聽清單中的這些意圖:
<receiver android:name=".CallReciever" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
你需要創建一個接收器用於以下目的的行動:
你能不能給我一個例子,我對呼出 – 2012-12-24 11:47:01
需要上述解決方案完美地工作了me.But接收器接收相同的狀態多次。我們如何解決這個問題?無論如何檢測接收意圖的ID或其他方式? – 2015-10-09 11:47:48
有隻使用TelephonyManager和 PhoneStateListener.You鴕鳥政策甚至要註冊一個BroadcastReceiver一個簡單的解決方案。
public class MyPhoneStateListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
//Hangup
case TelephonyManager.CALL_STATE_IDLE:
break;
//Outgoing
case TelephonyManager.CALL_STATE_OFFHOOK:
break;
//Incoming
case TelephonyManager.CALL_STATE_RINGING:
break;
}
}
}
並註冊它:
public static void registerListener(Context context) {
((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).listen(new MyPhoneStateListener(),
PhoneStateListener.LISTEN_CALL_STATE);
}
這不適用於撥出電話。 – ralphgabb 2015-07-30 01:35:41
感謝您的舉報代碼 有沒有一種方法來捕獲傳出呼叫,以及類似於捕獲來電的方式? – user669231 2011-03-31 10:41:13
@ user669231是的,您應該更改if-state中的支票。你可以看到這個主題:http://stackoverflow.com/questions/2668445/detect-outgoing-calls-and-problem-with-a-real-device – vendor 2011-03-31 10:59:01
工程就像一個魅力。此外,您必須在manifest.xml文件中聲明以下權限: <使用權限android:name =「android.permission.READ_PHONE_STATE」/> 否則,這將不起作用。 見TelephonyManager類的API: http://developer.android.com/reference/android/telephony/TelephonyManager.html – qupera 2014-10-08 09:19:15