For some applications, you will need to have your service up and running when the device is started, without user intervention. Such applications mainly include monitors (telephony, bluetooth, messages, other events). At least this feature is currently allowed by the exaggeratedly restrictive Android permissions policy.

Step 1: First you'll need to create a simple service, defined in Monitor.java:

public class Monitor extends Service { private static final String LOG_TAG = "::Monitor"; @Override public void onCreate() { super.onCreate(); Log.e(LOG_TAG, "Service created."); } @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); Log.e(LOG_TAG, "Service started."); } @Override public void onDestroy() { super.onDestroy(); Log.e(LOG_TAG, "Service destroyed."); } @Override public IBinder onBind(Intent intent) { Log.e(LOG_TAG, "Service bind."); return null; } 

}

Step 2: Next we need to create a Broadcast receiver class, StartAtBootServiceReceiver.java:

 public class StartAtBootServiceReceiver extends BroadcastReceiver { private static final String LOG_TAG=StartAtBootServiceReceiver"; @Override public void onReceive(Context context, Intent intent) { Log.e(LOG_TAG, "onReceive:"); if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) { Intent i = new Intent(); i.setAction("test.package.Monitor"); context.startService(i); } } 

}

Step 3: Finally, your AndroidManifest.xml file must contain the following:

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="test.package.Monitor" android:versionName="1.0" android:versionCode="100" android:installLocation="internalOnly"> <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:anyDensity="true" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission> <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="8"/> <application android:icon="@drawable/icon" android:label="@string/app_name"> <service android:name="test.package.Monitor">** <intent-filter> <action android:name="test.package.Monitor"> </action> </intent-filter> </service> <receiver android:name="test.package.StartAtBootServiceReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"> </action> <category android:name="android.intent.category.HOME"> </category> </intent-filter> </receiver> </application> 

I need to highlight some of the most important aspects, key factors for possible errors in implementation:
1) The permission android.permission.RECEIVE_BOOT_COMPLETED must be provided (in the manifest xml)
2) The installation must be performed in internal storage, not on SDCARD! To enforce this use android:installLocation="internalOnly" in the manifest

更多相关文章

  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 使用多线程下载文件
  2. Android上面TreeView效果
  3. Android之隐式意图(Intent)如何查找匹配
  4. [Com]Java中的串口编程
  5. Android下的Hook初探
  6. Android ANR (转)
  7. Android Handler 消息传递机制
  8. Android老版本项目导入到新版SDK提示错误
  9. Android Service——在子线程中更新UI
  10. Android中弹出对话框的实现