当我为android地理围栏运行sample codes时,它运行良好。但是为了在我自己的项目中实现地理围栏,我试图自己设置它。首先,我在build {gradle文件和清单文件中添加一些代码,如documentation所述。然后我获得了应用程序的SHA1密钥并将其添加到console.developers.google.com页面以创建API密钥。最后,我启用了Google Maps Android API v2和Google Play Android Developer API。当我运行下面的代码时,即使构建了Google Api Client,mGoogleApiClient.isConnecting()函数也会返回true,但mGoogleApiClient.isConnected()函数返回false。
有人可以告诉我可能在哪里做错了吗?
public class MainActivity extends ActionBarActivity implements
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, ResultCallback<Status> {
protected ArrayList<Geofence> mGeofenceList;
protected GoogleApiClient mGoogleApiClient;
private PendingIntent mGeofencePendingIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buildGoogleApiClient();
mGeofenceList = new ArrayList<Geofence>();
mGeofenceList.add(new Geofence.Builder().setRequestId("ITU_Tekno_kent").setCircularRegion(41.107993, 29.032625, 1609).setExpirationDuration(100000).setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER |
Geofence.GEOFENCE_TRANSITION_EXIT).build());
mGoogleApiClient.connect();
if (mGoogleApiClient.isConnecting()) {
Toast.makeText(this, "connecting", Toast.LENGTH_SHORT).show();
}
if (!mGoogleApiClient.isConnected()) {
Toast.makeText(this, "not connected", Toast.LENGTH_SHORT).show();
return;
}
try {
LocationServices.GeofencingApi.addGeofences(
mGoogleApiClient,
// The GeofenceRequest object.
getGeofencingRequest(),
// A pending intent that that is reused when calling removeGeofences(). This
// pending intent is used to generate an intent when a matched geofence
// transition is observed.
getGeofencePendingIntent()
).setResultCallback(this); // Result processed in onResult().
} catch (SecurityException securityException) {
Log.i("catch","d");
// logSecurityException(securityException);
}
}
private PendingIntent getGeofencePendingIntent() {
// Reuse the PendingIntent if we already have it.
if (mGeofencePendingIntent != null) {
return mGeofencePendingIntent;
}
Intent intent = new Intent(this, GeofenceTransitionsIntentService.class);
// We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when calling
// addGeofences() and removeGeofences().
return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
private GeofencingRequest getGeofencingRequest() {
GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
// The INITIAL_TRIGGER_ENTER flag indicates that geofencing service should trigger a
// GEOFENCE_TRANSITION_ENTER notification when the geofence is added and if the device
// is already inside that geofence.
builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
// Add the geofences to be monitored by geofencing service.
builder.addGeofences(mGeofenceList);
// Return a GeofencingRequest.
return builder.build();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build() ;
}
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
@Override
public void onConnected(Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
@Override
public void onResult(Status status) {
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="hangaar.geofencingtest2" >
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".GeofenceTransitionsIntentService"/>
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
答案 0 :(得分:0)
连接,因为大多数网络进程是异步的,而您的mGoogleApiClient.connect()不会立即发生。您需要使用侦听器来了解GoogleApiClient何时连接。我看到你添加了ConnectionCallbacks和OnConnectionFailedListener接口,但没有根据需要使用它们。连接发生后应执行的代码必须在ConnectionCallbacks.onConnected(Bundle)方法中实现,该方法在您的Activity中重写,如下所示:
@Override
public void onConnected(Bundle bundle) {
Toast.makeText(this, "Connected!", Toast.LENGTH_SHORT).show();
}
同样要激活Geofencing,您需要在GoogleApiclient连接后调用LocationServices.GeofencingApi.addGeofences(GoogleApiClient,GeofencingRequest,PendingIntent),即在onConnected(Bundle)中也是如此。不要忘记为Geofencing结果设置ResultCallback(LocationServices.GeofencingApi.addGeofences(...)。setResultCallback(resultCallback) - 也是侦听器接口)。