我想通过点击按钮停止并启动广播接收器。与广播接收器相关的两个服务也应该停止并按下按钮开始我该怎么做..
答案 0 :(得分:11)
这是代码............
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();
}
});
答案 1 :(得分:2)
您可以选择“停止”一个BroadcastReceiver,例如按钮点击,或者可能选择 onPause()。< / p>
例如:
// 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);
然后“停止”它,您所要做的就是在onPause()
或点击Button
时调用此方法:
// "STOP" THE BROADCASTRECEIVER
unregisterReceiver(receiver);
// STOP THE SERVICE
stopService(intentMyService);
答案 2 :(得分: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);
}
}
答案 3 :(得分:1)
点击按钮此代码,它将通过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);
}
}
然后创建服务以启动......... 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;
}
}
答案 4 :(得分: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()根据您的需求调用