Android之——监听手机开机事件

转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47028535

本文中,主要通过监听开机广播来达到监听手机开机状态的操作。在Android中能够通过广播机制来监听一些系统服务和系统级的操作,好了,不多说,直接上代码吧

1、创建广播接收者类BootCompleteReceiver

这个类中的回调方法是手机开机后自己主动调用,我在这里仅仅是打印出相关日志,详细的业务逻辑大家能够自己依据自己的详细需求去实现。

代码例如以下:

package cn.lyz.mobilesafe.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

/**
 * 注冊开机广播
 * @author liuyazhuang
 *
 */
public class BootCompleteReceiver extends BroadcastReceiver {
	@Override
	public void onReceive(Context context, Intent intent) {
		Log.i("BootCompleteReceiver", "已经监听到手机开机事件");
	}

}

2、在AndroidManifest.xml中注冊广播

实现例如以下:

<!-- 注冊开机广播 -->
          <receiver android:name="cn.lyz.mobilesafe.receiver.BootCompleteReceiver">
              <intent-filter<span style="color:#FF0000;"> android:priority="1000"</span>>
                 <span style="color:#FF0000;"> <action android:name="android.intent.action.BOOT_COMPLETED"/></span>
              </intent-filter>
          </receiver>
好了,大功告成

原文地址:https://www.cnblogs.com/liguangsunls/p/6819918.html