android電話接通
1. Android程序接聽及掛斷電話,適配所有Android版本
接聽來電
拒接來電:
其中文中的接聽電話android6-android7的處理方案是利用了監聽通知使用權,所以還要新建一個NotificationListenerService的子類,叫ListenerService,需要在AndroidManifests中配置
另外需要提示用戶打開通知使用權才可以正常使用
已測試通過了android6-android9各種機型,注意拒接來電在android9以上才有官方支持,所以需要設置compileSdkVersion=28。
需要的許可權為:Manifest.permission.CALL_PHONE,Manifest.permission.READ_PHONE_STATE
8.0以上需要Manifest.permission.ANSWER_PHONE_CALLS,Manifest.permission.READ_CALL_LOG。
還不行的請注意三點:
1:檢查許可權是否都申請及在manifests里注冊了
2:compileSdkVersion是否為28
3:android6到android7的手機的接聽來電要依賴通知使用權,所以通知使用權打開才可以正常工作。
2. 安卓手機哪個按鍵可以接電話
安卓系統手機接聽電話方法為:滑動"綠色電話"圖標接聽即可。
若您使用的是翻蓋手機,請翻蓋並按接聽按鍵進行通話。
3. Android怎麼實現自動接聽來電
android 實現來電自動接聽和自動掛斷的方法:
第一步:准備應用環境需要的系統包和aidl文件。
(1)在應用中創建包:android.telephony
將android系統框架下的\framework\telephony\java\android\telephony目錄中的NeighboringCellInfo.aidl文件復制到上面創建的包(android.telephony )中;
(2)在應用中創建包:com.android.internal.telephony
將android系統框架下的\framework\telephony\java\com\android\internal\telephony目錄中的ITelephony.aidl文件復制到上面創建的包(com.android.internal.telephony )中。
第二步:創建一個獲取ITelephony的方法
PhoneUtils.java
package com.zhouzijing.android.demo;
import java.lang.reflect.Method;
import com.android.internal.telephony.ITelephony;
import android.telephony.TelephonyManager;
public class PhoneUtils {
/**
* 根據傳入的TelephonyManager來取得系統的ITelephony實例.
* @param telephony
* @return 系統的ITelephony實例
* @throws Exception
*/
public static ITelephony getITelephony(TelephonyManager telephony) throws Exception {
Method getITelephonyMethod = telephony.getClass().getDeclaredMethod("getITelephony");
getITelephonyMethod.setAccessible(true);//私有化函數也能使用
return (ITelephony)getITelephonyMethod.invoke(telephony);
}
}
第三步:創建電話廣播攔截器
MyPhoneBroadcastReceiver.java
package com.zhouzijing.android.demo;
import com.android.internal.telephony.ITelephony;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;
public class MyPhoneBroadcastReceiver extends BroadcastReceiver {
private final static String TAG = MyPhone.TAG;
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i(TAG, "[Broadcast]"+action);
//呼入電話
if(action.equals(MyPhone.B_PHONE_STATE)){
Log.i(TAG, "[Broadcast]PHONE_STATE");
doReceivePhone(context,intent);
}
}
/**
* 處理電話廣播.
* @param context
* @param intent
*/
public void doReceivePhone(Context context, Intent intent) {
String phoneNumber = intent.getStringExtra(
TelephonyManager.EXTRA_INCOMING_NUMBER);
TelephonyManager telephony = (TelephonyManager)context.getSystemService(
Context.TELEPHONY_SERVICE);
int state = telephony.getCallState();
switch(state){
case TelephonyManager.CALL_STATE_RINGING:
Log.i(TAG, "[Broadcast]等待接電話="+phoneNumber);
try {
ITelephony iTelephony = PhoneUtils.getITelephony(telephony);
iTelephony.answerRingingCall();//自動接通電話
//iTelephony.endCall();//自動掛斷電話
} catch (Exception e) {
Log.e(TAG, "[Broadcast]Exception="+e.getMessage(), e);
}
break;
case TelephonyManager.CALL_STATE_IDLE:
Log.i(TAG, "[Broadcast]電話掛斷="+phoneNumber);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.i(TAG, "[Broadcast]通話中="+phoneNumber);
break;
}
}
}
第四部:注冊電話廣播攔截器
MyPhone.java
package com.zhouzijing.android.demo;
import android.app.Activity;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
public class MyPhone extends Activity {
public final static String TAG = "MyPhone";
public final static String B_PHONE_STATE = TelephonyManager.ACTION_PHONE_STATE_CHANGED;
private MyPhoneBroadcastReceiver mBroadcastReceiver;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_phone);
}
//按鈕1-注冊廣播
public void registerThis(View v) {
Log.i(TAG, "registerThis");
mBroadcastReceiver = new MyPhoneBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(B_PHONE_STATE);
intentFilter.setPriority(Integer.MAX_VALUE);
registerReceiver(mBroadcastReceiver, intentFilter);
}
//按鈕2-撤銷廣播
public void unregisterThis(View v) {
Log.i(TAG, "unregisterThis");
unregisterReceiver(mBroadcastReceiver);
}
}
第5步:在AndroidManifest.xml配置許可權
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
其中:
iTelephony.answerRingingCall();//自動接通電話
必須有許可權 android.permission.MODIFY_PHONE_STATE
iTelephony.endCall();//自動掛斷電話
必須有許可權 android.permission.CALL_PHONE。
4. android 9.0 獲取去電時對方接通的真實狀態(監聽通話過程狀態)
場景:app是完全接管了系統的撥號及通話頁面,因此很多狀態無法獲取,就比如去電時判斷對方是否真實接通還是處於對方振鈴狀態。
在應用層監聽通話狀態只有三種,從TelephonyManager.java中注釋可知這三種狀態含義如下:
CALL_STATE_IDLE :空閑態(沒有通話活動)
CALL_STATE_RINGING :包括響鈴、第三方來電等待
CALL_STATE_OFFHOOK 摘機(接聽):包括dialing撥號中、active接通、hold掛起等
監聽通話狀態:
參考文章:
https://blog.csdn.net/qq_32115439/article/details/78395537