How to disable the broadcast Receiver to stop receiving the incoming calls?

孤街醉人 提交于 2019-12-13 16:25:52

问题


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 ?


回答1:


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();


            }
        }
    });


来源:https://stackoverflow.com/questions/44691894/how-to-disable-the-broadcast-receiver-to-stop-receiving-the-incoming-calls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!