0
私は把握できない問題が発生しました。私は、場所プロバイダのonLocationChanged()メソッド(期待どおりの値を変更しています)に設定されているString型の経度にgetterとsetterを使用していましたが、getServiceはJobServiceのonStartJob()で使用されています。しかし、getterは常にnullですが、setter部分の値は変わりません。JobSchedulerでGetterとSetterが機能しない
public class MyJobService extends JobService
implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener
{
String latitude;
String longitude;
public void setLongitude(String longitude) {
this.longitude = longitude;
}
public String getLongitude() {
return longitude;
}
@Override
public boolean onStartJob(JobParameters jobParameters) {
Toast.makeText(this,"MyJobService.onStartJob()",Toast.LENGTH_SHORT).show();
Log.e("token", "Start Job Called");
setUpLocationClientIfNeeded();
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(10000);
Log.e("longitude", getLongitude() + "zz"); // this value is always null
return false;
}
@Override
public void onLocationChanged(Location location) {
Log.e("token",location.getLatitude()+""+location.getLongitude());
Toast.makeText(this, location.getLatitude()+" "+location.getLongitude()+ "", Toast.LENGTH_SHORT).show();
setLongitude(location.getLongitude() + "");
Log.e("longitude inside location change", getLongitude() + "zz");
// this value keeps changing, however it is not affecting the getLongitude method in onStartJob()
}
}
これは時間がかかりました –
ゲッターとセッターは使用しないでください。その代わりに変数に直接アクセスするには静的変数をパブリックにする必要があります。これは、ドキュメントに記載されているようなパフォーマンスに適しています。 –