我试图使用这个库:https://android-arsenal.com/details/1/3291来获取用户的位置。我认为样本显示的一切,但我的Observable/Subscriber从未触发。
这是我的活动:
public class GpsActivity extends AppCompatActivity/* implements LocationListener*/ {
final android.location.Location[] lastKnown = new android.location.Location[2];
LocationManager manager;
boolean exitLoop = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.no_gps);
ButterKnife.bind(this);
manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
getLocation();
}
private void getLocation() {
//TODO zmienić UI
boolean gpsPermission = checkPermissions();
if (!gpsPermission) {
Log.d(getClass().getSimpleName(), "No GPS permissions!");
//TODO Handle permissions result
requestPemissions();
return;
}
boolean gpsEnabled = checkSettings();
if (!gpsEnabled) {
Log.d(getClass().getSimpleName(), "GPS not enabled!");
//TODO Handle settings result
buildAlertMessageNoGps();
return;
}
subscribeForLocation(25f, this);
}
private void requestPemissions() {
Log.d(getClass().getSimpleName(), "Requesting permissions...");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, IntentHelper.PERMISSION_GPS);
}
Log.d(getClass().getSimpleName(), "Done.");
}
private boolean checkSettings() {
Log.d(getClass().getSimpleName(), "Checking settings...");
return !manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
private boolean checkPermissions() {
Log.d(getClass().getSimpleName(), "Checking permissions...");
return ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;
}
private void buildAlertMessageNoGps() {
Log.d(getClass().getSimpleName(), "Showing GPS settings alert...");
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Your GPS seems to be disabled, do you want to enable it?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), IntentHelper.GPS_SETTINGS);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
public void subscribeForLocation(float accuracy, final Context context) {
Log.d(getClass().getSimpleName(), "Subscribing for updates...");
final LocationRequestBuilder locationRequestBuilder = new LocationRequestBuilder(this);
locationRequestBuilder.addLastLocation(LocationManager.GPS_PROVIDER, new LocationTime(30, TimeUnit.SECONDS), false)
.addRequestLocation(LocationManager.GPS_PROVIDER, new LocationTime(10, TimeUnit.SECONDS))
.create().subscribe(new Subscriber<android.location.Location>() {
@Override
public void onCompleted() {
saveLocation();
}
@Override
public void onError(Throwable e) {
Log.d(getClass().getSimpleName(), e.toString());
}
@Override
public void onNext(android.location.Location location) {
final String name = Thread.currentThread().getName();
Log.d(getClass().getSimpleName(), location != null ? location.toString() : "Location is empty =(");
}
});
}
我从日志中得到的输出是:
D/GpsActivity: Checking permissions...
D/GpsActivity: Checking settings...
D/GpsActivity: Subscribing for updates...
然后它只是坐在那里什么也不做。这是我第一次使用Rx,所以我不确定在这种情况下我应该怎么做。
首先你有一个错误的if语句-双重否定。
boolean gpsEnabled = checkSettings();
if (!gpsEnabled) {
Log.d(getClass().getSimpleName(), "GPS not enabled!");
//TODO Handle settings result
buildAlertMessageNoGps();
return;
}
请记住,在订阅之后,你将只收到一个结果,因为observable会在第一次接收位置后发出onCompleted,因为:
return result.firstOrDefault(defaultLocation)
.observeOn(scheduler);
注意,在用户授予权限之后什么也不会发生,因为如果权限还没有被授予,你正在从函数中执行"return"。您可以在授予权限后通过重写以下方法来处理订阅:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
根据您的需要,您可能希望不断通知当前位置。其中一个解决方案是简单地修改你正在使用的库(更可取的解决方案),并"修复"由Observable发出的onCompleted事件。另一种方法是订阅每段时间:
Observable.interval(5, TimeUnit.SECONDS)
.compose(this.<Long>bindToLifecycle()) //google rx trello github
.subscribe(new Action1<Long>() {
@Override
public void call(Long aLong) {
subscribeForLocation(25f, GpsActivity.this);
}
});