android - 当 Android Oreo 版本中的堆栈中的应用程序进入暂停或停止时,LcoationChanged 未调用
问题描述
因为我在我的活动中使用 googleapiclient 来获取位置,但是当应用程序处于前台时意味着 onlocationchanged 方法正在工作,所以我正在获取位置。当应用程序处于暂停或停止状态时意味着位置没有进入 android Oreo 版本。当活动进入破坏状态时意味着我们不会获取位置,因为在后台,当应用程序处于暂停或停止状态时意味着它应该去其他活动或堆栈中。
服务仅在应用程序进入睡眠模式时不会调用,但活动需要在暂停或停止状态时工作,对吗?但它在我的应用程序中不起作用。请帮我解决我的问题。
public class UserHomePage extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, View.OnClickListener,GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {
Thread t;
private Location mylocation;
private GoogleApiClient googleApiClient;
private final static int REQUEST_CHECK_SETTINGS_GPS=0x1;
private final static int REQUEST_ID_MULTIPLE_PERMISSIONS=0x2;
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
private RequestQueue requestQueue,requestQueue1,requestQueue2,requestQueue3,requestQueue4;
String gs_var_fcm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_activity_home);
ctx=this;
setUpGClient();
mapboxToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mapboxToolbar);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
protected void onPause() {
super.onPause();
Log.i("WW","--onPause");
if (mGoogleApiClient != null) {
mGoogleApiClient.connect();
}
}
@Override
protected void onStart() {
super.onStart();
if (mGoogleApiClient != null) {
mGoogleApiClient.connect();
}
}
@Override
protected void onStop() {
super.onStop();
Log.i("WW","--onStop");
if (mGoogleApiClient != null) {
mGoogleApiClient.connect();
}
}
@Override
protected void onResume() {
super.onResume();
if (mGoogleApiClient != null) {
mGoogleApiClient.connect();
}
}
private synchronized void setUpGClient() {
googleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, 0, this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
googleApiClient.connect();
}
@Override
public void onLocationChanged(Location location) {
mylocation = location;
if (mylocation != null) {
Log.i("QES","onlocation"+mylocation);
}
}
@Override
public void onConnected(Bundle bundle) {
checkPermissions();
}
@Override
public void onConnectionSuspended(int i) {
//Do whatever you need
//You can display a message here
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
//You can display a message here
}
private void getMyLocation(){
if(googleApiClient!=null) {
if (googleApiClient.isConnected()) {
int permissionLocation = ContextCompat.checkSelfPermission(UserHomePage.this,
Manifest.permission.ACCESS_FINE_LOCATION);
if (permissionLocation == PackageManager.PERMISSION_GRANTED) {
mylocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
LocationRequest locationRequest = new LocationRequest();
locationRequest.setInterval(3000);
locationRequest.setFastestInterval(3000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
LocationServices.FusedLocationApi
.requestLocationUpdates(googleApiClient, locationRequest, this);
PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi
.checkLocationSettings(googleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
// All location settings are satisfied.
// You can initialize location requests here.
int permissionLocation = ContextCompat
.checkSelfPermission(UserHomePage.this,
Manifest.permission.ACCESS_FINE_LOCATION);
if (permissionLocation == PackageManager.PERMISSION_GRANTED) {
mylocation = LocationServices.FusedLocationApi
.getLastLocation(googleApiClient);
if(mylocation!=null)
{
Log.i("QES","permissionlocation"+mylocation);
// Toast.makeText(UserHomePage.this, "mylocation"+mylocation, Toast.LENGTH_SHORT).show();
gs_str_latitude = String.valueOf(mylocation.getLatitude());
gs_str_Longitude = String.valueOf(mylocation.getLongitude());
sharedPreferences = getSharedPreferences("myprefer", MODE_PRIVATE);
editor = sharedPreferences.edit();
editor.putString("dest_Lat", gs_str_latitude);
editor.putString("dest_Long", gs_str_Longitude);
editor.putString("Lat", gs_str_latitude);
editor.putString("Long", gs_str_Longitude);
editor.commit();
GS_UserUpdateLocation(gs_str_latitude, gs_str_Longitude, strlevel, Date, networkinformation);
}
else
{
// Toast.makeText(UserHomePage.this, "mylocation", Toast.LENGTH_SHORT).show();
}
}
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied.
// But could be fixed by showing the user a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
// Ask to turn on GPS automatically
status.startResolutionForResult(UserHomePage.this,
REQUEST_CHECK_SETTINGS_GPS);
} catch (IntentSender.SendIntentException e) {
// Ignore the error.
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// Location settings are not satisfied.
// However, we have no way
// to fix the
// settings so we won't show the dialog.
// finish();
break;
}
}
});
}
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_CHECK_SETTINGS_GPS:
switch (resultCode) {
case Activity.RESULT_OK:
getMyLocation();
break;
case Activity.RESULT_CANCELED:
finish();
break;
}
break;
}
}
private void checkPermissions(){
int permissionLocation = ContextCompat.checkSelfPermission(UserHomePage.this,
android.Manifest.permission.ACCESS_FINE_LOCATION);
List<String> listPermissionsNeeded = new ArrayList<>();
if (permissionLocation != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(android.Manifest.permission.ACCESS_FINE_LOCATION);
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(this,
listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), REQUEST_ID_MULTIPLE_PERMISSIONS);
}
}else{
getMyLocation();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
int permissionLocation = ContextCompat.checkSelfPermission(UserHomePage.this,
Manifest.permission.ACCESS_FINE_LOCATION);
if (permissionLocation == PackageManager.PERMISSION_GRANTED) {
getMyLocation();
}
}
注意:即使我只在暂停、停止、恢复、启动状态时才连接 googleapiclient,尽管它没有显示。请帮我
解决方案
从奥利奥开始,谷歌增加了应用程序在后台时获取位置的限制。您可以在此处阅读Oreo 中引入的更改。
您可以做的是为位置更新创建一个前台服务,以便当您的应用程序进入后台时,您可以正常接收位置更新。
推荐阅读
- arrays - 在 IF 语句中添加 OR 函数
- python - Seaborn 热图子图 - 共享热条和加热
- spring-boot - 如何在具有4个微服务、eureka服务器和API网关的架构上实现springboot中基于角色的方法级授权?
- ghostscript - 如何为 DEVICE=tiffsep1 设置像素级墨水限制
- python - Python连接两个具有共同第一个索引的列表
- vue.js - 如何在vuejs中使用jspdf在PDF文件上显示svg图像
- android - Google Mobile Vision ocrlib.so 文件下载错误
- c# - 从 Raspberry Pi 连接到 SQL Server 导致错误 35(在登录前握手期间)
- image - 灰度图像而不是 RGB 的阴影
- android - ML Kit 文本识别 + 西里尔文