Android中實(shí)現(xiàn)開機(jī)自動(dòng)啟動(dòng)服務(wù)(service)實(shí)例
最近在將 HevSocks5Client 移植到 Android 上了,在經(jīng)過增加 signalfd 和 timerfd 相關(guān)的系統(tǒng)調(diào)用支持后,就可以直接使用 NDK 編譯出 executable 了。直接的 native exectuable 在 Android 系統(tǒng)總還是不太方便用哦。還是做成一個(gè) apk 吧,暫定只寫一個(gè) service 并開機(jī)自動(dòng)啟用,無 activity 的。
Java 中調(diào)用 native 程序我選擇使用 JNI 方式,直接在 JNI_OnLoad 方法中調(diào)用 pthread_create 創(chuàng)建個(gè)線程跑原來的 main 就行啦。
...
#if defined(ANDROID)
#include <jni.h>
#include <pthread.h>
#endif
int
main (int argc, char *argv[])
{
...
}
#if defined(ANDROID)
static void *
thread_handler (void *data)
{
main (0, NULL);
return NULL;
}
jint
JNI_OnLoad (JavaVM *vm, void *reserved)
{
pthread_t thread;
pthread_create (&thread, NULL, thread_handler, NULL);
return JNI_VERSION_1_4;
}
#endif
Android 服務(wù)
服務(wù)主要是加載 JNI 接口的 hev-socks5-client 庫(kù),使服務(wù)跑起來。
package hev.socks5;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class MainService extends Service {
static {
System.loadLibrary("hev-socks5-client");
}
public IBinder onBind(Intent intent) {
return null;
}
}
BroadcastReceiver
ServiceReceiver 的功能就是監(jiān)聽系統(tǒng)上的 BOOT_COMPLETED 事件,用于實(shí)現(xiàn)自動(dòng)啟動(dòng)服務(wù)。
package hev.socks5;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class ServiceReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent i = new Intent(context, MainService.class);
context.startService(i);
}
}
}
AndroidManifest.xml
最后,要在 Manifest 中注冊(cè) Service 和 Receiver,增加上訪問 Internet 和 Boot completed 的權(quán)限。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="hev.socks5"
android:versionCode="1"
android:versionName="1.0">
<application android:label="@string/app_name" >
<service android:name=".MainService">
<intent-filter>
<action android:name="hev.socks5.MainService" />
</intent-filter>
</service>
<receiver android:enabled="true" android:name=".ServiceReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
</manifest>
Tips
此方法僅在 Android 2.3.3 及之前版本有效,之后版本如果此應(yīng)用在安裝后從沒運(yùn)行過,Broadcast Receiver 將接收不到 boot completed 的 action,解決方法是使用命令手動(dòng)啟動(dòng)一下 service。
- android?Service基礎(chǔ)(啟動(dòng)服務(wù)與綁定服務(wù))
- Android ServiceManager的啟動(dòng)和工作原理
- Android 系統(tǒng)服務(wù)TelecomService啟動(dòng)過程原理分析
- Android Service的啟動(dòng)過程分析
- Android Service啟動(dòng)過程完整分析
- Android實(shí)現(xiàn)開機(jī)自動(dòng)啟動(dòng)Service或app的方法
- Android Service自啟動(dòng)注意事項(xiàng)分析
- android開發(fā)教程之開機(jī)啟動(dòng)服務(wù)service示例
- Android?Service啟動(dòng)流程刨析
相關(guān)文章
AndroidQ分區(qū)存儲(chǔ)權(quán)限變更及適配的實(shí)現(xiàn)
這篇文章主要介紹了AndroidQ分區(qū)存儲(chǔ)權(quán)限變更及適配的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06Flutter文本Text和輸入框TextField組件使用示例
這篇文章主要為大家介紹了Flutter文本Text和輸入文本框TextField組件使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08Android通過記住密碼功能學(xué)習(xí)數(shù)據(jù)存儲(chǔ)類SharedPreferences詳解及實(shí)例
這篇文章主要通過“記住密碼”實(shí)例功能學(xué)習(xí)為大家介紹了Android數(shù)據(jù)存儲(chǔ)類SharedPreferences,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-03-03WebView 實(shí)現(xiàn)全屏播放視頻的示例代碼
這篇文章主要介紹了WebView 實(shí)現(xiàn)全屏播放視頻的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09Android Style.xml的應(yīng)用詳解及代碼實(shí)現(xiàn)
這篇文章主要介紹了Android Style.xml的應(yīng)用詳解及代碼實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2016-10-10Android技巧一之啟動(dòng)屏+新功能左右導(dǎo)航邏輯
這篇文章主要介紹了Android技巧一之啟動(dòng)屏+新功能左右導(dǎo)航邏輯的相關(guān)資料,需要的朋友可以參考下2016-01-01android MediaRecorder實(shí)現(xiàn)錄屏?xí)r帶錄音功能
這篇文章主要介紹了android MediaRecorder錄屏?xí)r帶錄音功能實(shí)現(xiàn)代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04Android APP啟動(dòng)方式、啟動(dòng)流程及啟動(dòng)優(yōu)化分析
這篇文章主要介紹了Android APP啟動(dòng)方式、啟動(dòng)流程及啟動(dòng)優(yōu)化分析的相關(guān)資料,需要的朋友可以參考下2016-09-09AndroidStudio不自動(dòng)添加新創(chuàng)建的文件到VCS的解決辦法
這篇文章主要介紹了AndroidStudio不自動(dòng)添加新創(chuàng)建的文件到VCS的解決辦法的相關(guān)資料,需要的朋友可以參考下2017-03-03