当前位置: 代码迷 >> Android >> android开机起动一个Service实例(转)
  详细解决方案

android开机起动一个Service实例(转)

热度:93   发布时间:2016-05-01 20:49:58.0
android开机启动一个Service实例(转)

android开机启动一个Service实例

    博客分类:?
  • android
  • java
?

android开机启动一个Service研究一下其实也蛮简单,下面直接看代码吧。

Bootbroadcastreveiver代码??收藏代码
  1. package?com.test.kevin;??
  2. ??
  3. ??
  4. import?android.content.BroadcastReceiver;??
  5. import?android.content.Context;??
  6. import?android.content.Intent;??
  7. import?android.util.Log;??
  8. ??
  9. public?class?BootBroadcastReveiver?extends?BroadcastReceiver?{??
  10. ????private?static?final?String?TAG?=?"BootBroadcastReveiver";??
  11. ??
  12. [email protected]
  13. ????public?void?onReceive(Context?context,?Intent?intent)?{??
  14. ????????if?(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))?{??
  15. ????????????Intent?bootServiceIntent?=?new?Intent(context,?MainActivity.class);??
  16. ????????????context.startService(bootServiceIntent);??
  17. ????????????Log.d(TAG,?"--------Boot?start?service-------------");??
  18. ????????}??
  19. ????}??
  20. }??

?

Mainservice代码??收藏代码
  1. package?com.test.kevin;??
  2. ??
  3. import?java.util.Timer;??
  4. import?java.util.TimerTask;??
  5. import?android.app.Service;??
  6. import?android.content.BroadcastReceiver;??
  7. import?android.content.Context;??
  8. import?android.content.Intent;??
  9. import?android.content.IntentFilter;??
  10. import?android.os.Handler;??
  11. import?android.os.IBinder;??
  12. import?android.os.Message;??
  13. import?android.util.Log;??
  14. ??
  15. public?class?MainService?extends?Service?{??
  16. ????private?static?final?String?TAG?=?"MainService";??
  17. ????private?final?int?CLOSE_ALERTDIALOG?=?0;??
  18. ????private?int?timecount?=?0;??
  19. ????private?int?batteryValue?=?0;??
  20. ??
  21. [email protected]
  22. ????public?IBinder?onBind(Intent?intent)?{??
  23. ????????Log.d(TAG,?"----onBind-----");??
  24. ????????return?null;??
  25. ????}??
  26. ??
  27. [email protected]
  28. ????public?void?onCreate()?{??
  29. ????????Log.d(TAG,?"---------onCreate--------");??
  30. ????????registerReceiver(batteryChangedReceiver,?new?IntentFilter(??
  31. ????????????????Intent.ACTION_BATTERY_CHANGED));??
  32. ????????delayCloseController.timer.schedule(delayCloseController,?1500,?1000);??
  33. ????}??
  34. ??
  35. [email protected]
  36. ????public?int?onStartCommand(Intent?intent,?int?flags,?int?startId)?{??
  37. ????????//?TODO?Auto-generated?method?stub??
  38. ????????return?super.onStartCommand(intent,?flags,?startId);??
  39. ????}??
  40. ??
  41. ????private?BroadcastReceiver?batteryChangedReceiver?=?new?BroadcastReceiver()?{?????
  42. [email protected]
  43. ????????public?void?onReceive(Context?context,?Intent?intent)?{?????
  44. ????
  45. ????????????if?(Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction()))?{?????
  46. ????????????????int?level?=?intent.getIntExtra("level",?0);?????
  47. ????????????????int?scale?=?intent.getIntExtra("scale",?100);?????
  48. ????????????????Log.d(TAG,?"----------level---"?+?level);?????
  49. ????????????????Log.d(TAG,?"----------scale---"?+?scale);?????
  50. ????????????????Log.d(TAG,?"----------Total---"?+?level?*?100?/?scale?+?"%");?????
  51. ????????????????batteryValue?=?level?*?100?/?scale;?????
  52. ????????????}?????
  53. ????????}?????
  54. ????
  55. ????};?????
  56. ??
  57. ????private?Handler?mHandler?=?new?Handler()?{?????
  58. [email protected]
  59. ????????public?void?handleMessage(Message?msg)?{?????
  60. ????????????switch?(msg.what)?{?????
  61. ????????????case?CLOSE_ALERTDIALOG:?{?????
  62. ????????????????if?(timecount?%?2?==?1)?{?????
  63. ????????????????????if?(batteryValue?<=?15)?{?????
  64. ????????????????????????onPowerLed();?????
  65. ????????????????????????try?{?????
  66. ????????????????????????????Thread.sleep(300);?????
  67. ????????????????????????}?catch?(InterruptedException?e)?{?????
  68. ????????????????????????????//?TODO?Auto-generated?catch?block?????
  69. ????????????????????????????e.printStackTrace();?????
  70. ????????????????????????}?????
  71. ????????????????????????offPowerLed();?????
  72. ????????????????????????Log.d(TAG,?"------PowerLed------");?????
  73. ????????????????????}?????
  74. ????????????????}?else?{?????
  75. ????????????????????onWorkLed();?????
  76. ????????????????????try?{?????
  77. ????????????????????????Thread.sleep(300);?????
  78. ????????????????????}?catch?(InterruptedException?e)?{?????
  79. ????????????????????????//?TODO?Auto-generated?catch?block?????
  80. ????????????????????????e.printStackTrace();?????
  81. ????????????????????}?????
  82. ????????????????????offWorkLed();?????
  83. ????????????????????Log.d(TAG,?"------WorkLed------");?????
  84. ????????????????}?????
  85. ????????????}?????
  86. ????????????????timecount++;?????
  87. ????????????????break;?????
  88. ????????????default:?????
  89. ????????????????break;?????
  90. ????????????}?????
  91. ????????}?????
  92. ????};?????
  93. ????
  94. ????private?class?DelayCloseController?extends?TimerTask?{?????
  95. ????????private?Timer?timer?=?new?Timer();?????
  96. ????
  97. [email protected]
  98. ????????public?void?run()?{?????
  99. ????????????Message?messageFinish?=?new?Message();?????
  100. ????????????messageFinish.what?=?CLOSE_ALERTDIALOG;?????
  101. ????????????mHandler.sendMessage(messageFinish);?????
  102. ????????}?????
  103. ????}?????
  104. ????
  105. ????private?DelayCloseController?delayCloseController?=?new?DelayCloseController();?????
  106. ????
  107. ????
  108. ????public?void?onPowerLed(){??
  109. ????????Log.d(TAG,?"------------onPowerLed-------------");??
  110. ????};??
  111. ??
  112. ????public?void?offPowerLed(){??
  113. ????????Log.d(TAG,?"------------offPowerLed-------------");??
  114. ????};??
  115. ??
  116. ????public?void?onWorkLed(){??
  117. ????????Log.d(TAG,?"------------onWorkLed-------------");??
  118. ????};??
  119. ??
  120. ????public?void?offWorkLed(){??
  121. ????????Log.d(TAG,?"------------offWorkLed-------------");??
  122. ????};??
  123. ??
  124. }??

?

Androidmanifest.xml代码??收藏代码
  1. <?xml?version="1.0"?encoding="utf-8"?>??
  2. <manifest?xmlns:android="http://schemas.android.com/apk/res/android"??
  3. ????package="com.test.kevin"?android:versionCode="1"?android:versionName="1.0">??
  4. ??
  5. ??
  6. ????<application?android:icon="@drawable/icon"?android:label="@string/app_name">??
  7. ????????<service?android:name=".MainService"?/>??
  8. ??
  9. ????????<receiver?android:name=".BootBroadcastReveiver">??
  10. ????????????<intent-filter>??
  11. ????????????????<action?android:name="android.intent.action.BOOT_COMPLETED"></action>??
  12. ????????????????<category?android:name="android.intent.category.LAUNCHER"?/>??
  13. ????????????</intent-filter>??
  14. ????????</receiver>??
  15. ??
  16. ????</application>??
  17. ????<uses-permission?android:name="android.permission.RECEIVE_BOOT_COMPLETED"?/>??
  18. </manifest>??

?

?? 把以上代码一个小小Service实例,重启机子后可以直接远行.

  相关解决方案