我是一名windows應用程序員,最近開始學習android。我打算在點擊「開始」按鈕後每隔30秒定期發送一個GPS座標的應用程序,並通過點擊「停止」按鈕(我已經開發了一個接受數據的Web服務)停止這樣做。在Windows中,我會使用一個計時器,並在每個「勾號」將找到GPS座標併發送它。請幫助理解如何在android中完成類似的事情。定期發送GPS座標
0
A
回答
0
請檢查我在我的應用程序中的使用方式及其完美。我使用融合api獲取更新位置請按照以下步驟操作。
第1步。這個類 GoogleLocationService.java
public class GoogleLocationService {
private GoogleServicesCallbacks callbacks = new GoogleServicesCallbacks();
LocationUpdateListener locationUpdateListener;
Context activity;
protected GoogleApiClient mGoogleApiClient;
protected LocationRequest mLocationRequest;
public static final long UPDATE_INTERVAL_IN_MILLISECONDS = 30000;
public GoogleLocationService(Context activity, LocationUpdateListener locationUpdateListener) {
this.locationUpdateListener = locationUpdateListener;
this.activity = activity;
buildGoogleApiClient();
}
protected synchronized void buildGoogleApiClient() {
//Log.i(TAG, "Building GoogleApiClient");
mGoogleApiClient = new GoogleApiClient.Builder(activity)
.addConnectionCallbacks(callbacks)
.addOnConnectionFailedListener(callbacks)
.addApi(LocationServices.API)
.build();
createLocationRequest();
mGoogleApiClient.connect();
}
protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
private class GoogleServicesCallbacks implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
@Override
public void onConnected(Bundle bundle) {
startLocationUpdates();
}
@Override
public void onConnectionSuspended(int i) {
mGoogleApiClient.connect();
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
if (connectionResult.getErrorCode() == ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED) {
Toast.makeText(activity, "Google play service not updated", Toast.LENGTH_LONG).show();
}
locationUpdateListener.cannotReceiveLocationUpdates();
}
@Override
public void onLocationChanged(Location location) {
if (location.hasAccuracy()) {
if (location.getAccuracy() < 30) {
locationUpdateListener.updateLocation(location);
}
}
}
}
private static boolean locationEnabled(Context context) {
boolean gps_enabled = false;
LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
try {
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {
ex.printStackTrace();
}
return gps_enabled;
}
private boolean servicesConnected(Context context) {
return isPackageInstalled(GooglePlayServicesUtil.GOOGLE_PLAY_STORE_PACKAGE, context);
}
private boolean isPackageInstalled(String packagename, Context context) {
PackageManager pm = context.getPackageManager();
try {
pm.getPackageInfo(packagename, PackageManager.GET_ACTIVITIES);
return true;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return false;
}
}
public void startUpdates() {
/*
* Connect the client. Don't re-start any requests here; instead, wait
* for onResume()
*/
if (servicesConnected(activity)) {
if (locationEnabled(activity)) {
locationUpdateListener.canReceiveLocationUpdates();
startLocationUpdates();
} else {
locationUpdateListener.cannotReceiveLocationUpdates();
Toast.makeText(activity, "Unable to get your location.Please turn on your device Gps", Toast.LENGTH_LONG).show();
}
} else {
locationUpdateListener.cannotReceiveLocationUpdates();
Toast.makeText(activity, "Google play service not available", Toast.LENGTH_LONG).show();
}
}
//stop location updates
public void stopUpdates() {
stopLocationUpdates();
}
//start location updates
private void startLocationUpdates() {
if (checkSelfPermission(activity, ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(activity, ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
if (mGoogleApiClient.isConnected()) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, callbacks);
}
}
public void stopLocationUpdates() {
if (mGoogleApiClient.isConnected()) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, callbacks);
}
}
public void startGoogleApi() {
mGoogleApiClient.connect();
}
public void closeGoogleApi() {
mGoogleApiClient.disconnect();
}
}
第二步。使此接口 LocationUpdateListener.java
public interface LocationUpdateListener {
/**
* Called immediately the service starts if the service can obtain location
*/
void canReceiveLocationUpdates();
/**
* Called immediately the service tries to start if it cannot obtain location - eg the user has disabled wireless and
*/
void cannotReceiveLocationUpdates();
/**
* Called whenever the location has changed (at least non-trivially)
* @param location
*/
void updateLocation(Location location);
/**
* Called when GoogleLocationServices detects that the device has moved to a new location.
* @param localityName The name of the locality (somewhere below street but above area).
*/
void updateLocationName(String localityName, Location location);
}
步驟3.調用此對您的OnCreate
私人GoogleLocationService googleLocationService;
googleLocationService = new GoogleLocationService(context, new LocationUpdateListener() {
@Override
public void canReceiveLocationUpdates() {
}
@Override
public void cannotReceiveLocationUpdates() {
}
//update location to our servers for tracking purpose
@Override
public void updateLocation(Location location) {
if (location != null) {
Timber.e("updated location %1$s %2$s", location.getLatitude(), location.getLongitude());
}
}
@Override
public void updateLocationName(String localityName, Location location) {
googleLocationService.stopLocationUpdates();
}
});
googleLocationService.startUpdates();
and call this onDestroy
if (googleLocationService != null) {
googleLocationService.stopLocationUpdates();
}
希望這會幫助你解決你的問題。
0
看一看RxGpsService(使用RxJava檢索GPS位置和路由統計信息的Android服務)。它檢索一個包含當前速度,距離,時間和航點的RouteStats對象。當您停止計時時,您也可以播放/停止計時器以放棄位置。
相關問題
- 1. Java Android GPS發送座標
- 2. 通過流發送Android GPS座標
- 3. 發送GPS座標XML到服務器
- 4. 從OEM GPS模塊發送GPS座標到服務器
- 5. QuickTime GPS座標
- 6. gps座標+米
- 7. shell腳本發送gps座標給模擬器?
- 8. 黑莓應用程序(發送GPS座標到服務器...)
- 9. 如何將GPS座標發送到Web服務器?
- 10. 如何發送經緯度(GPS座標)到服務器
- 11. 獲取/發送短信時如何獲取GPS座標?
- 12. 在輔助線程中訪問併發送GPS座標
- 13. 如何將GPS座標發送到另一個類
- 14. 通過使用JSON發送GPS座標到服務器
- 15. 移動GPS座標
- 16. GPS座標偏差
- 17. GPS座標精度
- 18. 查詢GPS座標
- 19. 從GPS座標列表中確定最大和最小GPS座標
- 20. GPS固定時獲取座標 - Android
- 21. 獲取GPS地圖的GPS座標
- 22. 沒有GPS模塊獲得GPS座標
- 23. OnLocationChange發送座標到Firebase
- 24. GPS座標MySQL查詢
- 25. android-get最新gps座標
- 26. 計算GPS座標半徑
- 27. 三角測量GPS座標
- 28. GPS座標不被檢索
- 29. Street to GPS座標列表
- 30. 訪問iPhone的GPS座標
你究竟在哪裏開始和停止? –
你可以看第3步 – Saveen
在Windows XNA的工作時間,但在Android中,你必須使用它的報警管理器。檢查它。 –