Hi
I need some help registering and unregistering a broadcastReceiver from a 
widget.
When my widget is pressed i start a service with the following code

public void onStart( Intent intent, int startId )
{ 
SharedPreferences sharedPreferences = 
getApplicationContext().getSharedPreferences(SignalSpot.SHARED_PREFS_NAME, 
0);
boolean enabled = sharedPreferences.getBoolean("enabled", false);
Log.i(TAG,"Enabled: "+enabled);
//
SharedPreferences.Editor editor = sharedPreferences.edit();
       Log.i(TAG,"Just before if");
        if (!enabled) {
            try
            {IntentFilter filter = new IntentFilter();
        filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        SignalReceiver receiver = new SignalReceiver();
            Log.i(TAG,"Not enabled. Attempting to enable");
            registerReceiver(receiver, filter);
            Log.i(TAG,"Registered reciver");
            editor.putBoolean("enabled", true);
            editor.commit();
            Log.i(TAG,"Commited value");
            Toast.makeText(getApplicationContext(), "Signal Spot Enabled", 
Toast.LENGTH_LONG).show();
            Log.i(TAG,"Toast");
            }
            catch (Exception e)
            {
            Toast.makeText(getApplicationContext(), "Cannot Enable Signal 
Spot.\n" +
            "It May already be enabled. Please Try Again", 
Toast.LENGTH_LONG).show();}
        } else {
            try{
            SignalReceiver receiver = new SignalReceiver();
            Log.i(TAG,"Enabled. Attempting to disable");
            unregisterReceiver(receiver);
            Log.i(TAG,"Unregistered receiver");
            editor.putBoolean("enabled", false);
            editor.commit();
            Log.i(TAG,"Commited value");
            Toast.makeText(getApplicationContext(), "Signal Spot Disabled", 
Toast.LENGTH_LONG).show();
            Log.i(TAG,"Toast");
            }
            catch (Exception e)
            {Log.i(TAG,e.getMessage());
            Toast.makeText(getApplicationContext(), "Cannot disable Signal 
Spot.\n" +
            "It May not yet be enabled. Please Try Again", 
Toast.LENGTH_LONG).show();}
            
        }
 }

the problem is that each time i run this service i initialize SignalReceiver 
(my broadcastreceiver) so i am not registering and unregistering the 
same broadcastreceiver.
Is there any way around this or a better way to do this?
Thanks in advance.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to