私はこのクラスのGPS_Serviceを以下に用意しています。それは完璧に働いています。 MainActivityからGPS_Service.gpsupdatedelay変数の値を変更して、GPS_ServiceのrequestLocationUpdates経由でGPSが呼び出される頻度を調整できます。 MainActivityから、GPS_Service.gpsupdatedelay = 10000で値を変更します。私は、変数値の変更を行うためにGPS_Serviceのいくつかの側面を呼び出す必要があると思いますか?私はGPS_Service.onCreate()を呼び出す方法を見ることができないし、私はそれを行うべきだとも思わない?Android Java - change requestLocationUpdates refresh in class
質問: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);
}
}
}
ありがとう:the documentation for startService()から
。それは有り難いです。上のコードで変数を更新する簡単な質問。現在位置情報サービスが開始されています。したがって、startService(i)を使用すると、ロケーションサービスが再度開始されて複製されるか、またはstartService(i)によって既存の/実行中のロケーションサービスが停止/再開されるか、startServuce(i)ロケーションサービスの実行には何の影響もありませんか?あなたはおそらく、私はAndroid/Javaには新しいことを伝えることができます。 –
もう一度お世話になります - 推奨された変更でクラスを更新し、MainActivityからのクラスの呼び出しを調整した後、私はこのエラーを受け取りました: "E/AndroidRuntime:FATAL EXCEPTION:main プロセス:com.example.ftonyan.gps4、PID:15469 java.lang.RuntimeException:サービスを作成できませんcom.example.ftonyan.gps6.GPS_Service:java.lang.NullPointerException:nullオブジェクト参照で仮想メソッド 'int java.lang.Integer.intValue()'を呼び出そうとしています android.app.ActivityThread.handleCreateService(ActivityThread.java:3158) " –
私は助けてくれて、私が間違って何をしたのか疑問に思っています。 –