android开机启动一个Service实例
16lz
2021-12-04
android开机启动一个Service实例
- 博客分类:
- android
- java
android开机启动一个Service研究一下其实也蛮简单,下面直接看代码吧。
Bootbroadcastreveiver代码- packagecom.test.kevin;
- importandroid.content.BroadcastReceiver;
- importandroid.content.Context;
- importandroid.content.Intent;
- importandroid.util.Log;
- publicclassBootBroadcastReveiverextendsBroadcastReceiver{
- privatestaticfinalStringTAG="BootBroadcastReveiver";
- @Override
- publicvoidonReceive(Contextcontext,Intentintent){
- if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
- IntentbootServiceIntent=newIntent(context,MainActivity.class);
- context.startService(bootServiceIntent);
- Log.d(TAG,"--------Bootstartservice-------------");
- }
- }
- }
- packagecom.test.kevin;
- importjava.util.Timer;
- importjava.util.TimerTask;
- importandroid.app.Service;
- importandroid.content.BroadcastReceiver;
- importandroid.content.Context;
- importandroid.content.Intent;
- importandroid.content.IntentFilter;
- importandroid.os.Handler;
- importandroid.os.IBinder;
- importandroid.os.Message;
- importandroid.util.Log;
- publicclassMainServiceextendsService{
- privatestaticfinalStringTAG="MainService";
- privatefinalintCLOSE_ALERTDIALOG=0;
- privateinttimecount=0;
- privateintbatteryValue=0;
- @Override
- publicIBinderonBind(Intentintent){
- Log.d(TAG,"----onBind-----");
- returnnull;
- }
- @Override
- publicvoidonCreate(){
- Log.d(TAG,"---------onCreate--------");
- registerReceiver(batteryChangedReceiver,newIntentFilter(
- Intent.ACTION_BATTERY_CHANGED));
- delayCloseController.timer.schedule(delayCloseController,1500,1000);
- }
- @Override
- publicintonStartCommand(Intentintent,intflags,intstartId){
- //TODOAuto-generatedmethodstub
- returnsuper.onStartCommand(intent,flags,startId);
- }
- privateBroadcastReceiverbatteryChangedReceiver=newBroadcastReceiver(){
- @Override
- publicvoidonReceive(Contextcontext,Intentintent){
- if(Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())){
- intlevel=intent.getIntExtra("level",0);
- intscale=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;
- }
- }
- };
- privateHandlermHandler=newHandler(){
- @Override
- publicvoidhandleMessage(Messagemsg){
- switch(msg.what){
- caseCLOSE_ALERTDIALOG:{
- if(timecount%2==1){
- if(batteryValue<=15){
- onPowerLed();
- try{
- Thread.sleep(300);
- }catch(InterruptedExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }
- offPowerLed();
- Log.d(TAG,"------PowerLed------");
- }
- }else{
- onWorkLed();
- try{
- Thread.sleep(300);
- }catch(InterruptedExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }
- offWorkLed();
- Log.d(TAG,"------WorkLed------");
- }
- }
- timecount++;
- break;
- default:
- break;
- }
- }
- };
- privateclassDelayCloseControllerextendsTimerTask{
- privateTimertimer=newTimer();
- @Override
- publicvoidrun(){
- MessagemessageFinish=newMessage();
- messageFinish.what=CLOSE_ALERTDIALOG;
- mHandler.sendMessage(messageFinish);
- }
- }
- privateDelayCloseControllerdelayCloseController=newDelayCloseController();
- publicvoidonPowerLed(){
- Log.d(TAG,"------------onPowerLed-------------");
- };
- publicvoidoffPowerLed(){
- Log.d(TAG,"------------offPowerLed-------------");
- };
- publicvoidonWorkLed(){
- Log.d(TAG,"------------onWorkLed-------------");
- };
- publicvoidoffWorkLed(){
- Log.d(TAG,"------------offWorkLed-------------");
- };
- }
- <?xmlversion="1.0"encoding="utf-8"?>
- <manifestxmlns:android="http://schemas.android.com/apk/res/android"
- package="com.test.kevin"android:versionCode="1"android:versionName="1.0">
- <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
- <serviceandroid:name=".MainService"/>
- <receiverandroid:name=".BootBroadcastReveiver">
- <intent-filter>
- <actionandroid:name="android.intent.action.BOOT_COMPLETED"></action>
- <categoryandroid:name="android.intent.category.LAUNCHER"/>
- </intent-filter>
- </receiver>
- </application>
- <uses-permissionandroid:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
- </manifest>
把以上代码一个小小Service实例,重启机子后可以直接远行.
更多相关文章
- android开机启动一个Service实例
- android全屏设置代码
- android framework 低电量关机处理流程 浅析
- Android中一些比较好的开源代码项目
- 推荐几个android 学习与应用 网站
- Android(安卓)使用WebView控件展示SVG图
- Android实现全屏
- 系出名门Android(6) - 控件(View)
- 【分享】大量android软件开发资料和实例代码【转】