Android 在安卓系统中,如何连续接收GPS定位?
android.location.LocationListener的location listener的onLocationChanged()函数中存在问题 在下面的代码中,在请求LocationManager的RequestLocationUpdate后,onLocationChanged会以不均匀的间隔调用,即我已将period设置为1秒,但不会在每秒钟后收到位置更改Android 在安卓系统中,如何连续接收GPS定位?,android,performance,gps,android-gps,Android,Performance,Gps,Android Gps,android.location.LocationListener的location listener的onLocationChanged()函数中存在问题 在下面的代码中,在请求LocationManager的RequestLocationUpdate后,onLocationChanged会以不均匀的间隔调用,即我已将period设置为1秒,但不会在每秒钟后收到位置更改 import android.content.Context; import android.location.Locati
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
@SuppressWarnings("MissingPermission")
public class SimplePositionProvider extends PositionProvider implements LocationListener {
public SimplePositionProvider(Context context, PositionListener listener) {
super(context, listener);
Log.i("SimplePositionProvider", "start");
if (!type.equals(LocationManager.NETWORK_PROVIDER)) {
type = LocationManager.GPS_PROVIDER;
}
}
public void startUpdates() {
Log.i("startUpdates", "start");
try {
Log.i("requestLocationUpdates", "start");
Log.i("TYPE", type);
locationManager.requestLocationUpdates(type, period, 0, this);
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "error");
}
}
public void stopUpdates() {
Log.i("stopUpdates", "start");
locationManager.removeUpdates(this);
}
@Override
public void onLocationChanged(Location location) {
Log.i("onLocationChanged", "start");
updateLocation(location);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.i("onStatusChanged", "start");
}
@Override
public void onProviderEnabled(String provider) {
Log.i("onProviderEnabled", "start");
}
@Override
public void onProviderDisabled(String provider) {
Log.i("onProviderDisabled", "start");
}
}
},您使用的period
参数是位置更新之间的最小间隔。如果希望在定位系统更新时尽快获取位置,请将period
设置为零。使用此类
public class MyLocationService extends Service {
private static final String TAG = "MyLocationService";
private LocationManager mLocationManager = null;
private static final int LOCATION_INTERVAL = 1000 * 30;;
private static final float LOCATION_DISTANCE = 0f;
private final IBinder serviceBinder = new MyLocationService.RunServiceBinder();
public MyLocationService() {
}
LocationListener[] mLocationListeners = new LocationListener[]{
new LocationListener(LocationManager.GPS_PROVIDER),
};
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand");
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
@Override
public void onCreate() {
Log.e(TAG, "onCreate");
initializeLocationManager();
/* try {
mLocationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
mLocationListeners[1]);
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "network provider does not exist, " + ex.getMessage());
}*/
try {
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
mLocationListeners[0]);
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "gps provider does not exist " + ex.getMessage());
}
}
@Override
public void onDestroy() {
Log.e(TAG, "onDestroy");
super.onDestroy();
if (mLocationManager != null) {
for (int i = 0; i < mLocationListeners.length; i++) {
try {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mLocationManager.removeUpdates(mLocationListeners[i]);
} catch (Exception ex) {
Log.i(TAG, "fail to remove location listener, ignore", ex);
}
}
}
}
private void removeUpdate(){
if (mLocationManager != null) {
for (int i = 0; i < mLocationListeners.length; i++) {
try {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mLocationManager.removeUpdates(mLocationListeners[i]);
} catch (Exception ex) {
Log.i(TAG, "fail to remove location listener, ignore", ex);
}
}
}
}
private void initializeLocationManager() {
Log.e(TAG, "initializeLocationManager - LOCATION_INTERVAL: " + LOCATION_INTERVAL + " LOCATION_DISTANCE: " + LOCATION_DISTANCE);
if (mLocationManager == null) {
mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
// throw new UnsupportedOperationException("Not yet implemented");
return null;
}
public class RunServiceBinder extends Binder {
public MyLocationService getService() {
return MyLocationService.this;
}
}
private class LocationListener implements android.location.LocationListener {
Location mLastLocation;
public LocationListener(String provider) {
Log.e(TAG, "LocationListener " + provider);
// Toast.makeText(MyLocationService.this, ""+provider, Toast.LENGTH_SHORT).show();
mLastLocation = new Location(provider);
}
@Override
public void onLocationChanged(Location location) {
Log.e(TAG, "onLocationChanged: " + location);
// Toast.makeText(MyLocationService.this, ""+"checked", Toast.LENGTH_SHORT).show();
mLastLocation.set(location);
}
@Override
public void onProviderDisabled(String provider) {
Log.e(TAG, "onProviderDisabled: " + provider);
}
@Override
public void onProviderEnabled(String provider) {
Log.e(TAG, "onProviderEnabled: " + provider);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.e(TAG, "onStatusChanged: " + provider);
}
}
}
公共类MyLocationService扩展服务{
私有静态最终字符串TAG=“MyLocationService”;
私有位置管理器mLocationManager=null;
专用静态最终整数位置_间隔=1000*30;;
专用静态最终浮动位置_距离=0f;
private final IBinder serviceBinder=新建MyLocationService.RunServiceBinder();
公共MyLocationService(){
}
LocationListener[]mlLocationListeners=新LocationListener[]{
新LocationListener(LocationManager.GPS_提供程序),
};
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId){
Log.e(标记“onStartCommand”);
super.onStartCommand(intent、flags、startId);
返回开始时间;
}
@凌驾
public void onCreate(){
Log.e(标记为“onCreate”);
初始化ElocationManager();
/*试一试{
mLocationManager.RequestLocationUpdate(
LocationManager.NETWORK_提供程序、位置间隔、位置距离、,
mLocationListeners[1]);
}catch(java.lang.SecurityException ex){
Log.i(标记“无法请求位置更新,忽略”,例如);
}捕获(IllegalArgumentException ex){
Log.d(标记“网络提供商不存在”+ex.getMessage());
}*/
试一试{
mLocationManager.RequestLocationUpdate(
LocationManager.GPS\u提供程序、位置\u间隔、位置\u距离、,
mLocationListeners[0]);
}catch(java.lang.SecurityException ex){
Log.i(标记“无法请求位置更新,忽略”,例如);
}捕获(IllegalArgumentException ex){
Log.d(标记“gps提供程序不存在”+ex.getMessage());
}
}
@凌驾
公共空间{
Log.e(标签“onDestroy”);
super.ondestory();
if(mLocationManager!=null){
for(int i=0;i
根据,您应该使用来请求位置更新。如果内存可用,该解决方案将按指定的时间间隔发送更新。哟
public class MyLocationService extends Service {
private static final String TAG = "MyLocationService";
private LocationManager mLocationManager = null;
private static final int LOCATION_INTERVAL = 1000 * 30;;
private static final float LOCATION_DISTANCE = 0f;
private final IBinder serviceBinder = new MyLocationService.RunServiceBinder();
public MyLocationService() {
}
LocationListener[] mLocationListeners = new LocationListener[]{
new LocationListener(LocationManager.GPS_PROVIDER),
};
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand");
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
@Override
public void onCreate() {
Log.e(TAG, "onCreate");
initializeLocationManager();
/* try {
mLocationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
mLocationListeners[1]);
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "network provider does not exist, " + ex.getMessage());
}*/
try {
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
mLocationListeners[0]);
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "gps provider does not exist " + ex.getMessage());
}
}
@Override
public void onDestroy() {
Log.e(TAG, "onDestroy");
super.onDestroy();
if (mLocationManager != null) {
for (int i = 0; i < mLocationListeners.length; i++) {
try {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mLocationManager.removeUpdates(mLocationListeners[i]);
} catch (Exception ex) {
Log.i(TAG, "fail to remove location listener, ignore", ex);
}
}
}
}
private void removeUpdate(){
if (mLocationManager != null) {
for (int i = 0; i < mLocationListeners.length; i++) {
try {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mLocationManager.removeUpdates(mLocationListeners[i]);
} catch (Exception ex) {
Log.i(TAG, "fail to remove location listener, ignore", ex);
}
}
}
}
private void initializeLocationManager() {
Log.e(TAG, "initializeLocationManager - LOCATION_INTERVAL: " + LOCATION_INTERVAL + " LOCATION_DISTANCE: " + LOCATION_DISTANCE);
if (mLocationManager == null) {
mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
// throw new UnsupportedOperationException("Not yet implemented");
return null;
}
public class RunServiceBinder extends Binder {
public MyLocationService getService() {
return MyLocationService.this;
}
}
private class LocationListener implements android.location.LocationListener {
Location mLastLocation;
public LocationListener(String provider) {
Log.e(TAG, "LocationListener " + provider);
// Toast.makeText(MyLocationService.this, ""+provider, Toast.LENGTH_SHORT).show();
mLastLocation = new Location(provider);
}
@Override
public void onLocationChanged(Location location) {
Log.e(TAG, "onLocationChanged: " + location);
// Toast.makeText(MyLocationService.this, ""+"checked", Toast.LENGTH_SHORT).show();
mLastLocation.set(location);
}
@Override
public void onProviderDisabled(String provider) {
Log.e(TAG, "onProviderDisabled: " + provider);
}
@Override
public void onProviderEnabled(String provider) {
Log.e(TAG, "onProviderEnabled: " + provider);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.e(TAG, "onStatusChanged: " + provider);
}
}
}
Handler handler = new Handler(Looper.myLooper());
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (expectingLocationUpdates) {
restartLocationUpdates();
}
}
}, EXPIRATION_DURATION);