日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android 自动挂断,[转]android 来电自动接听和自动挂断

發(fā)布時間:2023/12/10 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 自动挂断,[转]android 来电自动接听和自动挂断 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

注意:android2.3版本不支持下面的自動接聽方法。(會拋異常:java.lang.SecurityException:

Neither user xxxxx nor current process has

android.permission.MODIFY_PHONE_STATE.)

第一步:準(zhǔn)備應(yīng)用環(huán)境需要的系統(tǒng)包和aidl文件。

(1)在應(yīng)用中創(chuàng)建包:android.telephony

將android系統(tǒng)框架下的\framework\telephony\java\android\telephony目錄中的NeighboringCellInfo.aidl文件復(fù)制到上面創(chuàng)建的包(android.telephony

)中;

(2)在應(yīng)用中創(chuàng)建包:com.android.internal.telephony

將android系統(tǒng)框架下的\framework\telephony\java\com\android\internal\telephony目錄中的ITelephony.aidl文件復(fù)制到上面創(chuàng)建的包(com.android.internal.telephony

)中;

第二步:創(chuàng)建一個獲取ITelephony的方法

PhoneUtils.java

Java代碼 ?

packagecom.zhouzijing.android.demo;

importjava.lang.reflect.Method;

importcom.android.internal.telephony.ITelephony;

importandroid.telephony.TelephonyManager;

publicclassPhoneUtils?{

publicstaticITelephony?getITelephony(TelephonyManager?telephony)throwsException?{

Method?getITelephonyMethod?=?telephony.getClass().getDeclaredMethod("getITelephony");

getITelephonyMethod.setAccessible(true);//私有化函數(shù)也能使用

return(ITelephony)getITelephonyMethod.invoke(telephony);

}

}

package com.zhouzijing.android.demo;

import java.lang.reflect.Method;

import com.android.internal.telephony.ITelephony;

import android.telephony.TelephonyManager;

public class PhoneUtils {

public static ITelephony getITelephony(TelephonyManager telephony) throws Exception {

Method getITelephonyMethod = telephony.getClass().getDeclaredMethod("getITelephony");

getITelephonyMethod.setAccessible(true);//私有化函數(shù)也能使用

return (ITelephony)getITelephonyMethod.invoke(telephony);

}

}

第三步:創(chuàng)建電話廣播攔截器

MyPhoneBroadcastReceiver.java

Java代碼 ?

packagecom.zhouzijing.android.demo;

importcom.android.internal.telephony.ITelephony;

importandroid.content.BroadcastReceiver;

importandroid.content.Context;

importandroid.content.Intent;

importandroid.telephony.TelephonyManager;

importandroid.util.Log;

publicclassMyPhoneBroadcastReceiverextendsBroadcastReceiver?{

privatefinalstaticString?TAG?=?MyPhone.TAG;

@Override

publicvoidonReceive(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);

}

}

publicvoiddoReceivePhone(Context?context,?Intent?intent)?{

String?phoneNumber?=?intent.getStringExtra(

TelephonyManager.EXTRA_INCOMING_NUMBER);

TelephonyManager?telephony?=?(TelephonyManager)context.getSystemService(

Context.TELEPHONY_SERVICE);

intstate?=?telephony.getCallState();

switch(state){

caseTelephonyManager.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;

caseTelephonyManager.CALL_STATE_IDLE:

Log.i(TAG,"[Broadcast]電話掛斷="+phoneNumber);

break;

caseTelephonyManager.CALL_STATE_OFFHOOK:

Log.i(TAG,"[Broadcast]通話中="+phoneNumber);

break;

}

}

}

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 MyPhoneBroadcastReceiverextends 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);

}

}

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

Java代碼 ?

packagecom.zhouzijing.android.demo;

importandroid.app.Activity;

importandroid.content.IntentFilter;

importandroid.os.Bundle;

importandroid.telephony.TelephonyManager;

importandroid.util.Log;

importandroid.view.View;

publicclassMyPhoneextendsActivity?{

publicfinalstaticString?TAG?="MyPhone";

publicfinalstaticString?B_PHONE_STATE?=?TelephonyManager.ACTION_PHONE_STATE_CHANGED;

privateMyPhoneBroadcastReceivermBroadcastReceiver;

@Override

publicvoidonCreate(Bundle?savedInstanceState)?{

super.onCreate(savedInstanceState);

setContentView(R.layout.my_phone);

}

//按鈕1-注冊廣播

publicvoidregisterThis(View?v)?{

Log.i(TAG,"registerThis");

mBroadcastReceiver?=newMyPhoneBroadcastReceiver();

IntentFilter?intentFilter?=newIntentFilter();

intentFilter.addAction(B_PHONE_STATE);

intentFilter.setPriority(Integer.MAX_VALUE);

registerReceiver(mBroadcastReceiver,?intentFilter);

}

//按鈕2-撤銷廣播

publicvoidunregisterThis(View?v)?{

Log.i(TAG,"unregisterThis");

unregisterReceiver(mBroadcastReceiver);

}

}

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 MyPhoneBroadcastReceivermBroadcastReceiver;

@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配置權(quán)限

Xml代碼 ?

其中:

Java代碼 ?

iTelephony.answerRingingCall();//自動接通電話

iTelephony.answerRingingCall();//自動接通電話

必須有權(quán)限

android.permission.MODIFY_PHONE_STATE

Java代碼 ?

iTelephony.endCall();//自動掛斷電話

iTelephony.endCall();//自動掛斷電話

必須有權(quán)限 android.permission.CALL_PHONE

總結(jié)

以上是生活随笔為你收集整理的android 自动挂断,[转]android 来电自动接听和自动挂断的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。