android开机启动一个Service实例
- 博客分类:?
- android
- java
?
android开机启动一个Service研究一下其实也蛮简单,下面直接看代码吧。
- package?com.test.kevin;??
- ??
- ??
- import?android.content.BroadcastReceiver;??
- import?android.content.Context;??
- import?android.content.Intent;??
- import?android.util.Log;??
- ??
- public?class?BootBroadcastReveiver?extends?BroadcastReceiver?{??
- ????private?static?final?String?TAG?=?"BootBroadcastReveiver";??
- ??
- [email protected]
- ????public?void?onReceive(Context?context,?Intent?intent)?{??
- ????????if?(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))?{??
- ????????????Intent?bootServiceIntent?=?new?Intent(context,?MainActivity.class);??
- ????????????context.startService(bootServiceIntent);??
- ????????????Log.d(TAG,?"--------Boot?start?service-------------");??
- ????????}??
- ????}??
- }??
?
- package?com.test.kevin;??
- ??
- import?java.util.Timer;??
- import?java.util.TimerTask;??
- import?android.app.Service;??
- import?android.content.BroadcastReceiver;??
- import?android.content.Context;??
- import?android.content.Intent;??
- import?android.content.IntentFilter;??
- import?android.os.Handler;??
- import?android.os.IBinder;??
- import?android.os.Message;??
- import?android.util.Log;??
- ??
- public?class?MainService?extends?Service?{??
- ????private?static?final?String?TAG?=?"MainService";??
- ????private?final?int?CLOSE_ALERTDIALOG?=?0;??
- ????private?int?timecount?=?0;??
- ????private?int?batteryValue?=?0;??
- ??
- [email protected]
- ????public?IBinder?onBind(Intent?intent)?{??
- ????????Log.d(TAG,?"----onBind-----");??
- ????????return?null;??
- ????}??
- ??
- [email protected]
- ????public?void?onCreate()?{??
- ????????Log.d(TAG,?"---------onCreate--------");??
- ????????registerReceiver(batteryChangedReceiver,?new?IntentFilter(??
- ????????????????Intent.ACTION_BATTERY_CHANGED));??
- ????????delayCloseController.timer.schedule(delayCloseController,?1500,?1000);??
- ????}??
- ??
- [email protected]
- ????public?int?onStartCommand(Intent?intent,?int?flags,?int?startId)?{??
- ????????//?TODO?Auto-generated?method?stub??
- ????????return?super.onStartCommand(intent,?flags,?startId);??
- ????}??
- ??
- ????private?BroadcastReceiver?batteryChangedReceiver?=?new?BroadcastReceiver()?{?????
- [email protected]
- ????????public?void?onReceive(Context?context,?Intent?intent)?{?????
- ????
- ????????????if?(Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction()))?{?????
- ????????????????int?level?=?intent.getIntExtra("level",?0);?????
- ????????????????int?scale?=?intent.getIntExtra("scale",?100);?????
- ????????????????Log.d(TAG,?"----------level---"?+?level);?????
- ????????????????Log.d(TAG,?"----------scale---"?+?scale);?????
- ????????????????Log.d(TAG,?"----------Total---"?+?level?*?100?/?scale?+?"%");?????
- ????????????????batteryValue?=?level?*?100?/?scale;?????
- ????????????}?????
- ????????}?????
- ????
- ????};?????
- ??
- ????private?Handler?mHandler?=?new?Handler()?{?????
- [email protected]
- ????????public?void?handleMessage(Message?msg)?{?????
- ????????????switch?(msg.what)?{?????
- ????????????case?CLOSE_ALERTDIALOG:?{?????
- ????????????????if?(timecount?%?2?==?1)?{?????
- ????????????????????if?(batteryValue?<=?15)?{?????
- ????????????????????????onPowerLed();?????
- ????????????????????????try?{?????
- ????????????????????????????Thread.sleep(300);?????
- ????????????????????????}?catch?(InterruptedException?e)?{?????
- ????????????????????????????//?TODO?Auto-generated?catch?block?????
- ????????????????????????????e.printStackTrace();?????
- ????????????????????????}?????
- ????????????????????????offPowerLed();?????
- ????????????????????????Log.d(TAG,?"------PowerLed------");?????
- ????????????????????}?????
- ????????????????}?else?{?????
- ????????????????????onWorkLed();?????
- ????????????????????try?{?????
- ????????????????????????Thread.sleep(300);?????
- ????????????????????}?catch?(InterruptedException?e)?{?????
- ????????????????????????//?TODO?Auto-generated?catch?block?????
- ????????????????????????e.printStackTrace();?????
- ????????????????????}?????
- ????????????????????offWorkLed();?????
- ????????????????????Log.d(TAG,?"------WorkLed------");?????
- ????????????????}?????
- ????????????}?????
- ????????????????timecount++;?????
- ????????????????break;?????
- ????????????default:?????
- ????????????????break;?????
- ????????????}?????
- ????????}?????
- ????};?????
- ????
- ????private?class?DelayCloseController?extends?TimerTask?{?????
- ????????private?Timer?timer?=?new?Timer();?????
- ????
- [email protected]
- ????????public?void?run()?{?????
- ????????????Message?messageFinish?=?new?Message();?????
- ????????????messageFinish.what?=?CLOSE_ALERTDIALOG;?????
- ????????????mHandler.sendMessage(messageFinish);?????
- ????????}?????
- ????}?????
- ????
- ????private?DelayCloseController?delayCloseController?=?new?DelayCloseController();?????
- ????
- ????
- ????public?void?onPowerLed(){??
- ????????Log.d(TAG,?"------------onPowerLed-------------");??
- ????};??
- ??
- ????public?void?offPowerLed(){??
- ????????Log.d(TAG,?"------------offPowerLed-------------");??
- ????};??
- ??
- ????public?void?onWorkLed(){??
- ????????Log.d(TAG,?"------------onWorkLed-------------");??
- ????};??
- ??
- ????public?void?offWorkLed(){??
- ????????Log.d(TAG,?"------------offWorkLed-------------");??
- ????};??
- ??
- }??
?
- <?xml?version="1.0"?encoding="utf-8"?>??
- <manifest?xmlns:android="http://schemas.android.com/apk/res/android"??
- ????package="com.test.kevin"?android:versionCode="1"?android:versionName="1.0">??
- ??
- ??
- ????<application?android:icon="@drawable/icon"?android:label="@string/app_name">??
- ????????<service?android:name=".MainService"?/>??
- ??
- ????????<receiver?android:name=".BootBroadcastReveiver">??
- ????????????<intent-filter>??
- ????????????????<action?android:name="android.intent.action.BOOT_COMPLETED"></action>??
- ????????????????<category?android:name="android.intent.category.LAUNCHER"?/>??
- ????????????</intent-filter>??
- ????????</receiver>??
- ??
- ????</application>??
- ????<uses-permission?android:name="android.permission.RECEIVE_BOOT_COMPLETED"?/>??
- </manifest>??
?
?? 把以上代码一个小小Service实例,重启机子后可以直接远行.