我在下面有这个类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);
}
}
}
答案 0 :(得分:1)
实际上你不应该将gpsupdatedelay
变量定义为static。不需要将其定义为静态并直接从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);
}
}
}
为了更新活动中的值,只需创建一个Intent,将新值放在new_gps_update_delay
Extra中,然后调用startService()
以便将更新的信息发送到服务:< / p>
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():
如果此服务尚未运行,则将对其进行实例化 开始(如果需要,为它创建一个过程);如果它正在运行那么 它仍然在运行。
每次调用此方法都会导致相应的调用 目标服务的
onStartCommand(Intent, int, int)
方法,用 意图在这里给出。这提供了一种方便的方式来提交作业 服务,而不必绑定并调用其接口。