问题
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..
回答1:
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();
}
});
回答2:
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);
回答3:
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);
}
}
回答4:
Put this code on button click ,it will start an activity through broadcastreceiver
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "MyReceiver Started", Toast.LENGTH_SHORT).show();
Intent myIntent=new Intent(context,MyService.class);
context.startService(myIntent);
}
}
and then create service to be start .........as:
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId){
Toast.makeText(getBaseContext(), "Service Started", Toast.LENGTH_SHORT).show();
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
}
回答5:
Here is the code
private void startBroadCastReceiver() {
PackageManager pm = this.getPackageManager();
ComponentName componentName = new ComponentName(this, ConnectionReceiver.class);
pm.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
Toast.makeText(getApplicationContext(), "BroadCast Receiver Started", Toast.LENGTH_LONG).show();
}
private void killBroadCastReceiver() {
PackageManager pm = this.getPackageManager();
ComponentName componentName = new ComponentName(this, ConnectionReceiver.class);
pm.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
Toast.makeText(getApplicationContext(), "BroadCast Receiver Killed", Toast.LENGTH_LONG).show();
}
You can call startBroadCastReceiver() in onStart() method and killBroadCastReceiver() Acc to your Requirement
来源:https://stackoverflow.com/questions/14392281/stop-broadcast-receiver-from-a-button-in-acivity