如果应用程序尚未运行,我无法让我的接收器工作(如果它正在运行,则可以正常工作)
我也有同样的问题 USER_UNLOCKED
通过在我的主要活动中运行,当应用程序未以 BOOT_COMPLETED 意图运行时,我设法使接收器工作
override fun onCreate(savedInstanceState: Bundle?) {
...
val pref = getSharedPreferences("allow_notify", MODE_PRIVATE).edit()
pref.apply()
val sp = getSharedPreferences("allow_notify", MODE_PRIVATE)
if (!sp.getBoolean("protected", false)) {
val miuiIntent = Intent().setComponent(
ComponentName(
"com.miui.securitycenter",
"com.miui.permcenter.autostart.AutoStartManagementActivity"
)
)
if (packageManager.resolveActivity(miuiIntent, PackageManager.MATCH_DEFAULT_ONLY) != null) {
startActivity(miuiIntent)
sp.edit().putBoolean("protected", true).apply()
}
}
...
}
现在我的 onCreate 服务正是
override fun onCreate() {
super.onCreate()
val filter = IntentFilter(Intent.ACTION_BOOT_COMPLETED)
filter.addAction(Intent.ACTION_USER_PRESENT)
val mReceiver: BroadcastReceiver = StartupReceiver()
registerReceiver(mReceiver, filter)
}
我的接收器只是
通过在我的主要活动中运行,当应用程序未以 BOOT_COMPLETED 意图运行时,我设法使接收器工作
override fun onCreate(savedInstanceState: Bundle?) {
...
val pref = getSharedPreferences("allow_notify", MODE_PRIVATE).edit()
pref.apply()
val sp = getSharedPreferences("allow_notify", MODE_PRIVATE)
if (!sp.getBoolean("protected", false)) {
val miuiIntent = Intent().setComponent(
ComponentName(
"com.miui.securitycenter",
"com.miui.permcenter.autostart.AutoStartManagementActivity"
)
)
if (packageManager.resolveActivity(miuiIntent, PackageManager.MATCH_DEFAULT_ONLY) != null) {
startActivity(intent)
sp.edit().putBoolean("protected", true).apply()
}
}
...
}
现在我的 onCreate 服务正是
override fun onCreate() {
super.onCreate()
val filter = IntentFilter(Intent.ACTION_BOOT_COMPLETED)
filter.addAction(Intent.ACTION_USER_PRESENT)
val mReceiver: BroadcastReceiver = StartupReceiver()
registerReceiver(mReceiver, filter)
}
我的接收器只是
class StartupReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
MyService.start(context)
}
}
fun start(context: Context){
val manager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
for (service in manager.getRunningServices(Int.MAX_VALUE)) {
if (service.service.shortClassName.contains("MyService")) {
return
}
}
Intent(context, MyService::class.java).also {
context.startService(it)
}
}
<receiver
android:name=".StartupReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.USER_PRESENT"/>
</intent-filter>
</receiver>
在清单中添加权限
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver android:name=".StartupReceiver"
android:directBootAware="true"
android:enabled="true"
android:exported="false"
tools:targetApi="n">
<intent-filter>
<action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
class StartupReceiver: BroadcastReceiver() {
override fun onReceive(p0: Context?, p1: Intent?) {
val action = p1?.action
if (action.equals(Intent.ACTION_BOOT_COMPLETED) || action.equals(Intent.ACTION_LOCKED_BOOT_COMPLETED)) {
// This is executed once the boot is completed
// You may start you foreground service here
}
}
}
注意:从targetSdk 31开始,只有当应用程序位于前台时才能调用Service。您可以使用 WorkManager 来完成预期的任务。