按钮点击启用和禁用广播接收器

时间:2012-07-02 07:05:29

标签: android broadcastreceiver

我想在按钮点击时启用和禁用广播接收器以完成启动。接收器不应响应,直到按钮被单击一次。我确实看到了一些类似的问题,但没有答案有效..请帮助...

我尝试了这段代码,但没有用。

PackageManager pm  = getApplicationContext().getPackageManager();
ComponentName componentName = new ComponentName("mypackage", ".receivername");
pm.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);

4 个答案:

答案 0 :(得分:20)

试试这个

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();
        }
    });

这里button1用于激活广播接收器,button2用于停用它  您还需要在清单文件中注册广播接收器并设置

<receiver android:name="name_of_your_receiver" android:enabled="false">
        <intent-filter >                
            <action android:name="android.intent.action.BOOT_COMPLETED"/>                
        </intent-filter>
    </receiver>

答案 1 :(得分:1)

示例代码,您可以在 onCreate 方法中注册您的接收器, onDestroy 取消注册方法,< / p>

    private BroadcastReceiver receiver;

    @Overrride
    public void onCreate(Bundle savedInstanceState){

      // your oncreate code

      IntentFilter filter = new IntentFilter();
      filter.addAction("SOME_ACTION");
      filter.addAction("SOME_OTHER_ACTION");

      receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
          //do something based on the intent's action
        }
      }

      registerReceiver(receiver, filter);
}

请记住 onDestroy 方法中运行

@Override
protected void onDestroy() {
  unregisterReceiver(receiver); //unregisterReceiver can be anywhere based on your requirement
}

答案 2 :(得分:1)

我认为用这种方式更容易:

public static void setAppComponentEnabled(@NonNull final Context context, @NonNull final Class<?> componentClass, final boolean enable) {
    final PackageManager pm = context.getPackageManager();
    final int enableFlag = enable ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
    pm.setComponentEnabledSetting(new ComponentName(context, componentClass), enableFlag, PackageManager.DONT_KILL_APP);
}

答案 3 :(得分:0)

试试这个:

YourBroadcastReceiver mReceiver = new YourBroadcastReceiver();
IntentFilter filter = new IntentFilter();  //Filter for your receiver...
filter.addAction(your action goes here...);
registerReceiver(mReceiver , filter); **// Register**

unregisterReceiver(mReceiver );  **// Unregister**