Android permission error

与世无争的帅哥 提交于 2019-12-23 20:05:11

问题


I created an application which enables Bluetooth and discovers other devices. In manifest I have the following:

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

However,on the device there is this exception:

11-20 08:08:47.766: E/AndroidRuntime(9380): FATAL EXCEPTION: main
11-20 08:08:47.766: E/AndroidRuntime(9380): java.lang.SecurityException: Need BLUETOOTH permission: Neither user 10111 nor current process has android.permission.BLUETOOTH.

what else do i have to add to the Manifest so that it works?

This is the manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="cajun.meet"
  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=".CajunMeetActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".MyULCard"
              android:label="@string/app_name">
    </activity>

    <activity android:name=".MyULContacts"
              android:label="@string/app_name">
    </activity>

    <service android:name = ".BluetoothExchange" android:exported="true" android:enabled="true">
    </service>

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

</application>
</manifest>

回答1:


Try moving the permission outside the <application> tag. Like below:

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

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

<application android:icon="@drawable/icon" android:label="@string/app_name">

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

    <activity android:name=".MyULCard"
              android:label="@string/app_name">
    </activity>

    <activity android:name=".MyULContacts"
              android:label="@string/app_name">
    </activity>

    <service android:name = ".BluetoothExchange" android:exported="true" android:enabled="true">
    </service>

</application>
</manifest>



回答2:


Move the uses-permission tags outside of the application element. Uses-permission is a child element of manifest, not application. See the full structure here.




回答3:


Move it outside the application:

    </application>
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
</manifest>

See here in docs that uses-permission is a child of <manifest>




回答4:


if using mashmallow version of android then add this code:

this error occur due to security now in api 23 (mashmallow) version android improved there security they ask for permission

if (android.os.Build.VERSION.SDK_INT >= 23) {
        // only for gingerbread and newer versions
        String permission = Manifest.permission.READ_PHONE_STATE;
        if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.READ_PHONE_STATE}, MY_PERMISSIONS_REQUEST_READ_CONTACTS);
        } else {
            // Add your function here which open camera
        }
    } else {
        // Add your function here which open camera
    }

then add this method onRequestPermissionsResult

    @Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED{ } else { Toast.makeText(getApplication(), "Permission required",Toast.LENGTH_LONG).show(); } return; } }}


来源:https://stackoverflow.com/questions/8202095/android-permission-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!