欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android中實(shí)現(xiàn)開機(jī)自動(dòng)啟動(dòng)服務(wù)(service)實(shí)例

 更新時(shí)間:2014年06月12日 09:28:36   投稿:junjie  
這篇文章主要介紹了Android中實(shí)現(xiàn)自動(dòng)啟動(dòng)服務(wù)實(shí)例,并開機(jī)自動(dòng)啟用(無activity),的朋友可以參考下

最近在將 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 就行啦。

復(fù)制代碼 代碼如下:

...
#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ù)跑起來。

復(fù)制代碼 代碼如下:

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ù)。

復(fù)制代碼 代碼如下:

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)限。

復(fù)制代碼 代碼如下:

<?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。

復(fù)制代碼 代碼如下:
am startservice hev.socks5/.MainService

相關(guān)文章

最新評(píng)論