android 开机自动启动

</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 {

	@Override
	public 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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值