4

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

我试过这段代码但没有用..

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

4 回答 4

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>
于 2012-07-02T11:27:38.853 回答
1

您可以在onCreate方法和onDestroy方法中注册您的接收器的示例代码,您可以取消注册它,

    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
}
于 2012-07-02T07:14:01.383 回答
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);
}
于 2018-04-07T23:06:06.950 回答
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**
于 2012-07-02T07:16:01.043 回答