Auto-start service instance for Android

Source: Internet
Author: User

Recently, the hevsocks5client was ported to Android, and after increased SIGNALFD and TIMERFD related system call support, it was possible to compile executable directly using the NDK. Direct native exectuable on Android is not always easy to use OH. Or make an apk, tentatively write only one service and boot automatically enabled, no activity.

Calling the native program in Java I chose to use the JNI approach, calling Pthread_create directly in the Jni_onload method to create a thread to run the original main line.

... #if defined (ANDROID) #include <jni.h> #include <pthread.h> #endifintmain (int argc, char *argv[]) {    ...} #if defined (ANDROID) static void *thread_handler (void *data) {    main (0, NULL);    return NULL;} Jintjni_onload (JAVAVM *vm, void *reserved) {    pthread_t thread;    Pthread_create (&thread, NULL, Thread_handler, NULL);    return jni_version_1_4;} #endif

  

Android Services

The service is primarily the Hev-socks5-client library that loads the JNI interface, which makes the service run up.

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

The function of servicereceiver is to monitor the Boot_completed event on the system for automatic start-up service.

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

Finally, to register Service and Receiver in Manifest, increase access to the Internet and Boot completed.

<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "HEV.SOCKS5"Android:versioncode= "1"Android:versionname= "1.0">        <ApplicationAndroid:label= "@string/app_name" >                <ServiceAndroid:name=". Mainservice ">                        <Intent-filter>                                <ActionAndroid:name= "Hev.socks5.MainService" />                        </Intent-filter>                </Service>                <receiverandroid:enabled= "true"Android:name=". Servicereceiver ">                        <Intent-filter>                                <ActionAndroid:name= "Android.intent.action.BOOT_COMPLETED" />                        </Intent-filter>                </receiver>        </Application>        <uses-permissionAndroid:name= "Android.permission.INTERNET" />        <uses-permissionAndroid:name= "Android.permission.RECEIVE_BOOT_COMPLETED" /></Manifest>

Tips

This method is only valid on Android 2.3.3 and before version, if this app has never run after installation, broadcast receiver will not receive the boot completed action, the workaround is to start the service manually using the command.

Am StartService hev.socks5/. Mainservice

  

Auto-start service instance for Android

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.