日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android 开机自动启动服务

發布時間:2025/3/13 Android 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android 开机自动启动服务 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在前面的文章中提到了remote service 的創建過程,現在我們要讓它開機自動啟動

?

1.在前面代碼的基礎上添加 RemoteServiceBootReceiver.java ,實現一個intent的receiver

[java] view plaincopyprint?
  • package?com.fly;??
  • import?android.content.BroadcastReceiver;??
  • import?android.content.Context;??
  • import?android.content.Intent;??
  • import?android.util.Log;??
  • public?class?RemoteServiceBootReceiver?extends?BroadcastReceiver?{??
  • ????private?static?final?String?TAG?=?"U0fly?RemoteServiceBootReceiver";??
  • ????static?final?String?ACTION?=?"android.intent.action.BOOT_COMPLETED";??
  • ????@Override??
  • ????public?void?onReceive(Context?arg0,?Intent?arg1)?{??
  • ????????Log.d(TAG,?"Boot?completed");??
  • ????????//?TODO?Auto-generated?method?stub??
  • ????????if?(arg1.getAction().equals(ACTION))?{??
  • ????????????//?service??
  • ????????????Intent?myintent?=?new?Intent(arg0,?RemoteService.class);??
  • ????????????myintent.setAction("com.fly.RemoteService");??
  • ????????????arg0.startService(myintent);??
  • ????????}??
  • ????}??
  • }??
  • package com.fly; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class RemoteServiceBootReceiver extends BroadcastReceiver { private static final String TAG = "U0fly RemoteServiceBootReceiver"; static final String ACTION = "android.intent.action.BOOT_COMPLETED"; @Override public void onReceive(Context arg0, Intent arg1) { Log.d(TAG, "Boot completed"); // TODO Auto-generated method stub if (arg1.getAction().equals(ACTION)) { // service Intent myintent = new Intent(arg0, RemoteService.class); myintent.setAction("com.fly.RemoteService"); arg0.startService(myintent); } } } ?

    ?

    2.在AndroidManifast.xml中添加權限,并注冊一個receiver

    [java] view plaincopyprint?
  • <?xml?version="1.0"?encoding="utf-8"?>??
  • <manifest?xmlns:android="http://schemas.android.com/apk/res/android"??
  • ????package="com.fly"?android:versionCode="1"?android:versionName="1.0">??
  • ????<application?android:icon="@drawable/icon"?android:label="@string/app_name">??
  • ????????<activity?android:name=".RemoteServiceActivity"??
  • ????????????android:label="@string/app_name">??
  • ????????????<intent-filter>??
  • ????????????????<action?android:name="android.intent.action.MAIN"?/>??
  • ????????????????<category?android:name="android.intent.category.LAUNCHER"?/>??
  • ????????????</intent-filter>??
  • ????????</activity>??
  • ????????<service?android:name="RemoteService">??
  • ????????????<intent-fliter>??
  • ????????????????<action?android:name="com.fly.RemoteService"?/>??
  • ????????????</intent-fliter>??
  • ????????</service>??
  • ??????
  • ????????<receiver?android:name=".RemoteServiceBootReceiver">??
  • ????????????<intent-filter>??
  • ????????????????<action?android:name="android.intent.action.BOOT_COMPLETED"?/>??
  • ????????????</intent-filter>??
  • ????????</receiver>??
  • ??????????
  • ????</application>??
  • ??????
  • ????<uses-permission?android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>???
  • ????<uses-sdk?android:minSdkVersion="7"?/>??
  • </manifest>???
  • <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.fly" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".RemoteServiceActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name="RemoteService"> <intent-fliter> <action android:name="com.fly.RemoteService" /> </intent-fliter> </service> <receiver android:name=".RemoteServiceBootReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> </application> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission> <uses-sdk android:minSdkVersion="7" /> </manifest> ?

    ?

    ?

    轉載于:https://www.cnblogs.com/xin36933/p/3796952.html

    總結

    以上是生活随笔為你收集整理的Android 开机自动启动服务的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。