java - 尝试在 Android 设备上检索位置...适用于模拟器,但不适用于真实设备
问题描述
我的问题:我正在尝试创建一个可以在大学范围内使用的应用程序。它需要不断访问位置。我按照在线教程完成了下面的代码。此代码非常适用于我的 android 模拟器,但不适用于我的设备。我在我的设备上收到了许可请求,但没有任何更新。现在,奇怪的是,如果我在我的设备上使用模拟位置应用程序,这个应用程序确实可以工作。但仅限于模拟位置应用程序。
有人对如何解决这个问题有任何建议吗?(xml文件只是一个文本视图。就像我说的,我只是先测试一下)
package com.example.dylan.locationexample;
import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private LocationManager locationManager;
private LocationListener locationListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Log.d("Location: ", location.toString());
TextView tv = findViewById(R.id.textView);
tv.setText(tv.getText() + Double.toString(location.getLatitude()) + "\t" + Double.toString(location.getLongitude()) + "\n");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);
}else{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
if(ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
}
}
}
解决方案
请确保清单中有以下内容
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
并在您的物理设备上启用 gps。
如果您仍然遇到问题,请检查您手机上该应用程序的权限设置。您可能已拒绝访问位置的权限。
这是我正在使用的代码:
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
com.google.android.gms.location.LocationListener mLocationListener = new com.google.android.gms.location.LocationListener() {
@Override
public void onLocationChanged(final Location location) {
listener.onLocation(location);
}
};
if(location != null) {
listener.onLocation(location);
}else{
locationListener = new android.location.LocationListener() {
@Override
public void onLocationChanged(Location location) {
listener.onLocation(location);
locationManager.removeUpdates(locationListener);
Log.d(TAG,"Location: " + String.valueOf(location.getLongitude()));
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
推荐阅读
- javascript - 在角度的 debouceTime 之后不订阅
- javascript - JS / Node.js If 和 Else 语句问题
- python - 使用 Django 将值(字符串)从 html 输入发送到 python 脚本(views.py)
- ruby - Ruby - 类方法中的问号用法
- node.js - 访问组件外的 cookie 数据
- macos - 调试器可执行文件“/usr/local/bin/gdb”未签名。因此,调试可能无法正常工作
- java - 线程“主”io.micronaut.context.exceptions.DependencyInjectionException 中的异常:无法为类的参数注入值
- spring-boot - 如何在 SpringBootTest 中为 logback 设置环境变量?
- angular - Angular 在部署服务器上刷新页面时抛出 404
- c++ - 一个键多值对的容器,在 Qt 中具有简单的插入和查找功能?