应用自启动
步骤:
1.在清单文件里添加权限
<!-- 添加开机启动完成的权限 -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
2.在代码里写一个广播接受者,并在清单文件中注册。在清单文件中要配置
<receiver
android:name=".TestBootReceiver"
android:enabled="true">
<intent-filter android:priority="100">
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
在广播接收者代码里
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Intent BootService = new Intent(context, bootService.class);
context.startService(bootBootService);
}
就OK了。
灭屏,亮屏功能
和开机启动差不多,都是检测系统发出的广播事件。不过监听亮,灭屏事件的广播不支持静态注册,所以只能在代码里动态注册。
然后在注册的广播接收者写相应的代码。