为什么locationManager.getBestProvider()返回null? (Nexus 5x) [英] Why does locationManager.getBestProvider() returns null? (Nexus 5x)

查看:200
本文介绍了为什么locationManager.getBestProvider()返回null? (Nexus 5x)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过阅读教程来获取设备(Nexus 5x)的位置.但这行不通,我发现行

I am trying to get device (Nexus 5x) location by following a tutorial. But its not working and I found out, that the line

locationManager.getBestProvider(new Criteria(), false);

总是返回null.

这是我的清单内容:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="lt.wilkas.locationdemo">

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        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>
</manifest>

这是我的onCreate内容:

Here is my onCreate content:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //initialize location manager
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        //get best provider
        provider = locationManager.getBestProvider(new Criteria(), false);

        if (provider == null) {
            Log.i("i", "provider ir null");
        } else {
            Log.i("i", provider);
        }
}

我在做什么不对?

推荐答案

有两种方法可以解决此问题.

There are two ways to solve this issue.

1)在文件"build.gradle"中,可以将targetSdkVersion的值更新为小于23.感谢 Beyka .

1) One could update the value of targetSdkVersion to lower than 23 in a file "build.gradle". Thanks to Beyka.

2)由于Android 6.0的危险"权限不仅应包含在清单文件中,还应在App运行时要求确认(运行时请求权限).仅在获得用户的运行时许可后,才应询问位置提供者.感谢 Prera​​k Sola .

2) Since Android 6.0 "dangerous" permissions should not only be included in the manifest file, but also asked for confirmation while App is running (Requesting Permissions at Run Time). Location provider should be asked only after getting runtime permission from the user. Thanks to Prerak Sola.

这些都是很棒的资源和示例,说明了如何做到这一点:

These are great resources and examples how to do that:

  • https://developer.android.com/training/permissions/requesting.html
  • https://github.com/googlesamples/android-RuntimePermissions/blob/master/Application/src/main/java/com/example/android/system/runtimepermissions/MainActivity.java

这篇关于为什么locationManager.getBestProvider()返回null? (Nexus 5x)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆