簡體   English   中英

在后台跟蹤位置

[英]Tracking location in the background

我想在android中實現位置跟蹤但是在一個很小的時間間隔(秒)內,在搜索和閱讀文檔后我發現app在后台時位置訪問有限制,解決方案是使用前台服務,但是我需要另一種合法有效的解決方案,而無需向用戶顯示通知。

你可以使用類似的東西

public class GPSService extends Service {

private LocationListener listener;
private LocationManager locationManager;

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

@SuppressLint("MissingPermission")
@Override
public void onCreate() {
    super.onCreate();
    listener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            Intent i = new Intent("location_update");
            i.putExtra("coordinates",location);
            sendBroadcast(i);

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

            Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(i);
        }
    };
    locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,5000,0,listener);
}

@Override
public void onDestroy() {
    super.onDestroy();
    if (locationManager != null){
        locationManager.removeUpdates(listener);
    }
}
}

並由廣播公司檢索

暫無
暫無

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

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