It's possible to register your own application service for starting automatically when the device has been booted. You need this, for example, when you want to receive push events from a http server and want to inform the user as soon a new event occurs. The user doesn't have to start the activity manually before the service get started...

It's quite simple. First give your app the permissionRECEIVE_BOOT_COMPLETED.
Next you need to register a BroadcastReveiver. We call it BootCompletedIntentReceiver.

Your Manifest.xml should now look like this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.jjoe64"> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <application>  <receiver android:name=".BootCompletedIntentReceiver">   <intent-filter>    <action android:name="android.intent.action.BOOT_COMPLETED" />   </intent-filter>  </receiver>  <service android:name=".BackgroundService"/> </application></manifest>

As the last step you have to implement the Receiver. This receiver just starts your background service.


package com.jjoe64;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.SharedPreferences;import android.preference.PreferenceManager;import com.jjoe64.BackgroundService;public class BootCompletedIntentReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) {  if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {   Intent pushIntent = new Intent(context, BackgroundService.class);   context.startService(pushIntent);  } }}



更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Android studio设置activity主题程序崩溃
  2. TabLayout与ViewPager的使用
  3. Android打包jar的两种方法
  4. android的屏幕保持常亮
  5. Android——Handler总结
  6. 【安卓笔记】touch事件的分发和消费机制
  7. Android 短信模块分析(二) MMS中四大组件
  8. Android设备上i-jetty环境的搭建-手机上
  9. Android 中的WiFi学习笔记(转载)
  10. 为什么你的canvas那么慢?浅析Android的can