星空网 > 软件开发 > 操作系统

Android—应用程序开机自启

        android开机时候会发送开机广播,我们想要收到广播知道手机开机,才能启动我们的应用程序。

        首先要在配置文件中添加相应权限:

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

        然后新建一个类继承BroadcastReceiver,并且重写onReceive方法,在此方法中添加以下代码:

if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {//接收到的广播是开机广播
            System.out.println("@@@@@@@@@@检测到开机@@@@@@@@@@@@@@@");
            Toast.makeText(context, "开机自动启动", Toast.LENGTH_SHORT).show();
            Intent intent1 = new Intent();
            intent1.setClass(context, ScanActivity.class);
            intent1.addFlags(intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent1);//跳转到自己的程序主界面
        }

        不要忘记在项目清单文件中注册广播:

 <receiver android:name="cn.dhsr.sirelinkscanapp.util.MyReceiver" >
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        注册广播代码添加在application节点下。

        备忘,希望能帮助到大家




原标题:Android—应用程序开机自启

关键词:Android

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

南通到湖南物流专线:https://www.goluckyvip.com/tag/98077.html
江苏越南专线:https://www.goluckyvip.com/tag/98078.html
宁南专线:https://www.goluckyvip.com/tag/98079.html
圣诞节促销:https://www.goluckyvip.com/tag/9808.html
洛南专线:https://www.goluckyvip.com/tag/98080.html
东乡专线:https://www.goluckyvip.com/tag/98081.html
船底顶攻略(船底顶最详细攻略):https://www.vstour.cn/a/411239.html
川藏线自驾游要怎么走才比较划算呢?:https://www.vstour.cn/a/411240.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流