【问题标题】:How to reject any incoming call when I have already detected it当我已经检测到任何来电时如何拒绝它
【发布时间】:2015-04-11 16:06:03
【问题描述】:

应用程序检测来电并在来电时显示祝酒词。内部类CallStateListener负责检测调用:

    private class CallStateListener extends PhoneStateListener {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            switch (state) {
            case TelephonyManager.CALL_STATE_RINGING:
                // called when someone is ringing to this phone

                Toast.makeText(ctx, "Incoming: " + incomingNumber, Toast.LENGTH_LONG).show();           
                break;
            }
        }
    }

但是现在在显示 Toast 之后我想取消/拒绝这个来电(任何)。 如何取消这个来电?

整个类看起来像这样:

package com.example.a;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;


public class CallHelper {

    /**
     * Listener to detect incoming calls.
     */
    private class CallStateListener extends PhoneStateListener {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            switch (state) {
            case TelephonyManager.CALL_STATE_RINGING:
                // called when someone is ringing to this phone

                Toast.makeText(ctx, "Incoming: " + incomingNumber, Toast.LENGTH_LONG).show();           
                break;
            }
        }
    }

    /**
     * Broadcast receiver to detect the outgoing calls.
     */
    public class OutgoingReceiver extends BroadcastReceiver {
        public OutgoingReceiver() {
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);

            Toast.makeText(ctx, "Outgoing: " + number, Toast.LENGTH_LONG).show();
        }

    }

    private Context ctx;
    private TelephonyManager tm;
    private CallStateListener callStateListener;

    private OutgoingReceiver outgoingReceiver;

    public CallHelper(Context ctx) {
        this.ctx = ctx;

        callStateListener = new CallStateListener();
        outgoingReceiver = new OutgoingReceiver();
    }

    /**
     * Start calls detection.
     */
    public void start() {
        tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
        tm.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);

        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_NEW_OUTGOING_CALL);
        ctx.registerReceiver(outgoingReceiver, intentFilter);
    }

    /**
     * Stop calls detection.
     */
/*  public void stop() {
        tm.listen(callStateListener, PhoneStateListener.LISTEN_NONE);
        ctx.unregisterReceiver(outgoingReceiver);
    }*/

}

编辑:

EDIT2 也许我没有权限:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.a"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="17"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            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=".CallDetectService"
            android:enabled="true"
            android:exported="false" >
        </service>
    </application>



</manifest>

更新课程

包 com.example.a;

import java.lang.reflect.Method;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;


public class CallHelper {

    /**
     * Listener to detect incoming calls.
     */
    private class CallStateListener extends PhoneStateListener {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            switch (state) {
            case TelephonyManager.CALL_STATE_RINGING:
                // called when someone is ringing to this phone
                Toast.makeText(ctx, "Incoming: " + incomingNumber, Toast.LENGTH_LONG).show();
                 try{
                        TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
                        Class c = Class.forName(tm.getClass().getName());
                        Method m = c.getDeclaredMethod("getITelephony");
                        com.android.internal.telephony.ITelephony telephonyService = (com.android.internal.telephony.ITelephony) m.invoke(tm);  

                        telephonyService = (com.android.internal.telephony.ITelephony) m.invoke(tm);
                        telephonyService.silenceRinger();
                        telephonyService.endCall();
                    }catch (Exception e) {
                        e.printStackTrace();

                    }
                break;
            }
        }
    }

    /**
     * Broadcast receiver to detect the outgoing calls.
     */
    public class OutgoingReceiver extends BroadcastReceiver {
        public OutgoingReceiver() {
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);

            Toast.makeText(ctx, "Outgoing: " + number, Toast.LENGTH_LONG).show();
        }

    }

    private Context ctx;
    private TelephonyManager tm;
    private CallStateListener callStateListener;

    private OutgoingReceiver outgoingReceiver;

    public CallHelper(Context ctx) {
        this.ctx = ctx;

        callStateListener = new CallStateListener();
        outgoingReceiver = new OutgoingReceiver();
    }

    /**
     * Start calls detection.
     */
    public void start() {
        tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
        tm.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);

        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_NEW_OUTGOING_CALL);
        ctx.registerReceiver(outgoingReceiver, intentFilter);
    }

    /**
     * Stop calls detection.
     */
    /*
     * public void stop() { tm.listen(callStateListener,
     * PhoneStateListener.LISTEN_NONE);
     * ctx.unregisterReceiver(outgoingReceiver); }
     */

}

编辑 3 项目链接:http://www.speedyshare.com/ccY6T/A1.zip

【问题讨论】:

    标签: android


    【解决方案1】:

    导入这些...

      import java.lang.reflect.Method;
      import android.app.Activity;  
      import android.telephony.TelephonyManager;
      import com.android.internal.telephony.*;
    

    使用此代码拒接电话。

     try{
        TelephonyManager tm = (TelephonyManager)        context.getSystemService(Context.TELEPHONY_SERVICE);
        Class c = Class.forName(tm.getClass().getName());
        Method m = c.getDeclaredMethod("getITelephony");
        com.android.internal.telephony.ITelephony telephonyService = (ITelephony) m.invoke(tm);  
        telephonyService = (ITelephony) m.invoke(tm);
        telephonyService.silenceRinger();
        telephonyService.endCall();
    }catch (Exception e) {
        e.printStackTrace();
          // Some problem occurred while accessing private API
        // TODO: do whatever error handling you want here  
    
    }
    

    将这些权限添加到清单文件中。

    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    

    com.android.internal.telephony 不是 Android SDK 的一部分。 您可以从here 获取此 Java 文件。

    注意:

    这里我们使用 Java 反射。这些方法是存在的,但它们被标记为 “private”。使用反射,我们可以获得很多关于类的信息,甚至是那些被认为是“私有”的信息。由于这是一个“不受支持的 API”,它可能无法在所有设备上运行,并且可能会在未来的 Android 版本中进行更改

    【讨论】:

    • 谢谢。唯一的问题是我不知道telephonyService应该是什么类型。通过查看您的代码,它应该是ITelephony,因为您对其进行了转换,但是找不到此类/接口。
    • 我复制了更新的代码(请看 OP 中的编辑,我也编辑过)。问题是没有:com.android.internal.telephony.ITelephony。只有com.android.internal.util。
    • “导入 com.android.internal.telephony.*;无法解析”。我明白了。
    • 谢谢,现在没有错误了。我将更新的代码粘贴到 EDIT2 中。问题是电话不会拒绝来电。安卓版本为4.2
    • 不允许权限
    猜你喜欢
    • 1970-01-01
    • 2012-03-29
    • 2020-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多