Android-使用AIDL掛斷電話

1.AIDL簡單操做java

package org.zrf.demo;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

import com.android.internal.telephony.ITelephony;

public class PhoneService extends Service {
    private TelephonyManager telephony = null;                     // 電話管理器
    private AudioManager audio = null ;                            // 音頻管理
    private String phoneNumber = null ;                            // 要過濾的電話
    private IBinder myBinder = new BinderImpl() ;                // 定義IBinder
    class BinderImpl extends Binder implements IService {
        @Override
        public String getInterfaceDescriptor() {                // 取得接口描述信息
            return "過濾電話「" + PhoneService.this.phoneNumber
                    + "」設置成功!";                                 // 返回Service類的名稱
        }
    }
    @Override
    public IBinder onBind(Intent intent) {
        this.phoneNumber = intent.getStringExtra("phonenumber"); // 取得電話號碼
        this.telephony = (TelephonyManager) super
                .getSystemService(Context.TELEPHONY_SERVICE);     // 取得服務
        this.audio = (AudioManager) super
                .getSystemService(Context.AUDIO_SERVICE);        // 取得服務
        this.telephony.listen(new PhoneStateListenerImpl(),
                PhoneStateListener.LISTEN_CALL_STATE);             // 設置電話監聽
        return this.myBinder;                                    // 返回IBinder對象
    }

    private class PhoneStateListenerImpl extends PhoneStateListener {    // 電話監聽
        @Override
        public void onCallStateChanged(int state, String incomingNumber) { // 改變呼叫狀態
            switch (state) {                                        // 判斷狀態
            case TelephonyManager.CALL_STATE_IDLE:                     // 沒有撥入或撥出
                PhoneService.this.audio
                        .setRingerMode(AudioManager.RINGER_MODE_NORMAL);// 正常響鈴
                break;
            case TelephonyManager.CALL_STATE_RINGING:                // 有電話進入
                if (incomingNumber.equals(PhoneService.this.phoneNumber)) {    // 爲過濾號碼
                    ITelephony iTelephony=getITelephony();
                    if(iTelephony!=null){
                        try {
                            iTelephony.endCall();//掛斷電話
                        } catch (RemoteException e) {
                            e.printStackTrace();
                        }
                    }
                }
                break;
            }
        }
    }
 private ITelephony getITelephony(){
     ITelephony iTelephony=null;
     Class<TelephonyManager>cls=TelephonyManager.class;
     Method getITelephonyMethod=null;
     try {
        getITelephonyMethod=cls.getDeclaredMethod("getITelephony");
        getITelephonyMethod.setAccessible(true);//取消封裝
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }
     try {
         iTelephony=(ITelephony)getITelephonyMethod.invoke(this.telephony);
         return iTelephony;
        }  catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
     return iTelephony;
 }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

package com.android.internal.telephony;
interface ITelephony{
    boolean endCall();
    void answerRingingCall();
}

 

 

 

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="org.zrf.demo"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="10" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MyPhoneDemo"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
     <service  android:name=".PhoneService"/>
     <receiver android:name=".PhoneBroadcastReceiver">
       <intent-filter>
          <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
          <action  android:name="android.intent.action.BOOT_COMPLETED"/>
          <action  android:name="android.intent.action.PHONE_STATE"/>
       </intent-filter>
     </receiver>
    </application>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.SEND_SMS"/>
    <uses-permission android:name="android.permission.CALL_PHONE"/>
</manifest>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

相關文章
相關標籤/搜索