我想通过单击按钮来停止和启动广播接收器。与广播接收器关联的两个服务也应该停止并开始按钮单击我该怎么做..
问问题
13705 次
5 回答
13
这是代码…………
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();
}
});
于 2013-01-18T04:52:42.700 回答
2
您可以选择“停止”广播接收器,比如单击按钮,或者可能在onPause()中。
例如:
// DECLARED GLOBALLY
BroadcastReceiver receiver;
Intent intentMyService;
ComponentName service;
并且在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);
然后要“停止”它,您所要做的就是在 aonPause()
的单击或单击时调用此方法Button
:
// "STOP" THE BROADCASTRECEIVER
unregisterReceiver(receiver);
// STOP THE SERVICE
stopService(intentMyService);
于 2013-01-18T04:23:35.553 回答
2
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()
{
// 在您的按钮单击事件中添加以下行
unregisterReceiver(mybroadcast);
}
}
于 2013-01-18T04:52:47.197 回答
1
将此代码放在按钮单击上,它将通过广播接收器启动一个活动
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);
}
}
然后创建要启动的服务............作为:
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;
}
}
于 2013-01-18T04:48:37.180 回答
1
这是代码
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();
}
您可以在 onStart() 方法中调用 startBroadCastReceiver() 并根据您的要求调用 killBroadCastReceiver()
于 2017-04-11T06:36:02.207 回答