簡體   English   中英

GeofencingApi addGeofences沒有收到通知

[英]GeofencingApi addGeofences doesnt receive notification

我已經創建了GeoFencing的注冊類和一個使用它的Activity Main。 我使用了在此頁面上找到的代碼:[ Android LocationServices.GeofencingApi示例用法這是我的Activity Main:

import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.location.Geofence;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import java.util.ArrayList;
import java.util.List;

public class MapsActivity extends FragmentActivity implements GeofencingRegistererCallbacks{
    private static final String TAG= "GeoFencingTEst-MainActivity";
    private GoogleMap mMap; // Might be null if Google Play services APK is not available.
    private GeofencingRegisterer gRegister;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        setUpMapIfNeeded();
        gRegister= new GeofencingRegisterer(this);
        gRegister.setGeofencingCallback(this);
        List<Geofence> geofenceList= new ArrayList<Geofence>();
        Geofence geofence=new Geofence.Builder()
                .setRequestId("1")
                .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER)
                .setCircularRegion(37.0, -122.0, 10)
                .setExpirationDuration(Geofence.NEVER_EXPIRE)
                .build();
        geofenceList.add(geofence);
        gRegister.registerGeofences(geofenceList);
        //startService(new Intent(this, GeofencingReceiver.class));
        Log.e(TAG, ""+gRegister.getRequestPendingIntent());
    }

@Override
protected void onResume() {
    super.onResume();
    setUpMapIfNeeded();
}

/**
 * Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly
 * installed) and the map has not already been instantiated.. This will ensure that we only ever
 * call {@link #setUpMap()} once when {@link #mMap} is not null.
 * <p/>
 * If it isn't installed {@link SupportMapFragment} (and
 * {@link com.google.android.gms.maps.MapView MapView}) will show a prompt for the user to
 * install/update the Google Play services APK on their device.
 * <p/>
 * A user can return to this FragmentActivity after following the prompt and correctly
 * installing/updating/enabling the Google Play services. Since the FragmentActivity may not
 * have been completely destroyed during this process (it is likely that it would only be
 * stopped or paused), {@link #onCreate(Bundle)} may not be called again so we should call this
 * method in {@link #onResume()} to guarantee that it will be called.
 */
private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
        // Check if we were successful in obtaining the map.
        if (mMap != null) {
            setUpMap();
        }
    }
}

/**
 * This is where we can add markers or lines, add listeners or move the camera. In this case, we
 * just add a marker near Africa.
 * <p/>
 * This should only be called once and when we are sure that {@link #mMap} is not null.
 */
private void setUpMap() {
    mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}

@Override
public void onApiClientConnected() {
    Log.e(TAG,"onApiClientConnected");
}

@Override
public void onApiClientSuspended() {
    Log.e(TAG,"onApiClientSuspended");
}

@Override
public void onApiClientConnectionFailed(ConnectionResult connectionResult) {
    Log.e(TAG,"onApiClientConnectionFailed");
}

@Override
public void onGeofencesRegisteredSuccessful() {
    Log.e(TAG,"onGeofencesRegisteredSuccessful"+gRegister.getRequestPendingIntent());
}

}

當我使用模擬器進入感興趣的區域時,我很難收到通知。 成功完成Geofencing注冊,如logcat中所示:

`
01-10 10:57:46.075: E/it.unical.dimes.tesi.geofencingtest.GeofencingRegisterer(2295): onConnected
01-10 10:57:46.075: E/GeoFencingTEst-MainActivity(2295): onApiClientConnected
01-10 10:57:46.075: E/it.unical.dimes.tesi.geofencingtest.GeofencingRegisterer(2295): Call diverso da null
01-10 10:57:46.075: E/it.unical.dimes.tesi.geofencingtest.GeofencingRegisterer(2295): new pending intent
01-10 10:57:46.077: E/it.unical.dimes.tesi.geofencingtest.GeofencingRegisterer(2295): AddGeofences
01-10 10:57:46.146: D/WifiService(1224): acquireWifiLockLocked: WifiLock{NlpWifiLock type=2 binder=android.os.BinderProxy@38d081bb}
01-10 10:57:46.201: I/ActivityManager(1224): Displayed it.unical.dimes.tesi.geofencingtest/.MapsActivity: +1s4ms
01-10 10:57:46.207: E/it.unical.dimes.tesi.geofencingtest.GeofencingRegisterer(2295): Status:Status{statusCode=SUCCESS, resolution=null}
01-10 10:57:46.207: E/it.unical.dimes.tesi.geofencingtest.GeofencingRegisterer(2295): Status: Successful callback
01-10 10:57:46.207: E/it.unical.dimes.tesi.geofencingtest.GeofencingRegisterer(2295): get request pending intent
01-10 10:57:46.207: E/it.unical.dimes.tesi.geofencingtest.GeofencingRegisterer(2295): pending intent not null
01-10 10:57:46.207: E/GeoFencingTEst-MainActivity(2295): onGeofencesRegisteredSuccessfulPendingIntent{1cc51dc6: android.os.BinderProxy@28ee5bfd}`

我相信問題是該服務器未在后台運行,因為這是日志:

01-10 10:57:56.406: I/CheckinRequestBuilder(1708): Classify the device as Phone.
01-10 10:57:59.727: I/ConfigFetchService(1708): fetch service done; releasing wakelock
01-10 10:57:59.729: I/ActivityManager(1224): Waited long enough for: ServiceRecord{14d38e65 u0 com.google.android.gms/.wearable.service.WearableService}
01-10 10:57:59.730: I/ConfigFetchService(1708): stopping self
01-10 10:57:59.733: I/ConfigService(1656): onDestroy

有沒有人可以使用解決方案或示例代碼?

您是否有服務來處理從進入地理圍欄而觸發的意圖?

嘗試遵循此http://web.archive.org/web/20131203150710/http://developer.android.com/training/location/geofencing.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM