无法让广播接收器处理具有多个操作的意图

时间:2012-11-13 21:35:13

标签: android android-intent

我很难让BroadcastReceiver处理我的IntentService响应。该服务处理多个不同的操作并返回操作类型。然而,接收器似乎永远不会捡起它。意图确实被调用,因为我可以在IntentService中调试和设置断点,并查看操作是否已成功处理。我从来没有看到使用适当的数据更新文本框或看到正在调用BroadcastReceiver evein。

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);
}

广播接收器作为主要活动的子类

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 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>

我做错了什么?请注意,IntentService作为Android类库的一部分存在,其包含与Main活动不同的包。

1 个答案:

答案 0 :(得分:1)

因为接收器存在更新活动中的数据,所以应该在活动恢复时注册,并在活动暂停时注销。它不应该在清单中(请参阅the doc)。

如果不是这样,那么它不应该是您活动的子类。

在所有情况下,我认为您的Receiver未被调用,因为它在清单中没有正确的名称。它可能是这样的:.MainActivity$CheckinReceiver