在Android中获取GPS定位
我试图通过Android获取当前的GPS位置,但它不工作,并且显示错误。我还包括了所需的在Android中获取GPS定位,android,gps,android-permissions,Android,Gps,Android Permissions,我试图通过Android获取当前的GPS位置,但它不工作,并且显示错误。我还包括了所需的运行时权限,但它仍然不起作用 MainActivity.java public class MainActivity extends AppCompatActivity implements LocationListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedIn
运行时权限
,但它仍然不起作用
MainActivity.java
public class MainActivity extends AppCompatActivity implements LocationListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_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.
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
}
@Override
public void onLocationChanged(Location location) {
double latitude=location.getLatitude();
double longitude=location.getLongitude();
Log.i("Geo_location","Latitude: "+latitude+" ,Longitude: "+longitude);
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
}
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.shaloin.gps">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
. 请帮忙。谢谢:)您没有正确请求运行时权限
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_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.
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
在这里,当权限未授予时,您明确请求位置更新。但是,在if条件下,您应该做的是,如果未授予权限,则请求权限
if (Build.VERSION.SDK_INT >= 23) { // Marshmallow
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION_REQUEST_CODE);
}
return;
要解决此问题,请剪切locationManager.requestLocationUpdates的行,并将其粘贴到花括号外
然后在if条件内,检查SDK版本,如果高于23,则请求权限
if (Build.VERSION.SDK_INT >= 23) { // Marshmallow
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION_REQUEST_CODE);
}
return;
最后,您需要创建一个覆盖“onRequestPermissionsResult”的方法。
创建方法必须在onCreate方法之外完成
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == LOCATION_PERMISSION_REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// start to find location...
} else { // if permission is not granted
// decide what you want to do if you don't get permissions
}
}
}
最后,您需要为每个权限标识唯一的final int变量,以便根据变量值请求权限。这是在类的开头完成的,在这里定义实例变量
final int LOCATION_PERMISSION_REQUEST_CODE = 1252; //Note that the value 1252 is arbitrary.
您没有正确请求运行时权限
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_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.
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
在这里,当权限未授予时,您明确请求位置更新。但是,在if条件下,您应该做的是,如果未授予权限,则请求权限
if (Build.VERSION.SDK_INT >= 23) { // Marshmallow
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION_REQUEST_CODE);
}
return;
要解决此问题,请剪切locationManager.requestLocationUpdates的行,并将其粘贴到花括号外
然后在if条件内,检查SDK版本,如果高于23,则请求权限
if (Build.VERSION.SDK_INT >= 23) { // Marshmallow
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION_REQUEST_CODE);
}
return;
最后,您需要创建一个覆盖“onRequestPermissionsResult”的方法。
创建方法必须在onCreate方法之外完成
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == LOCATION_PERMISSION_REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// start to find location...
} else { // if permission is not granted
// decide what you want to do if you don't get permissions
}
}
}
最后,您需要为每个权限标识唯一的final int变量,以便根据变量值请求权限。这是在类的开头完成的,在这里定义实例变量
final int LOCATION_PERMISSION_REQUEST_CODE = 1252; //Note that the value 1252 is arbitrary.
您没有请求清单中的ACCESS\u rough\u LOCATION
。因此,从Java中去掉ACCESS\u rough\u位置code。演示如何使用具有运行时权限的LocationManager
。@Commonware我按照您的要求做了,但它仍然显示错误“gps”位置提供程序需要访问\u FINE\u位置权限
您没有在清单中请求访问\u location
。因此,从Java中去掉ACCESS\u rough\u位置code。演示如何使用具有运行时权限的LocationManager
。@Commonware我按照您的要求做了,但它仍然显示错误“gps”位置提供程序需要访问\u FINE\u位置权限
,正如您所说,我已剪切LocationManager.requestLocationUpdates(LocationManager.gps\u提供程序,0,0,this)
并将其粘贴到花括号外,在if(grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_grated)内我应该怎么做{
statement@PritomMazumdar那么,您可以复制locationManager.RequestLocationUpdate(locationManager.GPS\PROVIDER,0,0,这);这样,当授予权限时,gps将更新位置。在其他情况下,您可以返回警报对话框或其他内容。基本上,您可以根据用户是否授予您权限来执行任何操作。如果我复制locationManager.RequestLocationUpdate(LocationManager.GPS_提供程序,0,0,此);
内部if(grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_已授予){
语句,然后我必须再次添加权限
,因为只要我在if
语句中添加行,我就会得到一条红色下划线,说明我必须添加权限调用需要权限,这可能会被用户拒绝:代码应该明确检查权限是否可用(使用checkPermission)或显式处理潜在的安全异常
@PritomMazumdar oops.My bad。只需将其留空,或者您可以记录一些用于调试目的的内容,如“已授予权限”。谢谢,我已经排除了错误,但还有一个帮助,我如何显示纬度和经度?我想显示一条Toast
消息,但我无法确定应该将Toast
放在哪里。您能帮我吗?正如您所说,我已经剪切了locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,this);
并将其粘贴在花括号外,如果(grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_已授予){
statement@PritomMazumdar那么,您可以复制locationManager.RequestLocationUpdate(locationManager.GPS\PROVIDER,0,0,这);这样,当授予权限时,gps将更新位置。在其他情况下,您可以返回警报对话框或其他内容。基本上,您可以根据用户是否授予您权限来执行任何操作。如果我复制locationManager.RequestLocationUpdate(LocationManager.GPS_提供程序,0,0,此);
内部if(grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_已授予){
语句,然后我必须再次添加权限
,因为只要我在if
语句中添加行,我就会得到一条红色下划线,说明我必须添加权限调用需要权限,这可能会被用户拒绝:代码应该明确检查权限是否可用(使用checkPermission)或显式处理潜在的安全异常
@PritomMazumdar oops.My bad。请将其留空,或者您可以记录一些用于调试目的的内容,如“已授予权限”。谢谢