Search code examples
androidbroadcastreceiver

Android - BroadcastReceiver gets called unexpectedly


I'm using broadcasts to check if the connexion switches between off to on and then upload some data to Internet. The problem it's that this broadcast gets called suddenly. I don't want to get it called at that moment. I don't know how can I 'lock' this broadcast at that moment.

Here's the code of my BroadcastReceiver class:

class Broadcast_Reciver extends BroadcastReceiver implements Variables {

    CheckConexion cc;

    @Override
    public void onReceive(Context contxt, Intent intent) {

        // Cuando hay un evento, lo diferenciamos y hacemos una acción.

        if (intent.getAction().equals(SMS_RECEIVED)) {
            Sms sms = new Sms(null, contxt);
            sms.uploadNewSms(intent);
        } else if (intent.getAction().equals(Intent.ACTION_BATTERY_LOW)) {
            // st.batterylow(contxt);
        } else if (intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)) {
            // st.power(1, contxt);
        } else if (intent.getAction().equals(Intent.ACTION_POWER_DISCONNECTED)) {
            // st.power(0, contxt);
        } else if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)
                || intent.getAction().equals(Intent.ACTION_PACKAGE_CHANGED)
                || intent.getAction().equals(Intent.ACTION_PACKAGE_REMOVED)) {
            Database db = new Database(contxt);
            if (db.open().Preferences(4)) {
                Uri data = intent.getData();
                new ListApps(contxt).import_app(intent, contxt, data,
                        intent.getAction());
            }
            db.close();
        } else if (intent.getAction().equals(
                ConnectivityManager.CONNECTIVITY_ACTION)) {
            cc = new CheckConexion(contxt);
            if (cc.isOnline()) {
                /*
                 * Database db = new Database(contxt); if (db.open() != null) {
                 * if (db.move() == 1) { new UploadOffline(contxt); }
                 * db.close();
                 */

            }
        }

    }
}

I'm registering the broadcasts on a Java file not in a XML file because it's a service.

Take a look at this code when i register it:

        Broadcast_Reciver r = new Broadcast_Reciver();
        IntentFilter i = new IntentFilter();
        i.addAction(SMS_RECEIVED);
        i.addAction(Intent.ACTION_BATTERY_LOW);
        i.addAction(Intent.ACTION_POWER_CONNECTED);
        i.addAction(Intent.ACTION_POWER_DISCONNECTED);
        i.addAction(Intent.ACTION_CALL_BUTTON);
        i.addAction(Intent.ACTION_CAMERA_BUTTON);
        i.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(r, i);
        IntentFilter apps = new IntentFilter();
        apps.addAction(Intent.ACTION_PACKAGE_ADDED);
        apps.addAction(Intent.ACTION_PACKAGE_CHANGED);
        apps.addAction(Intent.ACTION_PACKAGE_REMOVED);
        apps.addDataScheme("package");
        registerReceiver(r, apps);

Solution

  • if you don't want your receiver to be called, unregister it.