繁体   English   中英

从IntentService连接到GoogleApiClient

[英]Connect to GoogleApiClient from IntentService

我是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()设置内容视图。

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

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

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

这解决了我。 希望这有助于任何人!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM