我在下面有这个类 GPS_Service。它工作完美。在 MainActivity 中,我可以更改变量 GPS_Service.gpsupdatedelay 的值,以调整通过 GPS_Service 中的 requestLocationUpdates 调用 GPS 的频率。在 MainActivity 中,我通过 GPS_Service.gpsupdatedelay = 10000 更改值;我想我还需要调用 GPS_Service 的某些方面来使变量值发生变化?我看不到调用 GPS_Service.onCreate() 的方法,我也不认为我应该这样做吗?
问题:在调整了 gpsupdatedelay 的值后,如何才能使 requestLocationUpdates 参数中的变量值发生变化并实际调整 GPS 频率?
提前谢谢你。
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;
public class GPS_Service extends Service {
private LocationListener listener;
private LocationManager locationManager;
public static Integer gpsupdatedelay;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
Log.e("GPS","onCreate");
listener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Intent i = new Intent("location_update");
//i.putExtra("coordinates",location.getLongitude()+" "+location.getLatitude());
i.putExtra("coordinate1",location.getLatitude());
i.putExtra("coordinate2",location.getLongitude());
Log.e("GPS","sendBroadcast");
sendBroadcast(i);
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
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);
//noinspection MissingPermission
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,gpsupdatedelay,0,listener);
}
@Override
public void onDestroy() {
super.onDestroy();
if(locationManager != null){
//noinspection MissingPermission
locationManager.removeUpdates(listener);
}
}
}
最佳答案
您实际上不应该将 gpsupdatedelay
变量定义为静态。不要将它定义为静态并直接从 Activity 访问它,而是在需要更新它时将新值作为 Intent Extra 发送。
第一步是定义 onStartCommand()
方法覆盖,它将更新新的间隔值,取消注册旧的位置监听器,并使用新的间隔重新注册位置更新:
public class GPS_Service extends Service {
private LocationListener listener;
private LocationManager locationManager;
public Integer gpsupdatedelay = 10000; //NOT static!
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
//Added:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
int newGpsUpdateDelay = intent.getIntExtra("new_gps_update_delay", -99);
if (newGpsUpdateDelay != -99 && locationManager != null) {
gpsupdatedelay = newGpsUpdateDelay;
//noinspection MissingPermission
locationManager.removeUpdates(listener);
//noinspection MissingPermission
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,gpsupdatedelay,0,listener);
}
return START_STICKY;
}
@Override
public void onCreate() {
Log.e("GPS","onCreate");
listener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Intent i = new Intent("location_update");
//i.putExtra("coordinates",location.getLongitude()+" "+location.getLatitude());
i.putExtra("coordinate1",location.getLatitude());
i.putExtra("coordinate2",location.getLongitude());
Log.e("GPS","sendBroadcast");
sendBroadcast(i);
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
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);
//noinspection MissingPermission
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,gpsupdatedelay,0,listener);
}
@Override
public void onDestroy() {
super.onDestroy();
if(locationManager != null){
//noinspection MissingPermission
locationManager.removeUpdates(listener);
}
}
}
为了更新来自 Activity 的值,只需创建一个 Intent,将新值放入 new_gps_update_delay
Extra,然后调用 startService()
以发送服务的更新信息:
int newDelay = 678;
Intent i = new Intent(this, GPS_Service.class);
i.putExtra("new_gps_update_delay", newDelay);
startService(i);
这是有效的,因为即使服务已经在运行,也可以调用 startService()
方法。如果该服务已经在运行,基本上它只是运行 onStartCommand()
方法覆盖。
来自 the documentation for startService() :
If this service is not already running, it will be instantiated and started (creating a process for it if needed); if it is running then it remains running.
Every call to this method will result in a corresponding call to the target service's
onStartCommand(Intent, int, int)
method, with the intent given here. This provides a convenient way to submit jobs to a service without having to bind and call on to its interface.
关于java - Android Java - 在类中更改 requestLocationUpdates 刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40097017/