Android Java-在类中更改请求位置更新刷新
我有下面这类GPS\U服务。它工作得很好。从MainActivity,我可以更改变量GPS_Service.gpsupdatedelay的值,以调整通过GPS_服务中的RequestLocationUpdate调用GPS的频率。从MainActivity I通过GPS_Service.gpsupdatedelay=10000更改值;我想我还需要调用GPS_服务的某些方面来进行变量值更改?我看不到调用GPS_Service.onCreate()的方法,我也不认为应该这样做 问题:在调整了gpsupdatedelay的值之后,我如何才能获得RequestLocationUpdate参数中的可变值变化,并实际调整GPS频率 先谢谢你Android Java-在类中更改请求位置更新刷新,java,android,class,gps,Java,Android,Class,Gps,我有下面这类GPS\U服务。它工作得很好。从MainActivity,我可以更改变量GPS_Service.gpsupdatedelay的值,以调整通过GPS_服务中的RequestLocationUpdate调用GPS的频率。从MainActivity I通过GPS_Service.gpsupdatedelay=10000更改值;我想我还需要调用GPS_服务的某些方面来进行变量值更改?我看不到调用GPS_Service.onCreate()的方法,我也不认为应该这样做 问题:在调整了gpsup
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
变量定义为静态变量。不要将其定义为静态并直接从活动访问它,而是在需要更新时将新值作为额外的意图发送
第一步是定义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);
}
}
}
要更新活动中的值,只需创建一个意图,将新值放入new\u gps\u update\u 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()
方法覆盖
发件人:
如果此服务尚未运行,将对其进行实例化并
已启动(如果需要,为其创建流程);如果它正在运行,那么
它仍在运行
对该方法的每次调用都将导致对
目标服务的onStartCommand(Intent,int,int)
方法
这里给出了意图。这提供了一种将作业提交给
服务,而无需绑定和调用其接口
谢谢你的快速回复。我很感激。使用上面的代码快速提问以更新变量。定位服务今天开始。那么,使用startService(i)是否会导致位置服务再次启动和复制,或者startService(i)是否会导致现有/正在运行的位置服务停止/重新启动,或者startServuce(i)是否只是传入变量而根本不影响位置服务的运行?您可能知道,我是Android/Java新手。再次感谢-在使用建议的更改更新类并调整MainActivity对类的调用后,我收到以下错误:“E/AndroidRuntime:致命异常:主进程:com.example.ftonyan.gps4,PID:15469 java.lang.RuntimeException:无法创建服务com.example.ftonyan.gps6.GPS_服务:java.lang.NullPointerException:尝试调用虚拟方法'int java.lang.Integer.intValue()”在android.app.ActivityThread.handleCreateService(ActivityThread.java:3158)上的空对象引用上“我很感激你的帮助,只是想知道我做错了什么。@FrankZappa看起来你已经没有初始值了。看看我刚刚做的编辑:
public Integer gpsupdatedelay=10000代码>丹尼尔-谢谢。你是对的。它是在没有初始延迟值的情况下启动的。现在它编译并运行。我正在测试,以确定它是否会根据编程事件调整延迟。非常感谢你的帮助。