Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop BroadcastReceiver manually in android

I have registered my BroadcastReceiver in AndroidManifest.xml like this

<?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>

I want to stop this BroadcastReceiver using other application input how to stop this receiver programmatically ?

here is my receiver code:

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

Answer:: I have add sharedprefernce checkbox and save the two string and validate in on receive method here is my code

second application :

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

validating code in first application inside of onReceive method:

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

this is my need. But I am not able to unregister the broadcast receiver. with out activity any this answer will helpful for some one.

like image 788
Gurumoorthy Arumugam Avatar asked Feb 22 '12 05:02

Gurumoorthy Arumugam


People also ask

What is BroadcastReceiver Android?

Android BroadcastReceiver is a dormant component of android that listens to system-wide broadcast events or intents. When any of these events occur it brings the application into action by either creating a status bar notification or performing a task.

How pass data from BroadcastReceiver to activity in Android?

Intent intent = getIntent(); String message = intent. getStringExtra("message"); And then you will use message as you need. If you simply want the ReceiveText activity to show the message as a dialog, declare <activity android:theme="@android:style/Theme.

Where do I register and unregister broadcast receiver?

Receiver can be registered via the Android manifest file. You can also register and unregister a receiver at runtime via the Context. registerReceiver() and Context. unregisterReceiver() methods.


2 Answers

You can use this :

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

PackageManager pm = context.getPackageManager();

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

For more information visit here.

like image 110
Mahmut EFE Avatar answered Oct 13 '22 03:10

Mahmut EFE


Like this:

PackageManager pm = getPackageManager();
pm.setComponentEnabledSetting(WiFiScanReceiver,
                            PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                            PackageManager.DONT_KILL_APP);
like image 25
Jviaches Avatar answered Oct 13 '22 02:10

Jviaches