2017-01-28 96 views
1

我是Android新手,我試圖設置一個geofence,它使用一個未決的意圖通知用戶他們已經進入geofence並贏得了徽章。我正在使用Google Play遊戲服務設置徽章/成就。我想讓通知可點擊,以便將您帶到成就頁面。這是我的IntentService:從IntentService連接到GoogleApiClient

public class GeofenceService extends IntentService { 
private NotificationManager mNotificationManager; 
public static final String TAG = "GeofenceService"; 
private GoogleApiClient mGoogleApiClient; 

public GeofenceService() { 
    super(TAG); 
} 

@Override 
protected void onHandleIntent(Intent intent) { 
    GeofencingEvent event = GeofencingEvent.fromIntent(intent); 

    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addApi(Games.API) 
      .addScope(Games.SCOPE_GAMES) 
      .build(); 
    mGoogleApiClient.connect(); 

    if (event.hasError()) { 
     //TODO handle error 
    } else { 
     int transition = event.getGeofenceTransition(); 
     List<Geofence> geofences = event.getTriggeringGeofences(); 
     Geofence geofence = geofences.get(0); 
     String requestId = geofence.getRequestId(); 

     if (transition == Geofence.GEOFENCE_TRANSITION_ENTER) { 
      Log.d(TAG, "onHandleIntent: Entering geofence - " + requestId); 

      if (mGoogleApiClient.isConnected()){ 
       sendNotification("+ 100"); 
      } 

     } else if (transition == Geofence.GEOFENCE_TRANSITION_EXIT) { 
      Log.d(TAG, "onHandleIntent: Exiting Geofence - " + requestId); 
     } 
    } 
} 


private String getTransitionString(int transitionType) { 
    switch (transitionType) { 
     case Geofence.GEOFENCE_TRANSITION_ENTER: 
      return getString(R.string.geofence_transition_entered); 
     case Geofence.GEOFENCE_TRANSITION_EXIT: 
      return getString(R.string.geofence_transition_exited); 
     default: 
      return getString(R.string.unknown_geofence_transition); 
    } 
} 

private void sendNotification(String details){ 
    mNotificationManager = (NotificationManager) 
      this.getSystemService(Context.NOTIFICATION_SERVICE); 

    Intent gamesIntent = Games.Achievements.getAchievementsIntent(mGoogleApiClient); 
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
      gamesIntent, 0); 


    NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(this) 
        .setContentTitle("You got a badge") 
        .setStyle(new NotificationCompat.BigTextStyle() 
          .bigText(details)) 
        .setContentText(details) 
        .setSmallIcon(R.drawable.tour); 

    mBuilder.setContentIntent(contentIntent); 
    mNotificationManager.notify(1, mBuilder.build()); 
} 

}

此代碼給我下面的錯誤,無法連接到GoogleApiClient:

E/PopUpManager中:沒有可用來顯示彈出窗口內容視圖。不會響應此客戶的呼叫顯示彈出式窗口 。使用 setViewForPopups()來設置您的內容視圖。

我怎樣才能連接到GoogleApiClient從待意圖或我怎麼能做出通知點擊,以便它帶我到谷歌Play遊戲服務成就意圖是什麼?

回答

0

我想出了問題所在。我沒有給GoogleApiClient實例足夠的時間來連接。因此,建設GoogleApiClient並調用其connect()方法後,我加入這一行:

ConnectionResult connectionResult = mGoogleApiClient.blockingConnect(30, TimeUnit.SECONDS); 

這解決了這個問題對我來說。希望這有助於任何人!

+0

也許會更好地傾聽GoogleApiClient.ConnectionCallbacks。連接建立後您會收到通知。 – Lancelot

相關問題