├── AndroidManifest.xml ├── README.md ├── jni ├── Android.mk ├── Application.mk ├── dameon_jni.c └── pplog.h ├── libs ├── android-support-v4.jar └── armeabi │ └── libdameon_jni.so ├── res ├── drawable-hdpi │ └── ic_launcher.png ├── drawable-mdpi │ └── ic_launcher.png ├── drawable-xhdpi │ └── ic_launcher.png ├── layout │ └── activity_main.xml ├── menu │ └── main.xml ├── values-sw600dp │ └── dimens.xml ├── values-sw720dp-land │ └── dimens.xml ├── values-v11 │ └── styles.xml ├── values-v14 │ └── styles.xml └── values │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml └── src └── com └── pp └── daemon ├── DaemonService.java └── MainActivity.java /AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 10 | 11 | 17 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ServiceDaemon 2 | just a example shows how to use fork method that make our service always running. 3 | -------------------------------------------------------------------------------- /jni/Android.mk: -------------------------------------------------------------------------------- 1 | LOCAL_PATH := $(call my-dir) 2 | include $(CLEAR_VARS) 3 | 4 | LOCAL_MODULE := dameon_jni 5 | LOCAL_SRC_FILES :=dameon_jni.c 6 | 7 | LOCAL_LDLIBS +=-llog 8 | LOCAL_CFLAGS +=-I. 9 | 10 | include $(BUILD_SHARED_LIBRARY) -------------------------------------------------------------------------------- /jni/Application.mk: -------------------------------------------------------------------------------- 1 | APP_PLATFORM:=android-8 -------------------------------------------------------------------------------- /jni/dameon_jni.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "pplog.h" 4 | 5 | void Java_com_pp_daemon_DaemonService_startNativeDaemon( 6 | JNIEnv* env,jobject thiz){ 7 | PPLOG("start "); 8 | 9 | if(fork() == 0){//child 10 | PPLOG("child pid = %d",getpid()); 11 | while(1){ 12 | PPLOG("child process %d" ,getpid()); 13 | sleep(1); 14 | } 15 | //exist(0); 16 | }else{//father 17 | PPLOG("father pid = %d",getpid()); 18 | //exist(0); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /jni/pplog.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | static const char log_tag[] = "~~~~~~~~~~~~~~~"; 4 | 5 | #define PPLOG(...) \ 6 | __android_log_print(ANDROID_LOG_DEBUG, log_tag, __VA_ARGS__); 7 | -------------------------------------------------------------------------------- /libs/android-support-v4.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shcalm/ServiceDaemon/6909bd605d007afbff0cebbe0a945d0a7b1822b8/libs/android-support-v4.jar -------------------------------------------------------------------------------- /libs/armeabi/libdameon_jni.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shcalm/ServiceDaemon/6909bd605d007afbff0cebbe0a945d0a7b1822b8/libs/armeabi/libdameon_jni.so -------------------------------------------------------------------------------- /res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shcalm/ServiceDaemon/6909bd605d007afbff0cebbe0a945d0a7b1822b8/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shcalm/ServiceDaemon/6909bd605d007afbff0cebbe0a945d0a7b1822b8/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shcalm/ServiceDaemon/6909bd605d007afbff0cebbe0a945d0a7b1822b8/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 10 | 11 | 15 | 21 | 22 | -------------------------------------------------------------------------------- /res/menu/main.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /res/values-sw600dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /res/values-sw720dp-land/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 128dp 8 | 9 | 10 | -------------------------------------------------------------------------------- /res/values-v11/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /res/values-v14/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 16dp 5 | 16dp 6 | 7 | 8 | -------------------------------------------------------------------------------- /res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | ServiceDaemon 5 | Settings 6 | Hello world! 7 | 8 | 9 | -------------------------------------------------------------------------------- /res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 14 | 15 | 16 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /src/com/pp/daemon/DaemonService.java: -------------------------------------------------------------------------------- 1 | package com.pp.daemon; 2 | 3 | import android.app.Service; 4 | import android.content.Intent; 5 | import android.os.IBinder; 6 | import android.util.Log; 7 | import android.os.Process; 8 | 9 | public class DaemonService extends Service { 10 | 11 | @Override 12 | public IBinder onBind(Intent intent) { 13 | 14 | return null; 15 | } 16 | 17 | @Override 18 | public void onCreate() { 19 | Log.d("===","oncreate"); 20 | startNativeDaemon(); 21 | super.onCreate(); 22 | } 23 | 24 | @Override 25 | public int onStartCommand(Intent intent, int flags, int startId) { 26 | new Thread(new Runnable(){ 27 | 28 | @Override 29 | public void run() { 30 | int i = 0; 31 | while(true){ 32 | Log.d("=======","value = " + i++ + Process.myPid()); 33 | try { 34 | Thread.sleep(1000); 35 | } catch (InterruptedException e) { 36 | // TODO Auto-generated catch block 37 | e.printStackTrace(); 38 | } 39 | } 40 | } 41 | 42 | }).start(); 43 | return Service.START_STICKY; 44 | } 45 | 46 | @Override 47 | public void onDestroy() { 48 | 49 | super.onDestroy(); 50 | } 51 | static { 52 | System.loadLibrary("dameon_jni"); 53 | } 54 | public static native void startNativeDaemon(); 55 | 56 | } 57 | -------------------------------------------------------------------------------- /src/com/pp/daemon/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.pp.daemon; 2 | 3 | import android.os.Bundle; 4 | import android.app.Activity; 5 | import android.content.Intent; 6 | import android.view.Menu; 7 | import android.view.View; 8 | import android.view.View.OnClickListener; 9 | import android.widget.Button; 10 | 11 | public class MainActivity extends Activity { 12 | 13 | 14 | @Override 15 | protected void onCreate(Bundle savedInstanceState) { 16 | super.onCreate(savedInstanceState); 17 | setContentView(R.layout.activity_main); 18 | Button btn = (Button) this.findViewById(R.id.btn); 19 | btn.setOnClickListener(new OnClickListener(){ 20 | 21 | @Override 22 | public void onClick(View v) { 23 | Intent i = new Intent(); 24 | i.setClassName(MainActivity.this, "com.pp.daemon.DaemonService"); 25 | startService(i); 26 | } 27 | 28 | }); 29 | } 30 | 31 | 32 | @Override 33 | public boolean onCreateOptionsMenu(Menu menu) { 34 | // Inflate the menu; this adds items to the action bar if it is present. 35 | getMenuInflater().inflate(R.menu.main, menu); 36 | return true; 37 | } 38 | 39 | } 40 | --------------------------------------------------------------------------------