更新我的应用以支持Android 7后,当触发GPS开/关时,不再调用GPS列表器。如果我刷新我的活动,它在Android 6上按预期工作,但在Android 7中没有。有人有任何想法。我已经添加了我的列表器,以及与我的活动中的gps更改相关的代码。
我知道如果一个理论难以超越背压或恢复活动以重新创造观点,但是也没有成功。
GPSListner.java
public abstract class GPSListener implements LocationListener {
private Context context;
public GPSListener(Context context) {
this.context = context;
}
@Override
public void onProviderEnabled(String provider) {
onGPSOn();
}
@Override
public void onProviderDisabled(String provider) {
onGPSOff();
}
public abstract void onGPSOff();
public abstract void onGPSOn();
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
我的班级
gpsListener = new GPSListener(this) {
@Override
public void onGPSOff() {
gpsImg.setImageResource(R.drawable.notok);
}
@Override
public void onGPSOn() {
gpsImg.setImageResource(R.drawable.ok);
}
};
final LocationManager manager;
manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
final ImageView gpsImg = (ImageView) findViewById(R.id.gpsstatus);
if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
gpsImg.setImageResource(R.drawable.ok);
} else {
gpsImg.setImageResource(R.drawable.notok); //not ok
}
最后一种方法打开gps设置。
public View.OnClickListener onButtongpsClick = new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent gpsOptionsIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(gpsOptionsIntent);
}
};
答案 0 :(得分:2)
获取位置本身有点棘手。只有GPS可以有视线问题,并且也会因设备而异,而不仅仅是Android版本。多年来,Android位置服务已经成熟,使用最新的标准做法确实可以提高结果的一致性。
顺便说一下,LocationClient
已被弃用。 FusedLocationProviderApi不再使用它了。
它通过GoogleApiClient工作,此部分使GooglePlayServices成为强制性的。如果这不适合您的应用,您可以选择。
Making your app location aware建议:
Google Play服务位置API优于Android 框架位置API(android.location)作为添加位置的一种方式 了解您的应用。如果您目前正在使用Android 框架位置API,强烈建议您切换到 Google Play尽快提供服务位置API。
你可以把它分成几部分来更好地理解它,比如;
构建GoogleApiClient
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this) //this = activity
.addApi(LocationServices.API)
.addConnectionCallbacks(this) //interfaces implemented
.addOnConnectionFailedListener(this)
.build();
}
申请地点,
// Create the location request
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(UPDATE_INTERVAL)
.setFastestInterval(FASTEST_INTERVAL);
// Request location updates
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
mLocationRequest, this);
如果该要求适合您,请尝试上一个已知位置
@Override
public void onConnected(Bundle bundle) {
Location mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
...}
onConnected()
是来自GoogleApiClient的回调...
因此,除了初始化客户端和实现监听器之外,还有更多的位置。我建议你通过几个问题或android文档来确保你实现适合你的要求。
另外, 而不是
Intent gpsOptionsIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS); ,
使用SettingsApi
可以参考Enabling location mode...
一些有用的Q& As:
答案 1 :(得分:0)
最终的LocationManager经理;
manager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
我甚至要评论这个吗?再读一遍,你会明白它有什么问题。
提示。尝试:
final LocationManager manager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
答案 2 :(得分:0)
您可以使用融合位置api获取位置
融合位置Api: 融合位置提供商根据可用选项自动确定最佳位置,因为它使用GPS和网络提供商。因此,如果设备GPS关闭,我们仍然可以从网络提供商处获取该位置,反之亦然。
为何选择融合位置Api?
请参阅以下获取位置的代码。 LocationService :我们需要这些以获取继续位置,并将这些注册为待处理意图,因此每当设备获得新位置时,这些服务都会调用。
public class LocationService extends IntentService {
private String TAG = this.getClass().getSimpleName();
public LocationService() {
super("Fused Location");
}
public LocationService(String name) {
super("Fused Location");
}
@Override
protected void onHandleIntent(Intent intent) {
Location location = intent.getParcelableExtra(LocationClient.KEY_LOCATION_CHANGED);
if(location !=null){
Log.i(TAG, "onHandleIntent " + location.getLatitude() + "," + location.getLongitude());
// write your code here.
}
}
}
MainActivity:注册回调函数,告诉我们是否与api连接或断开连接。
public class MainActivity extends Activity implements GooglePlayServicesClient.ConnectionCallbacks,GooglePlayServicesClient.OnConnectionFailedListener,LocationListener {
private String TAG = this.getClass().getSimpleName();
private LocationClient locationclient;
private LocationRequest locationrequest;
private Intent mIntentService;
private PendingIntent mPendingIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mIntentService = new Intent(this,LocationService.class);
mPendingIntent = PendingIntent.getService(this, 1, mIntentService, 0);
int resp =GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if(resp == ConnectionResult.SUCCESS){
locationclient = new LocationClient(this,this,this);
locationclient.connect();
}
else{
Toast.makeText(this, "Google Play Service Error " + resp, Toast.LENGTH_LONG).show();
}
}
public void buttonClicked(View v){
if(v.getId() == R.id.btnLastLoc){
if(locationclient!=null && locationclient.isConnected()){
Location loc =locationclient.getLastLocation();
Log.i(TAG, "Last Known Location :" + loc.getLatitude() + "," + loc.getLongitude());
txtLastKnownLoc.setText(loc.getLatitude() + "," + loc.getLongitude());
}
}
if(v.getId() == R.id.btnStartRequest){
if(locationclient!=null && locationclient.isConnected()){
if(((Button)v).getText().equals("Start")){
locationrequest = LocationRequest.create();
locationrequest.setInterval(Long.parseLong(etLocationInterval.getText().toString()));
locationclient.requestLocationUpdates(locationrequest, this);
((Button) v).setText("Stop");
}
else{
locationclient.removeLocationUpdates(this);
((Button) v).setText("Start");
}
}
}
if(v.getId() == R.id.btnRequestLocationIntent){
if(((Button)v).getText().equals("Start")){
locationrequest = LocationRequest.create();
locationrequest.setInterval(100);
locationclient.requestLocationUpdates(locationrequest, mPendingIntent);
((Button) v).setText("Stop");
}
else{
locationclient.removeLocationUpdates(mPendingIntent);
((Button) v).setText("Start");
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if(locationclient!=null)
locationclient.disconnect();
}
@Override
public void onConnected(Bundle connectionHint) {
Log.i(TAG, "onConnected");
txtConnectionStatus.setText("Connection Status : Connected");
}
@Override
public void onDisconnected() {
Log.i(TAG, "onDisconnected");
txtConnectionStatus.setText("Connection Status : Disconnected");
}
@Override
public void onConnectionFailed(ConnectionResult result) {
Log.i(TAG, "onConnectionFailed");
txtConnectionStatus.setText("Connection Status : Fail");
}
@Override
public void onLocationChanged(Location location) {
if(location!=null){
Log.i(TAG, "Location Request :" + location.getLatitude() + "," + location.getLongitude());
}
}
}
有关更多参考,请参阅以下链接
https://github.com/riteshreddyr/fused-location-provider https://github.com/kpbird/fused-location-provider-example
希望这些可以帮助你。