[英]Connect to GoogleApiClient from IntentService
I'm new to Android development and I'm trying to setup a geofence that uses a pending intent to notify the user they've entered the geofence and won a badge. 我是Android开发的新手,我正在尝试设置一个使用待定意图的地理围栏来通知用户他们已进入地理围栏并获得了徽章。 I am using Google Play Games Services to set up the badges/achievements. 我正在使用Google Play游戏服务来设置徽章/成就。 I want to make the notification clickable so it takes you to your achievements page. 我想让通知可以点击,因此它会将您带到您的成就页面。 This is my IntentService: 这是我的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());
}
} }
This code gives me to following error and is unable to connect to the GoogleApiClient: 此代码使我出现以下错误,无法连接到GoogleApiClient:
E/PopupManager: No content view usable to display popups. E / PopupManager:没有可用于显示弹出窗口的内容视图。 Popups will not be displayed in response to this client's calls. 弹出窗口不会显示以响应此客户端的呼叫。 Use setViewForPopups() to set your content view. 使用setViewForPopups()设置内容视图。
How can I connect to GoogleApiClient from a pending intent or how can I make the notification clickable so that it takes me to Google Play Games Services Achievements intent? 如何从待处理的意图连接到GoogleApiClient,或者如何使通知可以点击以便我将其转到Google Play游戏服务成就意图?
I figured out the problem. 我解决了这个问题。 I was not giving the GoogleApiClient instance enough time to connect. 我没有给GoogleApiClient实例足够的时间来连接。 So after building the GoogleApiClient and calling its connect() method, I added this line: 因此,在构建GoogleApiClient并调用其connect()方法之后,我添加了以下行:
ConnectionResult connectionResult = mGoogleApiClient.blockingConnect(30, TimeUnit.SECONDS);
That solved it for me. 这解决了我。 Hope this helps anyone! 希望这有助于任何人!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.