2017-01-28 22 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

相关问题