如何禁用广播接收器以停止接收来电? [英] How to disable the broadcast Receiver to stop receiving the incoming calls?

查看:106
本文介绍了如何禁用广播接收器以停止接收来电?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正面临禁用广播接收器的问题。广播接收器接收呼入和呼出电话。
以我为例,当开关处于打开状态时,接收器应接收数据;当开关处于关闭状态时,接收器应停止接收数据。

I am facing the issue in disable the broadcast receiver. The broadcast receiver receives the incoming and outgoing calls . In my case when switch is in on the receiver should receive the data when the switch is in off the receiver should stop receiving the data.

     switches.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {

//                sharedPreferences = getApplicationContext().getSharedPreferences("enableApp", Context.MODE_PRIVATE);
//                SharedPreferences.Editor editor = sharedPreferences.edit();
//                editor.putBoolean(getString(R.string.enable), isChecked);
//                editor.commit();


                if(isChecked)
                {

                    Toast.makeText(getApplicationContext(), "Enabled", Toast.LENGTH_SHORT).show();
                }
                else
                {
                    PackageManager pm  = DashBoardActivity.this.getPackageManager();
                    ComponentName componentName = new ComponentName(DashBoardActivity.this, CallReceiver.class);
                    pm.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                            PackageManager.DONT_KILL_APP);
                    Toast.makeText(getApplicationContext(), "cancelled", Toast.LENGTH_LONG).show();
                    Toast.makeText(getApplicationContext(), "Disabled", Toast.LENGTH_SHORT).show();


                }
            }
        });

这是我尝试过的代码,我尝试使用程序包管理器禁用广播接收器。

This is my tried code , i tried to disable the broadcast receiver by using the package manager.

The broadcast receiver registered in manifestfile
     <receiver android:name=".receiver.CallReceiver">
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
            </intent-filter>
        </receiver>

我想在活动中禁用接收器。如何禁用接收器?

I want to disable the receiver in my activity.How to disable the receiver ?

推荐答案

尝试一下,下面的代码可以解决您的问题,

Try this, the below code could solve your problem,

 switches.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {

            sharedPreferences = getApplicationContext().getSharedPreferences("enableApp", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putBoolean(getString(R.string.enable), isChecked);
            editor.commit();


            if(isChecked)
            {
                PackageManager pm  = DashBoardActivity.this.getPackageManager();
                ComponentName componentName = new ComponentName(DashBoardActivity.this, CallReceiver.class);
                int status = getApplicationContext().getPackageManager().getComponentEnabledSetting(componentName);
                pm.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                        PackageManager.DONT_KILL_APP);
                Log.e("Broadcast status",status + "");
                Toast.makeText(getApplicationContext(), "Enabled", Toast.LENGTH_SHORT).show();


            }
            else
            {


                PackageManager pm  = DashBoardActivity.this.getPackageManager();
                ComponentName componentName = new ComponentName(DashBoardActivity.this, CallReceiver.class);
                int status = getApplicationContext().getPackageManager().getComponentEnabledSetting(componentName);
                pm.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                        PackageManager.DONT_KILL_APP);
                Log.e("Broadcast status",status + "");
                Toast.makeText(getApplicationContext(), "cancelled", Toast.LENGTH_LONG).show();
                Toast.makeText(getApplicationContext(), "Disabled", Toast.LENGTH_SHORT).show();


            }
        }
    });

这篇关于如何禁用广播接收器以停止接收来电?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆