Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从IntentService连接到GoogleAppClient_Java_Android - Fatal编程技术网

Java 从IntentService连接到GoogleAppClient

Java 从IntentService连接到GoogleAppClient,java,android,Java,Android,我是Android开发的新手,我正在尝试设置一个地理围栏,它使用一个待定的意图来通知用户他们已经进入了地理围栏并赢得了徽章。我正在使用Google Play Games服务设置徽章/成就。我想让通知可以点击,这样你就可以进入你的成就页面。这是我的意向服务: public class GeofenceService extends IntentService { private NotificationManager mNotificationManager; public static fina

我是Android开发的新手,我正在尝试设置一个地理围栏,它使用一个待定的意图来通知用户他们已经进入了地理围栏并赢得了徽章。我正在使用Google Play Games服务设置徽章/成就。我想让通知可以点击,这样你就可以进入你的成就页面。这是我的意向服务:

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());
}
公共类GeofenceService扩展了IntentService{
私人通知经理通知经理;
公共静态最终字符串TAG=“GeofenceService”;
私人GoogleapClient MGoogleapClient;
公共地理信息服务(){
超级(标签);
}
@凌驾
受保护的手部内容无效(意图){
GeofencingEvent事件=GeofencingEvent.fromIntent(intent);
mgoogleapclient=新的Googleapclient.Builder(此)
.addApi(Games.API)
.addScope(Games.SCOPE_Games)
.build();
mGoogleApiClient.connect();
if(event.hasError()){
//TODO句柄错误
}否则{
int transition=event.getGeofenceTransition();
List geofenses=event.getTriggeringGeofenses();
Geofence Geofence=geofences.get(0);
String requestId=geofines.getRequestId();
if(transition==地理围栏。地理围栏\u transition\u ENTER){
Log.d(标记“onHandleIntent:输入地理围栏-”+请求ID);
if(mgoogleapClient.isConnected()){
发送通知(“+100”);
}
}else if(transition==地理围栏。地理围栏\u transition\u EXIT){
Log.d(标签“onHandleIntent:正在退出的地理围栏-”+请求ID);
}
}
}
私有字符串getTransitionString(int transitionType){
开关(转换类型){
案例Geopfence.Geopfence\u过渡\u输入:
返回getString(输入R.string.geofrance);
案例Geofence.Geofence_过渡_出口:
返回getString(R.string.geofrance\u transition\u退出);
违约:
返回getString(R.string.unknown\u geopfence\u transition);
}
}
私有void sendNotification(字符串详细信息){
mNotificationManager=(NotificationManager)
this.getSystemService(Context.NOTIFICATION\u服务);
Intent GamesInt=Games.acgressions.GetAcgreementIntent(MGooGleapicClient);
PendingEvent contentIntent=PendingEvent.getActivity(此,0,
配子体,0);
通知相容建筑商mBuilder=
新建NotificationCompat.Builder(此)
.setContentTitle(“你有徽章”)
.setStyle(新通知Compat.BigTextStyle()
.bigText(详细信息))
.setContentText(详细信息)
.setSmallIcon(R.drawable.tour);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(1,mBuilder.build());
}
}

此代码导致以下错误,无法连接到GoogleAppClient:

E/PopupManager:没有可用于显示弹出窗口的内容视图。弹出窗口将 不显示以响应此客户端的呼叫。使用 setViewForPopups()设置内容视图


我怎样才能从一个待定的意图连接到GoogleAppClient,或者怎样才能使通知可点击,从而将我带到Google Play Games Services成就意图?

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

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

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

也许听一下GoogleAppClient.ConnectionCallbacks会更好。当建立连接时,您将收到通知。