Android implementation of the Power-on Autostart Service (services) instance _android

Source: Internet
Author: User

Recently, in the porting of Hevsocks5client to Android, after adding SIGNALFD and TIMERFD related system call support, you can directly use NDK to compile the executable. The direct native exectuable is not always easy to use on the Android system. Or make a apk bar, tentatively only write a service and power-on automatically enabled, no activity.

Java to invoke the native program I chose to use the JNI way, directly in the Jni_onload method call Pthread_create Create a thread to run the original main on the line.

Copy Code code as follows:

...
#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 Services

The service is mainly to load the Hev-socks5-client library of the JNI interface and make the service run.

Copy Code code as follows:

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 to implement the automatic start service.

Copy Code code as follows:

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 the Service and Receiver in Manifest, increase access to the Internet and Boot completed.

Copy Code code as follows:

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

This method is only valid for Android 2.3.3 and previous versions, and after the version if this application has never been run after installation, broadcast Receiver will not receive the boot completed action by using the command to manually start the service.

Copy Code code as follows:
Am StartService hev.socks5/. Mainservice

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.