gpt4 book ai didi

java - 从 IntentService 连接到 GoogleApiClient

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

我是 Android 开发新手,我正在尝试设置一个地理围栏,该地理围栏使用待处理的 Intent 来通知用户他们已进入地理围栏并赢得了徽章。我正在使用 Google Play 游戏服务来设置徽章/成就。我想让通知可点击,以便您进入成就页面。这是我的 IntentService:

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: No content view usable to display popups. Popups will not be displayed in response to this client's calls. Use setViewForPopups() to set your content view.

如何从待处理 Intent 连接到 GoogleApiClient,或者如何使通知可点击,以便将我带到 Google Play 游戏服务成就 Intent ?

最佳答案

我发现了问题所在。我没有给 GoogleApiClient 实例足够的时间来连接。因此,在构建 GoogleApiClient 并调用其 connect() 方法后,我添加了这一行:

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

这为我解决了这个问题。希望这对任何人都有帮助!

关于java - 从 IntentService 连接到 GoogleApiClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41914922/

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