简体   繁体   中英

Security Exception requesting PARTIAL_WAKE_LOCK

I wrote a library which starts a STICKY Service when the main application calls a specific method. This Service acquires a PowerManager.PARTIAL_WAKE_LOCK during the execution of the onStartCommand() method:

PowerManager powerManager = (PowerManager)getSystemService(POWER_SERVICE);
if(powerManager != null) {
    wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, this.getClass().getName());
    wakeLock.acquire();
}

and the main application requires the WAKE_LOCK permission in its AndroidManifest.xml :

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

<application
    ....>

    <activity android:name=".SplashScreen" android:theme="@style/AppTheme.NoActionBar">
    <intent-filter>
            <action android:name="android.intent.action.MAIN" /
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

some activities...


</application>

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

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

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

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

<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />

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

<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_SMS" />

<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />

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

<uses-permission
    android:name="android.permission.PACKAGE_USAGE_STATS"
    tools:ignore="ProtectedPermissions" />

</manifest>

(yes, there are a lot of permissions....but I need them!).

However, with some devices (eg, samsung SM-G950F, or ASUS_Z017D) I receive from ACRA the following exception

java.lang.SecurityException: Neither user 10182 nor current process has android.permission.WAKE_LOCK.

at the exact point where the Service requires the PARTIAL_WAKE_LOCK . The strange thing is that with other devices (eg, Xiaomi Mi 5, or Nexus 5) everything works fine.

**UPDATE: ** I found that this and other problems related to "missing permissions" (also with missing ACCESS_COARSE_LOCATION and READ_CALENDAR) mainly come from Samsung devices. As far as you know, there are some restrictions or additional security policies on Samsung devices?

In which API version are you testing? In API 26 and newer you should ask user for permissions during the runtime

ActivityCompat.requestPermissions(getContext(), new String[]{Manifest.permission.WAKE_LOCK},1);

then override onRequestPermissionsResult method to get the user response.

check this out

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM