I want to stop and start a broadcast receiver through a button click. two services associated with the broadcast receiver should also stop and start with button click how can i do it..
this is the code............
b1.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
    // TODO Auto-generated method stub
        PackageManager pm  = Re_editActivity.this.getPackageManager();
        ComponentName componentName = new ComponentName(currentActivity.this, name_of_your_receiver.class);
        pm.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                        PackageManager.DONT_KILL_APP);
        Toast.makeText(getApplicationContext(), "activated", Toast.LENGTH_LONG).show();
        }
    });
  b2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        PackageManager pm  = Re_editActivity.this.getPackageManager();
        ComponentName componentName = new ComponentName(currentActivity.this, name_of_your_receiver.class);
        pm.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                        PackageManager.DONT_KILL_APP);
        Toast.makeText(getApplicationContext(), "cancelled", Toast.LENGTH_LONG).show();
    }
});
                        You can choose to "stop" a BroadcastReceiver either on, say a Button click, or perhaps in the onPause().
For example:
// DECLARED GLOBALLY
BroadcastReceiver receiver;
Intent intentMyService;
ComponentName service;
And in the onCreate():
// FOR THE SERVICE:
intentMyService = new Intent(this, MyGpsService.class);
service = startService(intentMyService);
// FOR THE BROADCASTRECEIVER:
IntentFilter mainFilter = new IntentFilter();
receiver = new MyMainLocalReceiver();
registerReceiver(receiver, mainFilter);
Then to "stop" it, all you have to do, is make a call to this method in either the onPause() or on the click of a Button:
// "STOP" THE BROADCASTRECEIVER
unregisterReceiver(receiver);
// STOP THE SERVICE
stopService(intentMyService);
                        public class MyActivity extends Activity
{
  private final BroadcastReceiver mybroadcast = new SmsBR();
  public void onResume()
  {
    IntentFilter filter = new IntentFilter();
    filter.addAction("android.provider.Telephony.SMS_RECEIVED");
    registerReceiver(mybroadcast, filter);  
  }
  public void onPause()
  {
// add the below line in your button click event
    unregisterReceiver(mybroadcast);
  }
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With