gpt4 book ai didi

android - 无法让广播接收器处理具有多个操作的 Intent

转载 作者:行者123 更新时间:2023-11-30 03:55:53 25 4
gpt4 key购买 nike

我很难让 BroadcastReceiver 处理我的 IntentService 响应。该服务处理多个不同的操作并返回一个操作类型。然而,接收者似乎永远不会接收到它。 Intent 确实被调用,因为我可以调试并在 IntentService 中设置断点并看到操作被成功处理。我从来没有看到文本框更新了适当的数据,也没有看到 BroadcastReceiver 被调用。

Intent 服务

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 作为 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)) {

}
}

}

list

<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 Activity 不同。

最佳答案

因为接收器的存在是为了在 Activity 中更新数据,所以应该在 Activity 恢复时注册,在 Activity 暂停时取消注册。它不应该出现在 list 中(参见 the doc)。

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

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

关于android - 无法让广播接收器处理具有多个操作的 Intent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13369212/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com