如何在android中手动停止BroadcastReceiver

时间:2012-02-22 05:40:44

标签: android broadcastreceiver

我在BroadcastReceiver中注册了我的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example" android:versionCode="1" android:versionName="1.0">
  <application android:icon="@drawable/icon" android:label="@string/app_name"
    android:theme="@android:style/Theme.Light">
    <activity android:name=".WiFiDemo" android:label="@string/app_name">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
    <receiver android:name="com.example.WiFiScanReceiver">
      <intent-filter>
        <action android:name="android.net.wifi.supplicant.CONNECTION_CHANGE" />
      </intent-filter>
    </receiver>
  </application>
  <uses-sdk android:minSdkVersion="3" />
  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
  <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
 <uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"></uses-permission>
 <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"> </uses-permission>
 <uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>

我想使用其他应用程序输入停止此BroadcastReceiver如何以编程方式停止此接收器?

这是我的接收者代码:

public class WIFIBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction()
                .equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
            Intent ssIntent = new Intent(context, com.Activity.class);
            ssIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(ssIntent);
        }
    }
}

答案:: 我添加了sharedprefernce复选框并保存两个字符串并在receive方法中进行验证  这是我的代码

第二次申请:

final CheckBoxPreference checkboxPref = (CheckBoxPreference) getPreferenceManager()
        .findPreference("checkboxPref");
// ...
checkboxPref
        .setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

            public boolean onPreferenceChange(Preference preference,
                    Object newValue) {
                if (newValue.toString().equals("true")) {
                    // String value = sp.getString(key, null);
                    sharedPreferences = getSharedPreferences(
                            PREFS_READ, Context.MODE_WORLD_READABLE);
                    Editor prefsPrivateEditor = sharedPreferences.edit();
                    prefsPrivateEditor.putString(KEY_READ1, "true");
                    prefsPrivateEditor.commit();
                    Toast.makeText(getApplicationContext(),
                            "CB: " + "true", Toast.LENGTH_SHORT).show();
                } else {
                    sharedPreferences = getSharedPreferences(
                            PREFS_READ, Context.MODE_WORLD_READABLE);
                    Editor prefsPrivateEditor = sharedPreferences.edit();
                    prefsPrivateEditor.putString(KEY_READ1, "false");
                    prefsPrivateEditor.commit();
                    Toast.makeText(getApplicationContext(),
                            "CB: " + "false", Toast.LENGTH_SHORT)
                            .show();
                }
                return true;
            }
        });

验证onReceive方法中第一个应用程序中的代码:

public class WIFIBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction()
                .equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
            sharedPreferences = otherAppsContext.getSharedPreferences(
                    PREFS_READ, Context.MODE_WORLD_WRITEABLE);
            String scaningAction = sharedPreferences.getString(KEY_READ1, null);
            if (scaningAction.equals("true")) {
                Intent ssIntent = new Intent(context, com.Activity.class);
                ssIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(ssIntent);
            }
        }
    }
}

这是我的需要。但我无法取消注册广播接收器。没有活动任何这个答案将有助于一些人。

4 个答案:

答案 0 :(得分:2)

您可以使用:

ComponentName receiver = new ComponentName(context, myReceiver.class);

PackageManager pm = context.getPackageManager();

pm.setComponentEnabledSetting(receiver,
        PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
        PackageManager.DONT_KILL_APP)

有关详细信息,请访问here

答案 1 :(得分:1)

像这样:

PackageManager pm = getPackageManager();
pm.setComponentEnabledSetting(WiFiScanReceiver,
                            PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                            PackageManager.DONT_KILL_APP);

答案 2 :(得分:0)

大多数情况下,您必须在onResume()方法中注册BroadCastReceiver,因为它是Activity的Running State。我们可以在活动的onPause() Stopped State内取消注册。您可以从Broadcast Receivers

了解更多相关信息

答案 3 :(得分:0)

我猜有偏好的代码是在某些偏好活动中。所以请保留原来的接收器并使用类似的东西:

public class YourSettingsActivity extends PreferenceActivity {

    OnPreferenceChangeListener onPreferenceChangeListener;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        onPreferenceChangeListener = new OnPreferenceChangeListener() {

            public boolean onPreferenceChange(Preference preference,
                    Object newValue) {
                if (newValue instanceof Boolean) {
                    final boolean enable = (Boolean) newValue;
                    PackageManager pacman = getPackageManager();
                    final ComponentName componentName = new ComponentName(
                            "com.example.yourapp1", WIFIBroadcastReceiver.class);
                    final int state = (enable) ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
                            : PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
                    pacman.setComponentEnabledSetting(componentName, state,
                            PackageManager.DONT_KILL_APP);
                    Toast.makeText(getApplicationContext(), "CB: " + enable,
                            Toast.LENGTH_SHORT).show();
                    return true;
                }
                return false;
            }
        };
        @SuppressWarnings("deprecation")
        final CheckBoxPreference checkboxPref = (CheckBoxPreference) getPreferenceManager()
                .findPreference("checkboxPref");
        checkboxPref.setOnPreferenceChangeListener(onPreferenceChangeListener);
    }
}

您可能会遇到安全例外 - 请参阅here添加权限:

<!-- Allows an application to change whether an application component (other than
      its own) is enabled or not. -->
<permission android:name="android.permission.CHANGE_COMPONENT_ENABLED_STATE"
    android:label="@string/permlab_changeComponentState"
    android:description="@string/permdesc_changeComponentState"
    android:protectionLevel="signatureOrSystem" />

你也必须小心:

final ComponentName componentName = new ComponentName(
                            "com.example.yourapp1", WIFIBroadcastReceiver.class);

如果这解决了,请回复