Android位置管理器使用权限

发布于 2024-11-27 13:54:17 字数 1236 浏览 2 评论 0原文

我的 android 项目中有以下代码:

locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
bestProvider = locationManager.getBestProvider(criteria, false);
Location currentLocation = locationManager.getLastKnownLocation(bestProvider);
location = currentLocation.getLatitude() + " " + currentLocation.getLongitude();
MyLocation.setText(location);

我收到 provider == null 错误。我需要使用什么权限?

我的 Android 清单文件是:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.DuckTag"
  android:versionCode="1"
  android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".DuckTagActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>
</manifest>

谢谢

I have the following code in my android project:

locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
bestProvider = locationManager.getBestProvider(criteria, false);
Location currentLocation = locationManager.getLastKnownLocation(bestProvider);
location = currentLocation.getLatitude() + " " + currentLocation.getLongitude();
MyLocation.setText(location);

I am getting an provider == null error. what permissions do I need to use?

My android manifest file is:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.DuckTag"
  android:versionCode="1"
  android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".DuckTagActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>
</manifest>

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

生活了然无味 2024-12-04 13:54:17

以下是您需要添加到清单文件中的内容

基于 GPS 的位置

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

基于网络的位置

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

之前的海报是错误的,如果您想同时使用两者,则只需要 ACCESS_FINE_LOCATION,详细信息如下:http://developer.android.com/guide/topics/location/obtaining-user-location.html

注意:如果您同时使用 NETWORK_PROVIDER 和 GPS_PROVIDER,则
您只需要请求 ACCESS_FINE_LOCATION 权限,因为
它包括两个提供商的许可。 (许可
ACCESS_COARSE_LOCATION 仅包含 NETWORK_PROVIDER 的权限。

Here is what you need to add to your manifest file

GPS-based location

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

Network-based location

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

Previous posters are wrong, if you want to use both, then only ACCESS_FINE_LOCATION is required, as detailed here: http://developer.android.com/guide/topics/location/obtaining-user-location.html

Note: If you are using both NETWORK_PROVIDER and GPS_PROVIDER, then
you need to request only the ACCESS_FINE_LOCATION permission, because
it includes permission for both providers. (Permission for
ACCESS_COARSE_LOCATION includes permission only for NETWORK_PROVIDER.

﹏雨一样淡蓝的深情 2024-12-04 13:54:17

你需要这个

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

You need this

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
森林散布 2024-12-04 13:54:17

以下是您需要添加到清单文件中的内容:

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

用于基于 GPS 的位置或

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

用于基于网络的位置

如果您想构建更通用的应用程序,则可以同时需要这两者。

您好,
史蒂芬

Here is what you need to add to your manifest file :

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

for GPS-based location or

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

for network-based location

You can require both if you want to build a more versatile application.

Greetings,
Stéphane

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文