Android GoogleAppClient:当服务位于后台时,在状态栏上隐藏GPS图标
我正在使用GoogleAppClient实现位置侦听器服务,但GPS图标始终显示,即使该服务位于后台。当服务在后台时,如何禁用GPS图标 请遵循以下来源: 活动Android GoogleAppClient:当服务位于后台时,在状态栏上隐藏GPS图标,android,android-service,google-api-client,android-gps,android-googleapiclient,Android,Android Service,Google Api Client,Android Gps,Android Googleapiclient,我正在使用GoogleAppClient实现位置侦听器服务,但GPS图标始终显示,即使该服务位于后台。当服务在后台时,如何禁用GPS图标 请遵循以下来源: 活动 public class ShowDistanceActivity extends AppCompatActivity implements ILocationConstants { protected static final String TAG = ShowDistanceActivity.class.getSimpleNam
public class ShowDistanceActivity extends AppCompatActivity implements ILocationConstants {
protected static final String TAG = ShowDistanceActivity.class.getSimpleName();
@Bind(R.id.tvLocationData)
TextView tvLocationData;
@Bind(R.id.toolbar)
Toolbar toolbar;
/**
* Receiver listening to Location updates and updating UI in activity
*/
private LocationReceiver locationReceiver;
/**
* Permission util with callback mechanism to avoid boilerplate code
* <p/>
* https://github.com/kayvannj/PermissionUtil
*/
private PermissionUtil.PermissionRequestObject mBothPermissionRequest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_distance);
ButterKnife.bind(this);
setSupportActionBar(toolbar);
locationReceiver = new LocationReceiver();
}
private void startLocationService() {
Intent serviceIntent = new Intent(this, LocationService.class);
startService(serviceIntent);
}
@Override
protected void onStart() {
super.onStart();
LocalBroadcastManager.getInstance(this).registerReceiver(locationReceiver, new IntentFilter(LOACTION_ACTION));
/**
* Runtime permissions are required on Android M and above to access User's location
*/
if (AppUtils.hasM() && !(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED
&& ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)) {
askPermissions();
} else {
startLocationService();
}
}
/**
* Ask user for permissions to access GPS location on Android M
*/
public void askPermissions() {
mBothPermissionRequest =
PermissionUtil.with(this).request(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION).onResult(
new Func2() {
@Override
protected void call(int requestCode, String[] permissions, int[] grantResults) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
startLocationService();
} else {
Toast.makeText(ShowDistanceActivity.this, R.string.permission_denied, Toast.LENGTH_LONG).show();
}
}
}).ask(PERMISSION_ACCESS_LOCATION_CODE);
}
@Override
protected void onStop() {
super.onStop();
LocalBroadcastManager.getInstance(this).unregisterReceiver(locationReceiver);
}
private class LocationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (null != intent && intent.getAction().equals(LOACTION_ACTION)) {
String locationData = intent.getStringExtra(LOCATION_MESSAGE);
tvLocationData.setText(locationData);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (null != mBothPermissionRequest) {
mBothPermissionRequest.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
清单
Android清单声明
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.technosavy.showmedistance">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".ShowDistanceActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".service.LocationService"
android:enabled="true"
android:exported="true"></service>
</application>
</manifest>
欢迎提供任何帮助。要删除GPS图标,您需要使用缓存和wi-fi位置设置(从内存中)。在代码中进行以下更改 在清单中,删除:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
可以肯定,这将删除GPS图标,但在调用onConnected()
方法时,也只会获得FusedLocationApi.getLastLocation()
值(很可能精度较低)
onLocationChanged()
方法可能永远不会触发,直到另一个应用程序以更高的优先级或更高的精度发出位置请求。新的融合位置提供程序采用的方法与以前的方法略有不同。开发人员现在选择用多少电池电量来计算位置,而不是用哪些设备组件来计算位置。它使用GPS、Wi-Fi、移动网络和车载传感器的任何可用组合来计算位置
LocationRequest优先级设置现在为:
优先级\u无\u电源(被动侦听来自其他客户端的位置更新)
低功率优先级(~10km“城市”精度)
优先级\u平衡\u功率\u精度(~100m“块”精度)
优先级\高精度(以牺牲电池寿命为代价,尽可能精确)
Google在这里描述了LocationRequest类:我相信这是一个安全特性,不能隐藏。很高兴纠正,但这是我所理解的。嗨,跳跳虎!某些应用程序会触发我们的位置,但不显示此图标:(他们使用的是
android.permission的权限设置。使用PRIORITY\u NO\u POWER
或PRIORITY\u LOW\u POWER
访问\u rough\u LOCATION
。如果您只需要用户位置的粗略位置,就可以了。这只会返回用户缓存的位置。如果需要,我会写一个正确的答案。@Tigger,请,我是Tnx很多,我在itHappy上花了2天的时间来帮忙,但是记住,这些设置的位置精度可能会很差。
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
mLocationRequest.setInterval(10000L);
mLocationRequest.setFastestInterval(5000L);
// Do NOT use LocationRequest.PRIORITY_HIGH_ACCURACY here
// Instead use one of the other option.
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
//mLocationRequest.setPriority(LocationRequest.PRIORITY_LOW_POWER);
//mLocationRequest.setPriority(LocationRequest.PRIORITY_NO_POWER);
// Remove setSmallestDisplacement() as it should not be used
// unless you are using a GPS / PRIORITY_HIGH_ACCURACY
//mLocationRequest.setSmallestDisplacement(DISPLACEMENT);