【问题标题】:How to call onReceive() to reject calls?如何调用 onReceive() 拒绝来电?
【发布时间】:2014-10-02 15:24:37
【问题描述】:

我已经设法编写代码来阻止来电,但它在不同的类中,我想在用户在对话框上按“是”时执行它。如何调用 onReceive()?我作为参数传递的意图是什么?

这是 MainActivity 的代码 -

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    activateButton = (Button)findViewById(R.id.activate);
    activateButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

                DialogBox();
        }
    });

}

protected void DialogBox() {
    box = new AlertDialog.Builder(this);
    box.setTitle("Reject incoming calls?").
            setMessage("On activation, your phone will reject all incoming calls").setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    //BLOCK CALLS

                }
            }).setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
        }
    });
    final AlertDialog alert = box.create();
    alert.show();

}

这是扩展 BroadcastReceiver 的类。 onReceive 保存拒绝呼叫的代码。

public class RejectCall extends BroadcastReceiver {

public void onReceive(Context context, Intent intent) {
    ITelephony telephonyService;
    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    try {
        Class c = Class.forName(tm.getClass().getName());
        Method m = c.getDeclaredMethod("getITelephony");
        m.setAccessible(true);
        telephonyService = (ITelephony) m.invoke(tm);
        Bundle bundle = intent.getExtras();
        String phoneNumber = bundle.getString("incoming_number");
        Log.d("INCOMING", phoneNumber);
        if ((phoneNumber != null)) {
            telephonyService.endCall();
            Log.d("HANG UP", phoneNumber);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

另外,要执行这样的操作,我必须在 AndroidManifest 中声明哪些权限?

注意 - 我遇到过类似的问题,但似乎没有一个是在调用 onReceive(),因此是这个问题。

谢谢!

【问题讨论】:

    标签: java android broadcastreceiver android-manifest


    【解决方案1】:

    您必须在 android 清单文件中注册您的广播接收器,如下所示。

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

    另外,您必须授予以下权限

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

    将以上 2 段代码放在你的 android manifest 中应该会触发拒绝呼叫接收者的onRecieve()

    【讨论】:

    • 谢谢,这回答了我的部分问题。但是,如果您看到我的 MainActivity 类,我会打开一个对话框,并且我只想在用户在对话框中按“是”时阻止调用。我该怎么做?我不知何故需要在用户按下是时缝合 onRecieve()。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多