1. 程式人生 > >android 接聽和結束通話實現方式

android 接聽和結束通話實現方式

注意:android2.3版本及以上不支援下面的自動接聽方法。

(會拋異常:java.lang.SecurityException: Neither user xxxxx nor current process hasandroid.permission.MODIFY_PHONE_STATE.)

原因:android2.3版本及以上android.permission.MODIFY_PHONE_STATE許可權限制已經改為系統許可權。

 普通應用程式已經無法呼叫,所以網上找到的那些如何使用android.permission.MODIFY_PHONE_STATE的文章均已失效,但仍有引用的辦法就是讓你的程式程式系統程式。一種就是預製到ROM中,另一種就是使用系統簽名。第一種我已經試驗通過,第二種還有待驗證。

言歸正傳,先說下如何使用對映機制實現自動接聽和結束通話

第一步:準備應用環境需要的系統包和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
Java程式碼
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);//私有化函式也能使用
return (ITelephony)getITelephonyMethod.invoke(telephony);
}
}


第三步:建立電話廣播攔截器

MyPhoneBroadcastReceiver.java
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);
}
}


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程式碼
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配置許可權
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"/>
其中:
Java程式碼
iTelephony.answerRingingCall();//自動接通電話

必須有許可權 android.permission.MODIFY_PHONE_STATE


Java程式碼
iTelephony.endCall();//自動結束通話電話

必須有許可權 android.permission.CALL_PHONE

因為Android2.3以上增加了對permission android.permission.MODIFY_PHONE_STATE 的限制,2.3之前的通過反射機制呼叫ITelephone的能力的做法已經不適用。
2.3上實現方式:
public synchronized void answerRingingCall() {

查詢系統PhoneAPP應用(PhoneGlobals.java)實現了對耳機插入、多媒體按鍵等通知的接受和處理。其中未發現有特殊的地方,個人認為,如果系統接收到此廣播應該可以進行接聽或結束通話操作。


   
    private void answerRingingCallWithBroadcast(Context context,TelephonyManager telmanager){  
        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);  
        //判斷是否插上了耳機  
        if (! audioManager.isWiredHeadsetOn()) { 

       //4.1以上系統限制了部分許可權, 使用三星4.1版本測試提示警告:Permission Denial: not allowed to send broadcast android.intent.action.HEADSET_PLUG from pid=1324, uid=10017

//這裡需要注意一點,傳送廣播時加了許可權“android.permission.CALL_PRIVLEGED”,則接受該廣播時也需要增加該許可權。但是4.1以上版本貌似這個許可權只能系統應用才可以得到。測試的時候,自定義的接收器無法接受到此廣播,後來去掉了這個許可權,設為NULL便可以監聽到了。


        if(android.os.Build.VERSION.SDK_INT >=15 ){
                Intent meidaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);  
                KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK);  
                meidaButtonIntent.putExtra(Intent.EXTRA_KEY_EVENT,keyEvent);  
                context.sendOrderedBroadcast(meidaButtonIntent, null);  
        }else{

// 以下適用於Android2.3及2.3以上的版本上 ,但測試發現4.1系統上不管用。
        Intent localIntent1 = new Intent(Intent.ACTION_HEADSET_PLUG);  
                localIntent1.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);  
                localIntent1.putExtra("state", 1);  
                localIntent1.putExtra("microphone", 1);  
                localIntent1.putExtra("name", "Headset");  
                context.sendOrderedBroadcast(localIntent1,  "android.permission.CALL_PRIVILEGED");  
                
                Intent localIntent2 = new Intent(Intent.ACTION_MEDIA_BUTTON);  
                KeyEvent localKeyEvent1 = new KeyEvent(KeyEvent.ACTION_DOWN,  KeyEvent.KEYCODE_HEADSETHOOK);  
                localIntent2.putExtra(Intent.EXTRA_KEY_EVENT,   localKeyEvent1);  
                context. sendOrderedBroadcast(localIntent2,  "android.permission.CALL_PRIVILEGED"); 
                
                Intent localIntent3 = new Intent(Intent.ACTION_MEDIA_BUTTON);  
                KeyEvent localKeyEvent2 = new KeyEvent(KeyEvent.ACTION_UP,  KeyEvent.KEYCODE_HEADSETHOOK);  
                localIntent3.putExtra(Intent.EXTRA_KEY_EVENT,  localKeyEvent2);  
                context.sendOrderedBroadcast(localIntent3,  "android.permission.CALL_PRIVILEGED");  
                
                Intent localIntent4 = new Intent(Intent.ACTION_HEADSET_PLUG);  
                localIntent4.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);  
                localIntent4.putExtra("state", 0);  
                localIntent4.putExtra("microphone", 1);  
                localIntent4.putExtra("name", "Headset");  
                context.sendOrderedBroadcast(localIntent4, "android.permission.CALL_PRIVILEGED");
        }
              
        } else {  
            Intent meidaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);  
            KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK);  
            meidaButtonIntent.putExtra(Intent.EXTRA_KEY_EVENT,keyEvent);