安卓学习笔记1——service开机自启动
2014-03-02 16:02
351 查看
例程代码:点击打开链接
本文参考了 好像睡觉.... 的博文点击打开链接
在平时开发中我们可能会遇到需要开机自启动app的某项服务,使其在后台运行。我们在手机刚开机的时候可以查看正在运行的应用,可以看到
比如qq,微信等应用已在后台运行,而这则需要使用android的广播机制来完成。
实现服务的开机自启动主要有三点:
1.创建需要开机自启动的服务,这里为了能便于观察,在该服务中我们呢使其在通知栏打印消息
而该标识即为<intent-filter>中的<action android:name=""> 因此这个标签不能省略,否则无法实现开机自启动。
2.创建广播接收器类,使其extends from BroadcastReceiver
并在AndroidMainfest文件中完成对该receiver的注册,在receiver中priority代表优先级,数字越大,优先级越高,这里的数字代表最大优先级,action标签代表该receiver能够接受到的广播行为。这里的注册方法为静态注册,还可以动态注册receiver,虽然我目前还没有尝试过,据说动态注册的优先级要比静态注册的优先级要高,关于动态注册可以参考博文:点击打开链接
3. 在androidMainfest中加上开机自启动的权限
完成以上三步即可实现开机自启动,已经过真机测试。
本文参考了 好像睡觉.... 的博文点击打开链接
在平时开发中我们可能会遇到需要开机自启动app的某项服务,使其在后台运行。我们在手机刚开机的时候可以查看正在运行的应用,可以看到
比如qq,微信等应用已在后台运行,而这则需要使用android的广播机制来完成。
实现服务的开机自启动主要有三点:
1.创建需要开机自启动的服务,这里为了能便于观察,在该服务中我们呢使其在通知栏打印消息
public class autoBoot extends Service{ private NotificationManager notificationManager=null; private Notification notification = null; @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notification = new Notification(R.drawable.ic_launcher, "服务已经自启动", System.currentTimeMillis()); PendingIntent pendingIntent = PendingIntent.getActivity(autoBoot.this ,0,new Intent(autoBoot.this, MainActivity.class) , 0); notification.setLatestEventInfo(autoBoot.this, "自启动", "服务已经自启动", pendingIntent); notification.flags= Notification.FLAG_AUTO_CANCEL; notification.defaults=Notification.DEFAULT_SOUND; notificationManager.notify(0, notification); super.onCreate(); } }在androidMainfest中完成对该服务的注册,需要注意与普通service不同,需要开机自启动的service必须加上标识,否则接收器中的intent会找不到该服务
而该标识即为<intent-filter>中的<action android:name=""> 因此这个标签不能省略,否则无法实现开机自启动。
<service android:name="com.example.bootstart.autoBoot"> <intent-filter> <action android:name="com.example.bootstart.autoBoot"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </service>
2.创建广播接收器类,使其extends from BroadcastReceiver
public class bootReceiver extends BroadcastReceiver{ @Override public void onReceive(Context arg0, Intent arg1) { Intent newIntent = new Intent("com.example.bootstart.autoBoot"); arg0.startService(newIntent); //启动com.example.bootstart.autoBoot服务 } }
并在AndroidMainfest文件中完成对该receiver的注册,在receiver中priority代表优先级,数字越大,优先级越高,这里的数字代表最大优先级,action标签代表该receiver能够接受到的广播行为。这里的注册方法为静态注册,还可以动态注册receiver,虽然我目前还没有尝试过,据说动态注册的优先级要比静态注册的优先级要高,关于动态注册可以参考博文:点击打开链接
<receiver android:name="com.example.bootstart.bootReceiver"> <intent-filter android:priority="2147483647"> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver>
3. 在androidMainfest中加上开机自启动的权限
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
完成以上三步即可实现开机自启动,已经过真机测试。
好想睡觉……
好想睡觉……
好想睡觉……
相关文章推荐
- NFC学习——NfcService开机自启动
- 安卓学习笔记---IntentService的分析与使用
- 安卓学习笔记之service(一)
- 安卓学习笔记之IntentService
- 安卓学习笔记之service和activity
- [Android新手学习笔记29]-Broadcast-例二广播实习开机自启动
- 教程学习 01-Building a RESTful Web Service 学习笔记
- 步步为营 SharePoint 开发学习笔记系列 九、SharePoint web service 开发(上)
- 安卓学习笔记:2:用EditView创建一个简单的注册页面
- android service 学习笔记
- boost asio io_service学习笔记
- AngularJs学习笔记--Managing Service Dependencies
- Android 学习笔记 Service (一) Local Service
- 个人安卓学习笔记---java.io.IOException: Unable to open sync connection!
- 安卓学习笔记---文件解压带进度条
- 安卓学习笔记-Animations的使用
- 【应用篇】WCF学习笔记(一):Host、Client、MetadataExchage 【应用篇】WCF学习笔记(二):ServiceContract、DataContract
- android开机自启动的后台Service的实现 .
- 安卓学习笔记---ScrollView中嵌套recycleView 出现的不显示,显示不全,终极解决方案
- [学习笔记]认识Android Service