gpt4 book ai didi

android - IntentService 不适用于 resultreceiver

转载 作者:行者123 更新时间:2023-11-30 00:43:39 29 4
gpt4 key购买 nike

在 intentservice 完成后,我无法将对象传回。我的目标是在发送通知后将 currentCharacter 对象发送回 Mainactivity。我用 onResult 尝试过,但 intentservice 没有这个。我也尝试创建一个自己的 resultreceiver 但是当我实现它时,通知甚至没有通过。有谁知道这个问题的解决方案或解决方法?

这是当我进入特定区域时从 MainActivity 调用的 GeofenceTransitionIntentService。我有一个 currentCharacter 对象,我想将其发送回 MainActivity,我在 MainActivity 中也有一个 currentCharacter,但这需要更新。

public class GeofenceTransitionIntentService extends IntentService {
protected static final String TAG = "MainActivity";
private boolean checkedIn = false;
private List<Store> stores;
private Store rightStore;
private Character currentCharacter;
private ResultReceiver rec;

/**
* This constructor is required, and calls the super IntentService(String)
* constructor with the name for a worker thread.
*/
public GeofenceTransitionIntentService() {
// Use the TAG to name the worker thread.
super(TAG);
}

@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate: geofencetransition");
}

/**
* Handles incoming intents.
*
* @param intent sent by Location Services. This Intent is provided to Location
* Services (inside a PendingIntent) when addGeofences() is called.
*/
@Override
protected void onHandleIntent(Intent intent) {
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
if (geofencingEvent.hasError()) {
String errorMessage = "err";
Log.e(TAG, errorMessage);
return;
}

// Get the transition type.
int geofenceTransition = geofencingEvent.getGeofenceTransition();

if(!checkedIn) {
// Test that the reported transition was of interest.
if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER ||
geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {

// Get the geofences that were triggered. A single event can trigger multiple geofences.
List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();

// Get the transition details as a String.
String geofenceTransitionDetails = getGeofenceTransitionDetails(
geofenceTransition,
triggeringGeofences
);

stores = new Gson().fromJson(intent.getStringExtra("stores"),
new TypeToken<ArrayList<Store>>() {
}.getType());

// Send notification and log the transition details.
sendNotification(geofenceTransitionDetails);
currentCharacter = new Gson().fromJson(intent.getStringExtra("char"),
Character.class);
rec = intent.getParcelableExtra("receiverTag");
String date = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH).format(Calendar.getInstance().getTime());
Checkin checkin = new Checkin(rightStore, date, 10);
currentCharacter.getCheckins().add(checkin);
currentCharacter.setCurrentExp(currentCharacter.getCurrentExp() + checkin.getReceivedExp());
putCharacter();

Log.d(TAG, geofenceTransitionDetails);
} else {
// Log the error.
Log.d(TAG, "error");
}
}
}

/**
* Gets transition details and returns them as a formatted string.
*
* @param geofenceTransition The ID of the geofence transition.
* @param triggeringGeofences The geofence(s) triggered.
* @return The transition details formatted as String.
*/
private String getGeofenceTransitionDetails(
int geofenceTransition,
List<Geofence> triggeringGeofences) {

String geofenceTransitionString = getTransitionString(geofenceTransition);

// Get the Ids of each geofence that was triggered.
ArrayList<String> triggeringGeofencesIdsList = new ArrayList<>();
for (Geofence geofence : triggeringGeofences) {
triggeringGeofencesIdsList.add(geofence.getRequestId());
}
String triggeringGeofencesIdsString = TextUtils.join(", ", triggeringGeofencesIdsList);

return geofenceTransitionString + triggeringGeofencesIdsString;
}

/**
* Posts a notification in the notification bar when a transition is detected.
* If the user clicks the notification, control goes to the MainActivity.
*/
private void sendNotification(String notificationDetails) {
Intent notifyIntent;
notifyIntent = new Intent(getApplicationContext(), StoresDetail.class);
for(Store store : stores){
if(notificationDetails.contains(store.getName())){
rightStore = store;
}
}

notifyIntent.putExtra("store", rightStore);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent pendingIntent = PendingIntent.getActivities(getApplicationContext(), 1234,
new Intent[] { notifyIntent }, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.logo)
.setContentTitle(notificationDetails)
.setContentText("Gamification")
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.build();
notification.defaults |= Notification.DEFAULT_SOUND;
NotificationManager notificationManager =
(NotificationManager) this.getSystemService(this.NOTIFICATION_SERVICE);
notificationManager.notify(5678, notification);
}

/**
* Maps geofence transition types to their human-readable equivalents.
*
* @param transitionType A transition type constant defined in Geofence
* @return A String indicating the type of transition
*/
private String getTransitionString(int transitionType) {
switch (transitionType) {
case Geofence.GEOFENCE_TRANSITION_ENTER:
checkedIn = true;
return "Entered geofence: ";
case Geofence.GEOFENCE_TRANSITION_EXIT:
checkedIn = false;
return "Exited geofence: ";
default:
return "default";
}
}

private void putCharacter(){
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
Gson gson = new Gson();

APIService caller = new APIService();
caller.put(queue, "character/", gson.toJson(currentCharacter), new VolleyCallback() {
@Override
public void onSuccess(String result) {
Log.d("GeofenceTransitionSer", "onSuccess of VolleyCallback, Method: putCharacter");
}

@Override
public void onFailed(String result) {
Log.d("GeofenceTransitionSer", "onFailed of VolleyCallback, Method: putCharacter");

}
});
}

最佳答案

通过您的代码,您确实通过 getParcelableExtra() 检索了 ResultReceiver,但我没有看到您使用 rec.send(int,Bundle);

MainActivity 永远不会收到通知。这就是为什么通知可能不会像您所说的那样通过。

关于android - IntentService 不适用于 resultreceiver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42157776/

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