1

我很难让 BroadcastReceiver 处理我的 IntentService 响应。该服务处理多个不同的操作并返回一个操作类型。然而,接收器似乎永远不会接听它。确实调用了意图,因为我可以在 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 活动不同。

4

1 回答 1

1

因为接收者的存在是为了更新activity中的数据,所以应该在activity恢复时注册,在activity暂停时取消注册。它不应该在清单中(参见文档)。

如果不是这种情况,它不应该是您的活动的子类。

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

于 2012-11-13T21:45:47.263 回答