(un)registerReceiver not giving expected results.

General topics about the Android-Platform itself.
Coding issues please to the subforum right below.

(un)registerReceiver not giving expected results.

Postby aellis » Sun Apr 04, 2010 10:15 am

Hi all,

I would like to register a broadcast receiver on onStart of an
activity and then unregister the receiver when the activity gets
onStop called. When I debug I see that both of these calls are made
when I expected, but for some reason, my broadcast receiver keeps
handling intents.

Can someone please enlighten me as to why the receiver is still
processing intents, even when I have called unregistered the receiver?

Best regards,

Alex

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class Main extends Activity {
  2.  
  3.        private static final String SMS_RECEIVED
  4.  
  5. ="android.provider.Telephony.SMS_RECEIVED";
  6.  
  7.  
  8.  
  9.        SMSReceiver r;
  10.  
  11.        IntentFilter smsFilter ;
  12.  
  13.        int resume = 0;
  14.  
  15.        int pause = 0;
  16.  
  17.  
  18.  
  19.        @Override
  20.  
  21.        public void onCreate(Bundle savedInstanceState) {
  22.  
  23.                super.onCreate(savedInstanceState);
  24.  
  25.                setContentView(R.layout.main);
  26.  
  27.                r = new SMSReceiver();
  28.  
  29.                smsFilter = new IntentFilter();
  30.  
  31.                smsFilter.addAction(SMS_RECEIVED);
  32.  
  33.        }
  34.  
  35.  
  36.  
  37.        @Override
  38.  
  39.        protected void onStart() {
  40.  
  41.                this.registerReceiver(r, smsFilter);
  42.  
  43.                resume++;
  44.  
  45.                super.onStart();
  46.  
  47.        }
  48.  
  49.  
  50.  
  51.        @Override
  52.  
  53.        protected void onStop() {
  54.  
  55.                this.unregisterReceiver(r);
  56.  
  57.                pause++;
  58.  
  59.                super.onStop();
  60.  
  61.        }
  62.  
  63. }
  64.  
  65.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.     package="com.SMSHandsFree"
  7.  
  8.     android:versionCode="1"
  9.  
  10.     android:versionName="1.0">
  11.  
  12.    <application android:icon="@drawable/icon" android:label="@string/
  13. app_name">
  14.  
  15.        <activity android:name=".Main"
  16.  
  17.                 android:label="@string/app_name">
  18.  
  19.            <intent-filter>
  20.  
  21.                <action android:name="android.intent.action.MAIN" />
  22.  
  23.                <category
  24.  
  25. android:name="android.intent.category.LAUNCHER" />
  26.  
  27.            </intent-filter>
  28.  
  29.        </activity>
  30.  
  31.  
  32.  
  33.    </application>
  34.  
  35.    <uses-sdk android:minSdkVersion="3" />
  36.  
  37.        <uses-permission android:name="android.permission.RECEIVE_SMS" />
  38.  
  39.        <uses-permission android:name="android.permission.READ_CONTACTS" />
  40.  
  41. </manifest>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4
aellis
Once Poster
Once Poster
 
Posts: 1
Joined: Sun Apr 04, 2010 10:13 am

Top

Return to General

Who is online

Users browsing this forum: No registered users and 7 guests