BroadCastReceiver(广播接收者)开机启动完成监听例子
2016-02-16 00:00
671 查看
摘要: 一开机就实施广播监听
1、在配置清单里静态注册BroadCastReceiver
代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcastreceiver.boot"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<!-- 添加开机启动完成的权限 -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 静态注册广播接收者(实施监听) -->
<receiver
android:name=".BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
=================
2、BootReceiver类
代码
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "监听到开机完成", Toast.LENGTH_LONG).show();
Intent intent1 = new Intent(context,MainActivity.class);
//必须要添加这个标签 否则启动失败
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//跳转
context.startActivity(intent1);
}
}
1、在配置清单里静态注册BroadCastReceiver
代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcastreceiver.boot"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<!-- 添加开机启动完成的权限 -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 静态注册广播接收者(实施监听) -->
<receiver
android:name=".BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
=================
2、BootReceiver类
代码
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "监听到开机完成", Toast.LENGTH_LONG).show();
Intent intent1 = new Intent(context,MainActivity.class);
//必须要添加这个标签 否则启动失败
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//跳转
context.startActivity(intent1);
}
}
相关文章推荐
- spring boot mybatis mysql
- MariaDB配置默认存储引擎
- MariaDB存储加密
- MariaDB多主集群方案(MariaDB Galera cluster)
- HttpUrlConnection缓存,仿微信朋友圈图片缓存效果
- webrtc学习之旅Getting Started
- 非常全面到位的介绍与源代码地址 :Python 网页爬虫 & 文本处理 & 科学计算 & 机器学习 & 数据挖掘兵器谱
- hdu(1686)——Oulipo
- 【点分治】poj1741
- 阿里2015实习生招聘前端方向--编程题(2)
- BSOJ2764:树中点对统计 点分治
- 【点分治】poj1741
- web log data mining 介紹好用工具:Log Parser (分析多種 Log 格式的超強工具)
- BSOJ2381:捉迷藏 括号序列 线段树维护
- LeetCode 136.137. 260.Single Number ⅠII III
- 静态顺序表
- Visual Studio提示错误无法convert char * to LPCWSTR
- BSOJ3194:黑白树 树链剖分 TLE90分
- BSOJ1125:树Tree 树链剖分 单点修改 区间取反 区间查询
- webmin的安装