我写了一个库,它Service
在主应用程序调用特定方法时启动 STICKY。该服务PowerManager.PARTIAL_WAKE_LOCK
在方法执行期间获取onStartCommand()
:
PowerManager powerManager = (PowerManager)getSystemService(POWER_SERVICE);
if(powerManager != null) {
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, this.getClass().getName());
wakeLock.acquire();
}
并且主应用程序需要以下WAKE_LOCK
权限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>
(是的,有很多权限......但我需要它们!)。
但是,对于某些设备(例如,三星 SM-G950F 或 ASUS_Z017D),我从 ACRA 收到以下异常
java.lang.SecurityException: Neither user 10182 nor current process has android.permission.WAKE_LOCK.
在服务需要PARTIAL_WAKE_LOCK
. 奇怪的是,使用其他设备(例如,小米米 5 或 Nexus 5)一切正常。
**更新:** 我发现与“缺少权限”(也缺少 ACCESS_COARSE_LOCATION 和 READ_CALENDAR)相关的这个问题和其他问题主要来自三星设备。据您所知,三星设备有一些限制或额外的安全策略吗?