我正在尝试通过Google的FusedLocationProviderApi订阅位置更新.我想在后台接收更新,这样即使应用程序被杀,我也会收到更新.尽可能遵循在线文档,我编写了以下代码.注意:这是在intent服务中完成的,而不是在UI线程上完成的,这就是我使用阻塞连接/结果方法的原因.
private void startLocationServices(String deviceId, int pollingInterval) {
Log.i(TAG, "Starting location services with interval: " + pollingInterval + "ms");
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
final PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
wakeLock.acquire();
final GoogleApiClient googleApiClient =
new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.build();
ConnectionResult result = googleApiClient.blockingConnect();
if (!result.isSuccess() || !googleApiClient.isConnected()) {
Log.e(TAG, "Failed to connect to Google Api");
wakeLock.release();
return;
}
LocationRequest locationRequest = new LocationRequest();
locationRequest.setInterval(pollingInterval);
locationRequest.setFastestInterval(10000);
locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
Intent locationIntent = new Intent(this, GeoBroadcastReceiver.class);
locationIntent.putExtra(EXTRA_LOCATION_UPDATE_DEVICE_ID, deviceId);
locationIntent.setAction(GeoBroadcastReceiver.ACTION_LOCATION_UPDATE);
PendingIntent locationPendingIntent = PendingIntent.getBroadcast(
this, …Run Code Online (Sandbox Code Playgroud) 我遇到过两次或三次这种情况,我有一些看起来像这样的代码:
ViewGroup.LayoutParams params = myView.getLayoutParams();
params.height = 240;
myView.requestLayout();
Run Code Online (Sandbox Code Playgroud)
或者:
ViewGroup.LayoutParams params = myView.getLayoutParams();
params.height = 240;
myView.setLayoutParams(params);
Run Code Online (Sandbox Code Playgroud)
视图大小永远不会改变.我试过以下,没有运气:
forceLayout,具有讽刺意味的似乎不那么有力requestLayout,因为它没有通知View的父母需要布局.onMeasure()以查看是否曾被调用(它不是).requestLayout在布局过程中没有进行调用.我也尝试过调用requestLayout所有View的父母,如下所示:
ViewParent parent = myView.getParent();
while (parent != null) {
parent.requestLayout();
parent = parent.getParent();
}
Run Code Online (Sandbox Code Playgroud)
这有效,但看起来真的很酷.我更愿意找到真正的解决方案.
我究竟做错了什么?