android 开机自动启动

来源:互联网 发布:微信企业宣传制作软件 编辑:程序博客网 时间:2024/04/29 19:16

</pre>初学 android 就是记录自己平时遇到的问题 及解决方案<p></p><p>现在公司需求是在机顶盒上 安装一个 应用   在开机的时候自动开启一个后台服务去做一些事情</p><p>用到了广播处理</p><p>先在Manifest文件中静态注册一个用来监听设备动态的广播</p><p></p><pre code_snippet_id="1585878" snippet_file_name="blog_20160223_2_6989421" name="code" class="java"> <!-- 系统启动完成后会调用 -->        <receiver            android:name="com.example.testghost.bootReceiver"            android:enabled="true"            android:priority="2147483647" >            <intent-filter>                <action android:name="android.intent.action.BOOT_COMPLETED" />//开机广播                <action android:name="android.intent.action.SCREEN_ON" />//屏幕点亮                <action android:name="android.intent.action.SCREEN_OFF" />//屏幕熄灭                <action android:name="android.intent.action.USER_PRESENT" />//用户解锁            </intent-filter>        </receiver>

然后实现这个广播

package com.example.testghost;import com.lidroid.xutils.util.LogUtils;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;import android.widget.Toast;public class bootReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {LogUtils.d("开及" + intent.getAction().toString());switch (intent.getAction()) {case Intent.ACTION_SCREEN_OFF:Toast.makeText(context, "锁屏", 1000).show();LogUtils.d("锁屏");break;case Intent.ACTION_SCREEN_ON:LogUtils.d("亮屏");Toast.makeText(context, "亮屏", 1000).show();break;case Intent.ACTION_USER_PRESENT:LogUtils.d("解锁");startService(context, intent);Toast.makeText(context, "用户解锁", 1000).show();break;case Intent.ACTION_BOOT_COMPLETED:LogUtils.d("开及");startService(context, intent);Toast.makeText(context, "用户开机", 1000).show();break;default:break;}}private void startService(Context context, Intent intent) {Log.d("DMCC", "收到广播 开启服务或者做一些其它的事情" + intent.getAction());}}

0 1