Reputation: 1329
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 ?
Upvotes: 2
Views: 649
Reputation: 9084
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();
}
}
});
Upvotes: 2