在开机广播中启动服务


开机广播:在广播中启动服务。

public class BootCompletedReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent it = new Intent(context, ReceiveMsgService.class);
        context.startService(it);
    }
}

在Manifest.xml文件中注册开机广播。

<!-- 开机广播 -->
<receiver android:name=".receiver.BootCompletedReceiver">
    <intent-filter >
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
</receiver>

延伸。关机广播的action。

<action android:name="android.intent.action.ACTION_SHUTDOWN"/>

开机广播需要添加权限。

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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM