Android(AIDL)自动重复拨号及挂断/接听电话

Android默认没有提供挂断/接听电话的api,需要伪装com/android/internal/telephony/ITelephony.aidl的接口来欺骗系统。而自动重复拨号可以通过(BroadcastReceiver)监听电话状态(android.intent.action.PHONE_STATE)来实现。

1、Android挂断和接听电话的接口

public static void endCall(Context cx) { //挂断电话
    TelephonyManager telMag = (TelephonyManager) cx
            .getSystemService(Context.TELEPHONY_SERVICE);
    Class<TelephonyManager> c = TelephonyManager.class;
    Method mthEndCall = null;
    try {
        mthEndCall = c.getDeclaredMethod("getITelephony"(Class[]) null);
        mthEndCall.setAccessible(true);
        ITelephony iTel = (ITelephony) mthEndCall.invoke(telMag,
                (Object[]) null);
        iTel.endCall();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private void answerCall(Context cx) { //接听电话
    TelephonyManager telMag = (TelephonyManager) cx
            .getSystemService(Context.TELEPHONY_SERVICE);
    Class<TelephonyManager> c = TelephonyManager.class;
    Method mthEndCall = null;
    try {
        mthEndCall = c.getDeclaredMethod("getITelephony"(Class[]) null);
        mthEndCall.setAccessible(true);
        ITelephony iTel = (ITelephony) mthEndCall.invoke(telMag,
                (Object[]) null);
        iTel.answerRingingCall();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

com/android/internal/telephony/ITelephony.aidl文件

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

<strong>2、AndroidManifest.xml相关权限</strong>
<code lang='xml' width='auto' height='auto'>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

3、Android监听电话状态实现自动拨号

package com.orgcent.ticketcall;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.telephony.TelephonyManager;

public class PhoneStateReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
            if(null != BaseAppliction.phone_num) {
                BaseAppliction app = (BaseAppliction) context.getApplicationContext();
                app.showCtrlWin();
            }
        } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
            TelephonyManager tm = (TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE);
            switch (tm.getCallState()) {
            case TelephonyManager.CALL_STATE_RINGING:
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                break;
            case TelephonyManager.CALL_STATE_IDLE:
                if(null != BaseAppliction.phone_num) {
                    Intent it = new Intent(Intent.ACTION_CALL,Uri.parse("tel:" + BaseAppliction.phone_num));
                    it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(it);
                    BaseAppliction.mCallCount ++;
                    BaseAppliction app = (BaseAppliction) context.getApplicationContext();
                    app.updateState();
                }
                break;
            }
        }
    }
}

AndroidManifest.xml相关配置:

<receiver android:name="PhoneStateReceiver" >
      <intent-filter >
            <action android:name="android.intent.action.PHONE_STATE" />
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
       </intent-filter>
</receiver>

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值