2012-11-13 28 views
1

我很難獲得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活動不同。

+0

只是一個點,你也不會在Java這樣的...比較字符串**如果(動作== ACTION_GET_POI)** - 你應該使用'如果(action.equals(ACTION_GET_POI))'。 – Squonk

回答

1

由於接收器的存在是爲了更新活動數據,應當登記活動簡歷和未登記的活動時暫停時。它不應該在清單中(參見the doc)。

如果不是這種情況,它不應該是你的活動的一個子類。

在任何情況下,我覺得你的接收機不叫,因爲它在清單中不是正確的名稱。它可能是這樣的:.MainActivity$CheckinReceiver

+0

我試圖爲了測試線束的目的而迅速做到這一點。將它移動到Activity代碼中註冊。 – mjsalinger

+0

爲什麼IntentService無法在Manifest中註冊? –

相關問題