繁体   English   中英

重启或充电后如何打开应用 api 27+

[英]How to open app after rebooting or charging api 27+

该应用程序应该在重新启动手机后自动启动并且连接充电时,我是这样做的:

显现

<receiver android:name=".ChargingReceiver">
     <intent-filter>
           <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
     </intent-filter>
</receiver>

<receiver
    android:name=".RestartReceiver" >
    <intent-filter>
          <action
                android:name="android.net.conn.CONNECTIVITY_CHANGE"
                tools:ignore="BatteryLife" />
             <action android:name="android.intent.action.BOOT_COMPLETED" />
             <action android:name="android.intent.action.QUICKBOOT_POWERON" />
             <action android:name="com.htc.intent.action.QUICKBOOT_POWERON" />
    </intent-filter>
</receiver>

重启接收器

class RestartReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent?) {
        if (intent!!.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {

            val intent = Intent(context, MainActivity::class.java)
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            context!!.startActivity(intent)
        }
    }
}

充电接收器

class ChargingReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        val intent = Intent(context, MainActivity::class.java)
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        context.startActivity(intent)
    }
}

一切都适用于 Api27+。 我在 Api27+ 上读到您需要使用 JobScheduler,但我找不到有关我的特定任务的信息,这可以在 27+ 上完成吗? 如果是这样,请告诉我如何。

您的代码看起来不错,但要记住能够接收这些广播的事情很少。

  1. 您的应用程序应该有一个通过手动单击应用程序图标至少启动一次的活动。 这使应用程序启用,以便它可以接收广播。

  2. 如果您的应用程序没有用户界面,那么它必须是系统应用程序的一部分,默认情况下启用应用程序。如果应用程序不是系统应用程序,您必须有一种机制通过发送自定义广播或其他方式启用它方法。

如果上述任一条件为真,那么据我所知,只有您可以收到 BOOT COMPLETE 意图。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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