【发布时间】:2017-03-30 04:55:35
【问题描述】:
我是 android 开发新手,正在尝试学习地理围栏。我相信我的意图服务可以正常工作,我可以注册我的地理围栏并在理论上接收到服务的转换;但是,我有兴趣从意图服务中获取数据并将其发送回MainActivity.class,以便可以利用它来执行一些任务。我看到很多创建通知的示例,但我不想要通知,而是简单地将转换类型和触发地理围栏传递回 MainActivity 类。
我的 GeofenceTransitionsIntentService.class 在下面,我假设我需要实现某种方式将结果发送回主类,并且我假设主类将需要某种侦听器来接收这些结果,因为他们发布.
public class GeofenceTransitionsIntentService extends IntentService implements GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks {
private GoogleApiClient myGoogleApiClient;
public GeofenceTransitionsIntentService(){
super(GeofenceTransitionsIntentService.class.getSimpleName());
}
@Override
public void onCreate(){
super.onCreate();
myGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
@Override
protected void onHandleIntent(Intent intent) {
GeofencingEvent geoFenceEvent = GeofencingEvent.fromIntent(intent);
if (geoFenceEvent.hasError()){
int errorcode = geoFenceEvent.getErrorCode();
Log.e("GeofencingApp", "ERROR: " + errorcode);
} else {
int transitionType = geoFenceEvent.getGeofenceTransition();
if (Geofence.GEOFENCE_TRANSITION_ENTER == transitionType){
myGoogleApiClient.blockingConnect(100, TimeUnit.MILLISECONDS);
String triggeredGeofenceID = geoFenceEvent.getTriggeringGeofences().get(0).getRequestId();
} else if (Geofence.GEOFENCE_TRANSITION_EXIT == transitionType) {
myGoogleApiClient.blockingConnect(100, TimeUnit.MILLISECONDS);
String triggeredGeofenceID = geoFenceEvent.getTriggeringGeofences().get(0).getRequestId();
Toast.makeText(getApplicationContext(), "EXIT: " + triggeredGeofenceID, Toast.LENGTH_LONG).show();
}
}
}
@Override
public void onConnected(@Nullable Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
}
【问题讨论】:
标签: android