mjsalinger
mjsalinger

Reputation: 660

Unable to get Broadcast Receiver to process Intent with Multiple Actions

I am having difficulty getting a BroadcastReceiver to process my IntentService response. The service processes multiple different actions and returns an action type. The receiver will never seem to pick it up, however. The intent does get called as I can debug and set a breakpoint in the IntentService and see the action get processed successfully. I just never see the textbox updated with the appropriate data or see the BroadcastReceiver evein being called.

IntentService

protected void onHandleIntent(Intent intent) {


        String action = intent.getAction();
        // Data the service was called with.
        Bundle incomingData = intent.getExtras();

        String key = incomingData.getString(KEY_APPKEY);
        String secret = incomingData.getString(KEY_SECRET);
        String collection = incomingData.getString(KEY_COLLECTION);

        CheckinManager cm = new CheckinManager(this.getApplicationContext(),key,secret,collection);

        Intent broadcastIntent = new Intent();

        broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);


        if (action == ACTION_GET_POI) {
            Double lat = incomingData.getDouble(KEY_LATITUDE);
            Double lon = incomingData.getDouble(KEY_LONGITUDE);

            ArrayList<POI> nearbyPOIs = new ArrayList<POI>();
            //broadcastIntent.setAction(ACTION_GET_POI_PROCESSED);
            broadcastIntent.setAction("com.msalinger.checkinmanager.CheckinService.getPOIProcessed");
            try {
                nearbyPOIs = cm.getPOI(lat, lon);

                broadcastIntent.putExtra(OUT_KEY_RESULT, true);
                broadcastIntent.putExtra(OUT_KEY_ERROR, "");
                broadcastIntent.putParcelableArrayListExtra(OUT_KEY_POILIST, nearbyPOIs);
            } catch (JSONException ex) {
                Log.d(TAG,ex.getMessage() + "\n" + ex.getStackTrace());
                broadcastIntent.putExtra(OUT_KEY_RESULT, false);
                broadcastIntent.putExtra(OUT_KEY_ERROR, ex.getMessage());
            }

        }
        else if (action == ACTION_CHECK_IN) {
            // Do something
        }
        else if (action ==  ACTION_GET_CHECKINS) {
            // Do Something
        }
        else if (action == ACTION_FIND_NEARBY_POIS_WITH_CHECKINS) {
            // Do Something 
        }    

        sendBroadcast(broadcastIntent);
}

Broadcast Receiver as sub-class of Main Activity

public class CheckinReceiver extends BroadcastReceiver {

        private final static String INTENT_BASE_URI = "com.msalinger.checkinmanager.CheckinService";

        private final static String ACTION_GET_POI_PROCESSED = ".getPOIProcessed";
        private final static String ACTION_CHECK_IN_PROCESSED = ".checkInProcessed";
        private final static String ACTION_GET_CHECKINS_PROCESSED = ".getCheckinsProcessed";
        private final static String ACTION_FIND_NEARBY_POIS_WITH_CHECKINS_PROCESSED = ".findNearbyPOIsWithCheckinsProcessed";


        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals("com.msalinger.checkinmanager.CheckinService.getPOIProcessed")) {
                tv = (TextView)findViewById(R.id.textBox1);

                Bundle incomingData = intent.getExtras();
                String st = "";

                if (incomingData.getBoolean("result")) {
                    ArrayList<POI> poiList = incomingData.getParcelableArrayList("poList");
                    st = printPOI(poiList);
                }
                else {
                    st = incomingData.getString("error");
                }
            }
            else if (intent.getAction().equals(INTENT_BASE_URI + ACTION_CHECK_IN_PROCESSED)) {

            }
            else if (intent.getAction().equals(INTENT_BASE_URI + ACTION_GET_CHECKINS_PROCESSED)) {

            }
            else if (intent.getAction().equals(INTENT_BASE_URI + ACTION_FIND_NEARBY_POIS_WITH_CHECKINS_PROCESSED)) {

            }
        }

    }

Manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.msalinger.checkinmanagerdemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="15" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service 
            android:enabled="true" 
            android:name="com.msalinger.checkinmanager.CheckinService" />
        <receiver
            android:name=".CheckinReceiver">
            <intent-filter>
                <action android:name="com.msalinger.checkinmanager.CheckinService.getPOIProcessed" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.msalinger.checkinmanager.CheckinService.checkInProcessed" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.msalinger.checkinmanager.CheckinService.getCheckinsProcessed" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.msalinger.checkinmanager.CheckinService.findNearbyPOIsWithCheckinsProcessed" />
            </intent-filter>
        </receiver>        
    </application>
</manifest>

What am I doing wrong? Note that the IntentService exists as part of an Android class library with a different package than the Main activity.

Upvotes: 1

Views: 1183

Answers (1)

yDelouis
yDelouis

Reputation: 2094

Because the receiver exists to update data in the activity, it should be registered when the activity resume and unregistered when the activity pause. And it should not be in the manifest (see the doc for this).

If it's not the case, it shouldn't be a subclass of your activity.

In all cases, I think your Receiver is not called because it has not the right name in the manifest. It may be something like this : .MainActivity$CheckinReceiver.

Upvotes: 1

Related Questions