2013-05-04 70 views
1

我正在編寫一個Android應用程序(不是小說),它會根據用戶的位置關閉用戶的無線網絡。它將使用網絡的位置獲得大概的位置。我基本上使用谷歌地圖來保存他們創建到我的數據庫的地理柵欄。我很好奇什麼是確保應用程序不必在前臺才能正常工作的最佳方法。Android無線地理柵欄/背景位置查詢

我應該使用addProximityAlert還是別的不同?考慮到我正在創建多個地理柵欄,並且用戶可能已經設置了10個,我擔心爲每個地理圍欄使用不同的鄰近偵聽器。相反,每隔15分鐘左右查詢一次位置信息看起來效率更高,然後讓應用程序確定它是否在任何地理圍欄的合理範圍內。

讓我知道,感謝幫助。

回答

6
+0

那麼整個最後的項目有很多小時花費在後期工作,它被熟成Android。只是我的運氣:)感謝您的建議,但可能會改寫它。 – maschwenk 2013-05-16 21:52:13

+0

你有沒有參考100 geofences的限制?我無法找到有關此限制的任何信息或線索...... – usb79 2013-06-11 16:13:39

+1

@ usb79雖然開發人員表示會對Geofences的限制做出澄清,但在Google I/O中提到了它。他們可能已經將其刪除。我會編輯我的答案。 – Ejmedina 2013-06-11 19:51:25

6

這裏是樣品c頌我寫的,它的工作對我罰款

public class LocationClientService extends Service implements 
     GooglePlayServicesClient.ConnectionCallbacks, 
     GooglePlayServicesClient.OnConnectionFailedListener, 
     LocationClient.OnAddGeofencesResultListener { 

private LocationClient mLocationClient; 
private List<Geofence> mGeofenceLists = new ArrayList<Geofence>(); 

@Override 
public void onCreate() { 
    super.onCreate(); 

    Geofence geofence1 = new Geofence.Builder() 
      .setRequestId("your target place") 
      .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT) 
      .setCircularRegion(0.0, 0.0, 2000.0f) 
      .setExpirationDuration(Geofence.NEVER_EXPIRE) 
      .build(); 

    mGeofenceLists.add(geofence1); 

    mLocationClient = new LocationClient(this, this, this); 
    mLocationClient.connect(); 
} 

@Override 
public IBinder onBind(Intent intent) { 
    return null; 
} 

private PendingIntent getPendingIntent() { 
    Intent intent = new Intent(this, TransitionsIntentService.class); 
    return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
} 

@Override 
public void onConnected(Bundle bundle) { 
    mLocationClient.addGeofences(mGeofenceLists, getPendingIntent(), this); 
} 

@Override 
public void onDisconnected() { 
} 

@Override 
public void onConnectionFailed(ConnectionResult connectionResult) { 
} 

@Override 
public void onDestroy() { 
    mLocationClient.disconnect(); 
    super.onDestroy(); 
} 

@Override 
public void onAddGeofencesResult(int i, String[] strings) { 
    if (LocationStatusCodes.SUCCESS == i) { 
     //todo check geofence status 
    } else { 
    } 
} 

}

然後寫一個IntentService才能收到地理圍欄進入或退出:

public class TransitionsIntentService extends IntentService { 
    public static final String TRANSITION_INTENT_SERVICE = "ReceiveTransitionsIntentService"; 

    public TransitionsIntentService() { 
     super(TRANSITION_INTENT_SERVICE); 
    } 

@Override 
protected void onHandleIntent(Intent intent) { 
    if (LocationClient.hasError(intent)) { 
     //todo error process 
    } else { 
     int transitionType = LocationClient.getGeofenceTransition(intent); 
     if (transitionType == Geofence.GEOFENCE_TRANSITION_ENTER || 
       transitionType == Geofence.GEOFENCE_TRANSITION_EXIT) { 
      List<Geofence> triggerList = LocationClient.getTriggeringGeofences(intent); 
      for (Geofence geofence : triggerList) { 
       Log.i("test", "triggered Id " + geofence.getRequestId()); 
      } 
     } 
     generateNotification(transitionType); 
    } 
} 

    private void generateNotification(int type) { 

    } 
} 
+0

https://github.com/chenjishi/android_location_demo這是一個演示如何使用android提供的新位置API,包括融合位置,地理範圍和活動識別的示例代碼。 – 2013-07-23 17:28:40

1

如果你有興趣,你可以看看這個IO會議以查看功能和限制的概述: https://www.youtube.com/watch?v=Bte_GHuxUGc Geofencing大約23:15

+0

鏈接已死:( – Raul 2015-09-22 20:48:25

+0

@Raul我在YouTube上找到了視頻並更新了鏈接。 – PSIXO 2015-09-23 09:08:14