android开机启动一个Service实例

    博客分类:
  • android
  • java

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

Bootbroadcastreveiver代码收藏代码
  1. packagecom.test.kevin;
  2. importandroid.content.BroadcastReceiver;
  3. importandroid.content.Context;
  4. importandroid.content.Intent;
  5. importandroid.util.Log;
  6. publicclassBootBroadcastReveiverextendsBroadcastReceiver{
  7. privatestaticfinalStringTAG="BootBroadcastReveiver";
  8. @Override
  9. publicvoidonReceive(Contextcontext,Intentintent){
  10. if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
  11. IntentbootServiceIntent=newIntent(context,MainActivity.class);
  12. context.startService(bootServiceIntent);
  13. Log.d(TAG,"--------Bootstartservice-------------");
  14. }
  15. }
  16. }

Mainservice代码收藏代码
  1. packagecom.test.kevin;
  2. importjava.util.Timer;
  3. importjava.util.TimerTask;
  4. importandroid.app.Service;
  5. importandroid.content.BroadcastReceiver;
  6. importandroid.content.Context;
  7. importandroid.content.Intent;
  8. importandroid.content.IntentFilter;
  9. importandroid.os.Handler;
  10. importandroid.os.IBinder;
  11. importandroid.os.Message;
  12. importandroid.util.Log;
  13. publicclassMainServiceextendsService{
  14. privatestaticfinalStringTAG="MainService";
  15. privatefinalintCLOSE_ALERTDIALOG=0;
  16. privateinttimecount=0;
  17. privateintbatteryValue=0;
  18. @Override
  19. publicIBinderonBind(Intentintent){
  20. Log.d(TAG,"----onBind-----");
  21. returnnull;
  22. }
  23. @Override
  24. publicvoidonCreate(){
  25. Log.d(TAG,"---------onCreate--------");
  26. registerReceiver(batteryChangedReceiver,newIntentFilter(
  27. Intent.ACTION_BATTERY_CHANGED));
  28. delayCloseController.timer.schedule(delayCloseController,1500,1000);
  29. }
  30. @Override
  31. publicintonStartCommand(Intentintent,intflags,intstartId){
  32. //TODOAuto-generatedmethodstub
  33. returnsuper.onStartCommand(intent,flags,startId);
  34. }
  35. privateBroadcastReceiverbatteryChangedReceiver=newBroadcastReceiver(){
  36. @Override
  37. publicvoidonReceive(Contextcontext,Intentintent){
  38. if(Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())){
  39. intlevel=intent.getIntExtra("level",0);
  40. intscale=intent.getIntExtra("scale",100);
  41. Log.d(TAG,"----------level---"+level);
  42. Log.d(TAG,"----------scale---"+scale);
  43. Log.d(TAG,"----------Total---"+level*100/scale+"%");
  44. batteryValue=level*100/scale;
  45. }
  46. }
  47. };
  48. privateHandlermHandler=newHandler(){
  49. @Override
  50. publicvoidhandleMessage(Messagemsg){
  51. switch(msg.what){
  52. caseCLOSE_ALERTDIALOG:{
  53. if(timecount%2==1){
  54. if(batteryValue<=15){
  55. onPowerLed();
  56. try{
  57. Thread.sleep(300);
  58. }catch(InterruptedExceptione){
  59. //TODOAuto-generatedcatchblock
  60. e.printStackTrace();
  61. }
  62. offPowerLed();
  63. Log.d(TAG,"------PowerLed------");
  64. }
  65. }else{
  66. onWorkLed();
  67. try{
  68. Thread.sleep(300);
  69. }catch(InterruptedExceptione){
  70. //TODOAuto-generatedcatchblock
  71. e.printStackTrace();
  72. }
  73. offWorkLed();
  74. Log.d(TAG,"------WorkLed------");
  75. }
  76. }
  77. timecount++;
  78. break;
  79. default:
  80. break;
  81. }
  82. }
  83. };
  84. privateclassDelayCloseControllerextendsTimerTask{
  85. privateTimertimer=newTimer();
  86. @Override
  87. publicvoidrun(){
  88. MessagemessageFinish=newMessage();
  89. messageFinish.what=CLOSE_ALERTDIALOG;
  90. mHandler.sendMessage(messageFinish);
  91. }
  92. }
  93. privateDelayCloseControllerdelayCloseController=newDelayCloseController();
  94. publicvoidonPowerLed(){
  95. Log.d(TAG,"------------onPowerLed-------------");
  96. };
  97. publicvoidoffPowerLed(){
  98. Log.d(TAG,"------------offPowerLed-------------");
  99. };
  100. publicvoidonWorkLed(){
  101. Log.d(TAG,"------------onWorkLed-------------");
  102. };
  103. publicvoidoffWorkLed(){
  104. Log.d(TAG,"------------offWorkLed-------------");
  105. };
  106. }

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

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

更多相关文章

  1. android全屏设置代码
  2. Android中一些比较好的开源代码项目
  3. 【分享】大量android软件开发资料和实例代码【转】
  4. Android核心技术与实例详解(第2版)
  5. Android Fragment 实例
  6. Eclipse Android 代码自动提示功能
  7. 【Android 应用开发】 Android 相关代码规范 更新中 ...
  8. Android开发常用代码片段(一)

随机推荐

  1. Your Android(安卓)SDK is missing, out
  2. 手机浏览器浏览WebApp弹出的键盘遮盖住文
  3. Android 之 使用Pull 解析xml文件
  4. Android弹窗页面的三种实现方式
  5. Android平台搭建
  6. Android中XML解析-PULL解析
  7. Android 源码 InputManagerService 启动
  8. Android--使用开源vitamio做万能视频播放
  9. OkHttp源码深度解析
  10. android 中判断WiFi是否可用的可靠方法 ,