所以我跟着我发现这里的文档:https://developer.android.com/training/location/geofencing.htmlAndroid的地理栅栏不触发,造成GPS的活动,或致电IntentService
实现教程后,我可以看到,地理栅栏我与已创建和测试添加。我获得了成功地位。
我现在的问题是:添加了地理栅栏,但我的手机没有显示GPS活动,即使添加成功,Geofence也没有触发PendingIntent。
我在舱单申报服务:
<service android:name=".journeytools.TraiNapIntentService"
android:exported="false"/>
和我有以下权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
类添加地理栅栏是:
public class SplashScreen extends Activity implements View.OnClickListener, ResultCallback<Status>,
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
TextView miles, push, alarm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
buildGoogleApiClient();
System.out.println("Building Google Api Client");
if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext())
!= ConnectionResult.SUCCESS) {
//TODO Add something to post a message to the screen that the service isn't available
System.out.println("Service not Available");
} else {
System.out.println("Service Available");
miles = (TextView) findViewById(R.id.tvmiles);
push = (TextView) findViewById(R.id.tvpush);
alarm = (TextView) findViewById(R.id.tvalarm);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(this);
}
}
ArrayList geoFenceList;
PendingIntent geoFencePendingIntent = null;
/**
* Provides the entry point to Google Play services.
*/
protected GoogleApiClient mGoogleApiClient;
@Override
public void onClick(View v) {
System.out.println("Onclick pressed");
DataGather data = new DataGather();
data.writePreferences(this, 5, false, true);
data.writeRecentJourney(this, "Wareham", "Poole");
double longitude = data.getGoingToLong(getBaseContext());
double latitude = data.getGoingToLat(getBaseContext());
float proximity = getProximity();
/*
* Add the geofence work.
*/
geoFenceList = new ArrayList();
geoFenceList.add(
new Geofence.Builder()
.setRequestId("Testing the addition")
.setCircularRegion(
latitude,
longitude,
proximity)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER)
.build()
);
System.out.println("Added Geofence to Arraylist");
LocationServices.GeofencingApi.addGeofences(
mGoogleApiClient,
getGeofencingRequest(),
getGeofencePendingIntent()
).setResultCallback(this);
System.out.println("Added GeoFence");
}
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
@Override
protected void onStop() {
super.onStop();
mGoogleApiClient.disconnect();
}
/**
* Builds a GoogleApiClient. Uses the {@code #addApi} method to request the LocationServices API.
*/
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
private GeofencingRequest getGeofencingRequest() {
GeofencingRequest.Builder geoFencingRequestBuilder = new GeofencingRequest.Builder();
geoFencingRequestBuilder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
geoFencingRequestBuilder.addGeofences(geoFenceList);
System.out.println("Returning geoFencingRequestBuilder.build()");
return geoFencingRequestBuilder.build();
}
/**
* Uses the users inserted information to derive the proximity setting.
* @return Returns a float of the proximity number.
*/
private float getProximity(){
DataGather dataGather = new DataGather();
String[] prefs = dataGather.getPreferences(this);
System.out.println("Returning Float" + Float.parseFloat(prefs[0]) * 1609.344);
return (float) (Float.parseFloat(prefs[0]) * 1609.344);
}
private PendingIntent getGeofencePendingIntent(){
Intent intent = new Intent(this, TraiNapIntentService.class);
System.out.println("Returning PendingIntent.getSerivce");
return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
/**
* TODO Finish this method
* @param status
*/
public void onResult(Status status) {
if (status.isSuccess()) {
System.out.println("Success status received");
} else {
System.out.println("Non-Success status received");
}
}
/**
* TODO Finish this method
* @param bundle
*/
@Override
public void onConnected(Bundle bundle) {
System.out.println("onConnected");
}
/**
* TODO Finish this method
* @param i
*/
@Override
public void onConnectionSuspended(int i) {
System.out.println("onConnectionSuspended");
}
/**
* TODO Finish this method
* @param connectionResult
*/
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
System.out.println("Connection Failed");
}
}
我的代码没有按没有达到IntentService,所以我没有发布,但如果有人需要,我可以。
有什么想法缺少什么?
猜测这已经让所有人难住,而不仅仅是我... –
你有没有找到这个解决方案?我在同一条船上。 –