1

インテントサービスが完了した後、オブジェクトを戻すのに問題があります。 私の目標は、通知が送信された後に、MainactivityにcurrentCharacterオブジェクトを送り返すことです。私はonResultでそれを試しましたが、intentserviceはこれを持っていません。私も独自のresultreceiverを作成しようとしましたが、これを実装すると、通知が届かなくなります。誰もがこの問題の解決策または回避策を知っていますか?IntentServiceがresultreceiverと動作しません

これは、特定の地域を入力するとMainActivityから呼び出されるGeofenceTransitionIntentServiceです。 MainActivityに返信したいcurrentCharacterオブジェクトがあります。そこには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"); 

     } 
    }); 
} 

}

答えて

1

は、あなたはgetParcelableExtra(でResultReceiver取得していない)が、私は、あなたがどこに行くことはありませんrec.send(int,Bundle);

MainActivityを使用してそれを介してブロードキャストを送信表示されません通知された。 これは、あなたが言ったように通知が届かない可能性がある理由です。

0

まず、サービスとIntentServiceの違いを理解する必要があります。 IntentServiceが開始されるたびに、ワーカースレッドを使用して各Intentを順番に処理します。これは基本的にメインスレッド上で動作しているサービスとの主な違いです。つまり、作業要求のステータスをアクティビティコンポーネントに送信する方法を見つける必要があると言われています。これを行うには、CurrentCharacterを含むブロードキャストを、Activityに存在するはずのBroadcastReceiverに送信します。公式アンドロイドデベロッパーのドキュメントから次のリンクをご覧ください。https://developer.android.com/training/run-background-service/report-status.html

+0

これがどのように正確に機能するか具体的な例を教えてください。 私はドキュメントを見ましたが、私はそれを理解できません(BroadcastReceiver) –

関連する問題