【问题标题】:Connect to GoogleApiClient from IntentService从 IntentService 连接到 GoogleApiClient
【发布时间】:2017-01-28 21:17:26
【问题描述】:

我是 Android 开发的新手,我正在尝试设置一个地理围栏,它使用待处理的意图来通知用户他们已进入地理围栏并赢得了徽章。我正在使用 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:没有可用于显示弹出窗口的内容视图。弹出窗口将 不会在响应此客户的呼叫时显示。利用 setViewForPopups() 来设置你的内容视图。

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

【问题讨论】:

    标签: java android


    【解决方案1】:

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

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

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

    【讨论】:

    • 听 GoogleApiClient.ConnectionCallbacks 可能会更好。建立连接时您会收到通知。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-20
    • 2019-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多