2015-11-10 26 views
0

我的Android清单给出的是这样的:Android Studio中请求许可,即使它在Android清单

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

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

<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> 

当我尝试使用它是Android清单这样的权限:

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); 
    Location location = locationManager.getLastKnownLocation(provider); 

它会告诉我,我需要做的权限检查,像这样:

if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      // TODO: Consider calling 
      // public void requestPermissions(@NonNull String[] permissions, int requestCode) 
      // 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 Activity#requestPermissions for more details. 
      return; 
     } 

我觉得这是斯特拉nge,因为我的Android清单已经允许我这样做..我做错了什么?

我在编辑工作地点Android清单:

Stilla /应用/ src目录/主/ AndroidManifest.xml中

我到底做错了什么?

+0

您是否正在Android 6.0运行您的应用程序? –

+0

并显示你的logcat。 –

+0

我忘记补充说,代码总是进入“if(checkselfpermission ..”,即使我已经在AndroidManifest中给予权限 – Regnav

回答

0

如果您使用的是Android 6.0,请检查您是否在应用程序生命周期的任何给定时间点拥有特定权限。这是因为用户可以随时撤销任何单一权限,并且您的应用程序是而不是通知了此revoken权限。

因此,在使用任何需要权限的API之前,您应该使用已发布的代码段来检查您是否拥有该权限。即使您在清单中拥有该权限,情况也是如此。 看看developer article

+0

好吧,我会试试这个。谢谢! – Regnav

+0

@Regnav如果有帮助,请接受答案。 –

0

随着Android棉花糖的权限系统被更改。一些权限需要在运行时请求。 ACCESS_FINE_LOCATION就是其中之一。请参阅Requesting Permissions at Run Time

+0

请访问:[Android Granular Permissions](http://arc.applause.com/cards/android-6-permissions-faq/)。 [官方文档](https://www.android.com/versions/marshmallow-6-0/) – activesince93