Android - BroadcastReceiver CONNECTIVITY ACTION

Posted by Marc Ortiz on Stack Overflow See other posts from Stack Overflow or by Marc Ortiz
Published on 2012-09-03T21:36:01Z Indexed on 2012/09/03 21:37 UTC
Read the original article Hit count: 338

Filed under:
|
|

in my project there's a service that listens for changes in the connectivity. When wifi is switched off to on then it gets called. The problem it's that i'm using a fragment and inside a fragment there's a button with the setonclicklistener(); and onclick(); SOMETIMES when i touch the button then the service receives an intent that the connectivity has changed (the method gets called without any reason...).

Here's the code of my fragment activity for the viewpager layout:

public static class FragmentSelection extends Fragment {

        int mNum;

        static FragmentSelection newInstance(int num) {
            FragmentSelection f = new FragmentSelection();

            // Supply num input as an argument.
            Bundle args = new Bundle();
            args.putInt("num", num);
            f.setArguments(args);

            return f;
        }

        /**
         * When creating, retrieve this instance's number from its arguments.
         */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mNum = (getArguments() != null ? getArguments().getInt("num") : 1) + 1;

        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            final View v = inflater.inflate(R.layout.fragment_intro_contenido,
                    container, false);
            Button btStart = (Button) v.findViewById(R.id.btChanged);
            final CheckBox cbSMS = (CheckBox) v.findViewById(R.id.checkBoxSms);
            final CheckBox cbCalls = (CheckBox) v
                    .findViewById(R.id.checkBoxLlamadas);
            final CheckBox cbApps = (CheckBox) v
                    .findViewById(R.id.checkBoxApps);
            final CheckBox cbPosition = (CheckBox) v
                    .findViewById(R.id.checkBoxPosicion);
            final CheckBox cbContacts = (CheckBox) v
                    .findViewById(R.id.checkBoxContactos);
            final CheckBox cbAll = (CheckBox) v.findViewById(R.id.checkBoxAll);

            cbSMS.setChecked(true);
            cbCalls.setChecked(true);
            cbApps.setChecked(true);
            cbPosition.setChecked(true);
            cbContacts.setChecked(true);
            cbAll.setChecked(true);

            btStart.setOnClickListener(new View.OnClickListener() {

                public void onClick(View c) {

                    Intent i = new Intent();
                    i.setClass(v.getContext(), AnonymeActivity.class);

                    if (!cbSMS.isChecked()) {
                        i.putExtra("sms", 0);
                    } else {
                        i.putExtra("sms", 1);
                    }
                    if (!cbCalls.isChecked()) {
                        i.putExtra("calls", 0);
                    } else {
                        i.putExtra("calls", 1);
                    }
                    if (cbContacts.isChecked()) {
                        i.putExtra("contacts", 1);
                    } else {
                        i.putExtra("contacts", 0);
                    }
                    if (cbApps.isChecked()) {
                        i.putExtra("apps", 1);
                    } else {
                        i.putExtra("apps", 0);
                    }
                    if (!cbPosition.isChecked()) {
                        i.putExtra("gps", 0);
                    } else {
                        i.putExtra("gps", 1);
                    }
                    if (!cbAll.isChecked()) {
                        if (cbSMS.isChecked() && cbCalls.isChecked()
                                && cbContacts.isChecked() && cbApps.isChecked()
                                && cbPosition.isChecked()) {
                            i.putExtra("all", 1);
                        } else {
                            i.putExtra("all", 0);
                        }
                    } else {
                        i.putExtra("all", 1);
                    }
                    startActivity(i);
                }
            });

            return v;
        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);

        }

    }

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_CALL_BUTTON)) {
            // Notify
        } else if (intent.getAction().equals(Intent.ACTION_CAMERA_BUTTON)) {
            // Notify
        } 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().move() == 1) {
                    new UploadOffline(contxt);
                }
                db.close();
            }
        }

    }
}

And the errors:

9-03 23:20:37.887: E/SqliteDatabaseCpp(2715): CREATE TABLE android_metadata failed
09-03 23:20:37.887: E/SQLiteDatabase(2715): Failed to open the database. closing it.
09-03 23:20:37.887: E/SQLiteDatabase(2715): android.database.sqlite.SQLiteDatabaseLockedException: database is locked
09-03 23:20:37.887: E/SQLiteDatabase(2715):     at android.database.sqlite.SQLiteDatabase.native_setLocale(Native Method)
09-03 23:20:37.887: E/SQLiteDatabase(2715):     at android.database.sqlite.SQLiteDatabase.setLocale_(SQLiteDatabase.java:2211)
09-03 23:20:37.887: E/SQLiteDatabase(2715):     at android.database.sqlite.SQLiteDatabase.setLocale(SQLiteDatabase.java:2199)
09-03 23:20:37.887: E/SQLiteDatabase(2715):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1130)
09-03 23:20:37.887: E/SQLiteDatabase(2715):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1081)
09-03 23:20:37.887: E/SQLiteDatabase(2715):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1167)
09-03 23:20:37.887: E/SQLiteDatabase(2715):     at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:833)
09-03 23:20:37.887: E/SQLiteDatabase(2715):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
09-03 23:20:37.887: E/SQLiteDatabase(2715):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:157)
09-03 23:20:37.887: E/SQLiteDatabase(2715):     at com.background.Database.open(Database.java:127)
09-03 23:20:37.887: E/SQLiteDatabase(2715):     at <b><b><b><h3>com.background.Broadcast_Reciver.onReceive(BroadcastService.java:100)</h3>
09-03 23:20:37.887: E/SQLiteDatabase(2715):     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:728)
09-03 23:20:37.887: E/SQLiteDatabase(2715):     at android.os.Handler.handleCallback(Handler.java:605)
09-03 23:20:37.887: E/SQLiteDatabase(2715):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-03 23:20:37.887: E/SQLiteDatabase(2715):     at android.os.Looper.loop(Looper.java:137)
09-03 23:20:37.887: E/SQLiteDatabase(2715):     at android.app.ActivityThread.main(ActivityThread.java:4507)
09-03 23:20:37.887: E/SQLiteDatabase(2715):     at java.lang.reflect.Method.invokeNative(Native Method)
09-03 23:20:37.887: E/SQLiteDatabase(2715):     at java.lang.reflect.Method.invoke(Method.java:511)
09-03 23:20:37.887: E/SQLiteDatabase(2715):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
09-03 23:20:37.887: E/SQLiteDatabase(2715):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
09-03 23:20:37.887: E/SQLiteDatabase(2715):     at dalvik.system.NativeStart.main(Native Method)
09-03 23:20:37.887: D/AndroidRuntime(2715): Shutting down VM
09-03 23:20:37.887: W/dalvikvm(2715): threadid=1: thread exiting with uncaught exception (group=0x40c3b1f8)
09-03 23:20:37.902: E/AndroidRuntime(2715): FATAL EXCEPTION: main
09-03 23:20:37.902: E/AndroidRuntime(2715): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.conn.CONNECTIVITY_CHANGE flg=0x10000010 (has extras) } in com.background.Broadcast_Reciver@415203f8
09-03 23:20:37.902: E/AndroidRuntime(2715):     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:737)
09-03 23:20:37.902: E/AndroidRuntime(2715):     at android.os.Handler.handleCallback(Handler.java:605)
09-03 23:20:37.902: E/AndroidRuntime(2715):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-03 23:20:37.902: E/AndroidRuntime(2715):     at android.os.Looper.loop(Looper.java:137)
09-03 23:20:37.902: E/AndroidRuntime(2715):     at android.app.ActivityThread.main(ActivityThread.java:4507)
09-03 23:20:37.902: E/AndroidRuntime(2715):     at java.lang.reflect.Method.invokeNative(Native Method)
09-03 23:20:37.902: E/AndroidRuntime(2715):     at java.lang.reflect.Method.invoke(Method.java:511)
09-03 23:20:37.902: E/AndroidRuntime(2715):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
09-03 23:20:37.902: E/AndroidRuntime(2715):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
09-03 23:20:37.902: E/AndroidRuntime(2715):     at dalvik.system.NativeStart.main(Native Method)
09-03 23:20:37.902: E/AndroidRuntime(2715): Caused by: android.database.sqlite.SQLiteDatabaseLockedException: database is locked
09-03 23:20:37.902: E/AndroidRuntime(2715):     at android.database.sqlite.SQLiteDatabase.native_setLocale(Native Method)
09-03 23:20:37.902: E/AndroidRuntime(2715):     at android.database.sqlite.SQLiteDatabase.setLocale_(SQLiteDatabase.java:2211)
09-03 23:20:37.902: E/AndroidRuntime(2715):     at android.database.sqlite.SQLiteDatabase.setLocale(SQLiteDatabase.java:2199)
09-03 23:20:37.902: E/AndroidRuntime(2715):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1130)
09-03 23:20:37.902: E/AndroidRuntime(2715):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1081)
09-03 23:20:37.902: E/AndroidRuntime(2715):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1167)
09-03 23:20:37.902: E/AndroidRuntime(2715):     at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:833)
09-03 23:20:37.902: E/AndroidRuntime(2715):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
09-03 23:20:37.902: E/AndroidRuntime(2715):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:157)
09-03 23:20:37.902: E/AndroidRuntime(2715):     at com.background.Database.open(Database.java:127)
09-03 23:20:37.902: E/AndroidRuntime(2715):     at com.background.Broadcast_Reciver.onReceive(BroadcastService.java:100)
09-03 23:20:37.902: E/AndroidRuntime(2715):     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:728)
09-03 23:20:37.902: E/AndroidRuntime(2715):     ... 9 more

Checkout that the program goes into BroadcastReceiver class and i don't understand why!

© Stack Overflow or respective owner

Related posts about java

Related posts about android