Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.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
onConnected在Android Geofence上没有响应_Android_Android Geofence - Fatal编程技术网

onConnected在Android Geofence上没有响应

onConnected在Android Geofence上没有响应,android,android-geofence,Android,Android Geofence,当我运行android geofence时,它运行良好。然而,为了在我自己的项目上实现geofence,我尝试自己设置它。首先,我在build.gradle文件和manifest文件中添加了一些代码。然后,我得到了应用程序的SHA1密钥,并将其添加到console.developers.google.com页面中,以创建API密钥。最后,我启用了谷歌地图Android API v2和谷歌游戏Android开发者API。当我运行下面的代码时,即使构建了Google Api客户端,mGoogleAp

当我运行android geofence时,它运行良好。然而,为了在我自己的项目上实现geofence,我尝试自己设置它。首先,我在build.gradle文件和manifest文件中添加了一些代码。然后,我得到了应用程序的SHA1密钥,并将其添加到console.developers.google.com页面中,以创建API密钥。最后,我启用了谷歌地图Android API v2和谷歌游戏Android开发者API。当我运行下面的代码时,即使构建了Google Api客户端,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) {

}
}


连接,因为大多数网络进程都是异步的,您的mgoogleapclient.Connect不会立即发生。您需要使用listener来了解GoogleAppClient何时连接。正如我看到的,您添加了ConnectionCallbacks和OnConnectionFailedListener接口,但没有根据需要使用它们。连接发生后应执行的代码必须在活动中重写的ConnectionCallbacks.onConnectedBundle方法中实现,如下所示:

@Override
public void onConnected(Bundle bundle) {
    Toast.makeText(this, "Connected!", Toast.LENGTH_SHORT).show();
}

此外,要激活地理围栏,您需要在GoogleAppClient连接后调用LocationServices.GeofencingApi.addgeofengesgoogleapiclient、GeofencingRequest、pendingent,也就是在onConnectedBundle中。别忘了为GeoFenging result LocationServices.GeofencingApi.addGeofences…设置ResultCallbackResultCallback-listener接口设置ResultCallback。

您能在清单中发布代码吗?
@Override
public void onConnected(Bundle bundle) {
    Toast.makeText(this, "Connected!", Toast.LENGTH_SHORT).show();
}