专栏名称: 安卓开发精选
伯乐在线旗下账号,分享安卓应用相关内容,包括:安卓应用开发、设计和动态等。
目录
相关文章推荐
开发者全社区  ·  邻家弟弟好厉害 ·  22 小时前  
开发者全社区  ·  清华状元在痛苦中释怀了 ·  22 小时前  
开发者全社区  ·  空姐会不会爱上了我? ·  昨天  
开发者全社区  ·  华为夫妇一夜回到解放前 ·  2 天前  
开发者全社区  ·  梁文锋的流量密码 ·  2 天前  
51好读  ›  专栏  ›  安卓开发精选

Android 通过JNI实现守护进程(下)

安卓开发精选  · 公众号  · android  · 2016-10-25 11:38

正文

(点击 上方公众号 ,可快速关注)


来源:LeBron_Six

链接:blog.csdn.net/yyh352091626/article/details/50542554


接上文


C/C++端关键的部分主要是以上这些,接下来就是Java端调用。

首先来看一下so库的加载类,以及C++函数的调用:


package com . yyh . fork ;

import java . io . DataInputStream ;

import java . io . DataOutputStream ;

import java . io . File ;

public class NativeRuntime {

private static NativeRuntime theInstance = null ;

private NativeRuntime () {

}

public static NativeRuntime getInstance () {

if ( theInstance == null )

theInstance = new NativeRuntime ();

return theInstance ;

}

/**

* RunExecutable 启动一个可自行的lib*.so文件

* @date 2016-1-18 下午8:22:28

* @param pacaageName

* @param filename

* @param alias 别名

* @param args 参数

* @return

*/

public String RunExecutable ( String pacaageName , String filename , String alias , String args ) {

String path = "/data/data/" + pacaageName ;

String cmd1 = path + "/lib/" + filename ;

String cmd2 = path + "/" + alias ;

String cmd2_a1 = path + "/" + alias + " " + args ;

String cmd3 = "chmod 777 " + cmd2 ;

String cmd4 = "dd if=" + cmd1 + " of=" + cmd2 ;

StringBuffer sb_result = new StringBuffer ();

if ( ! new File ( "/data/data/" + alias ). exists ()) {

RunLocalUserCommand ( pacaageName , cmd4 , sb_result ); // 拷贝lib/libtest.so到上一层目录,同时命名为test.

sb_result . append ( ";" );

}

RunLocalUserCommand ( pacaageName , cmd3 , sb_result ); // 改变test的属性,让其变为可执行

sb_result . append ( ";" );

RunLocalUserCommand ( pacaageName , cmd2_a1 , sb_result ); // 执行test程序.

sb_result . append ( ";" );

return sb_result . toString ();

}

/**

* 执行本地用户命令

* @date 2016-1-18 下午8:23:01

* @param pacaageName

* @param command

* @param sb_out_Result

* @return

*/

public boolean RunLocalUserCommand ( String pacaageName , String command , StringBuffer sb_out_Result ) {

Process process = null ;

try {

process = Runtime . getRuntime (). exec ( "sh" ); // 获得shell进程

DataInputStream inputStream = new DataInputStream ( process . getInputStream ());

DataOutputStream outputStream = new DataOutputStream ( process . getOutputStream ());

outputStream . writeBytes ( "cd /data/data/" + pacaageName + "\n" ); // 保证在command在自己的数据目录里执行,才有权限写文件到当前目录

outputStream . writeBytes ( command + " &\n" ); // 让程序在后台运行,前台马上返回

outputStream . writeBytes ( "exit\n" );

outputStream . flush ();

process . waitFor ();

byte [] buffer = new byte [ inputStream . available ()];

inputStream . read ( buffer );

String s = new String ( buffer );

if ( sb_out_Result != null )

sb_out_Result . append ( "CMD Result:\n" + s );

} catch ( Exception e ) {

if ( sb_out_Result != null )

sb_out_Result . append ( "Exception:" + e . getMessage ());

return false ;

}

return true ;

}

public native void startActivity ( String compname );

public native String stringFromJNI ();

public native void startService ( String srvname , String sdpath );

public native int findProcess ( String packname );

public native int stopService ();

static {

try {

System . loadLibrary ( "helper" ); // 加载so库

} catch ( Exception e ) {

e . printStackTrace ();

}

}

}


然后,我们在收到开机广播后,启动该服务。


package com . yyh . activity ;

import android . content . BroadcastReceiver ;

import android . content . Context ;

import android . content . Intent ;

import android . util . Log ;

import com . yyh . fork . NativeRuntime ;

import com . yyh . utils . FileUtils ;

public class PhoneStatReceiver extends BroadcastReceiver {

private String TAG = "tag" ;

@Override

public void onReceive ( Context context , Intent intent ) {

if ( Intent . ACTION_BOOT_COMPLETED . equals ( intent . getAction ())) {

Log . i ( TAG , "手机开机了~~" );

NativeRuntime . getInstance (). startService ( context . getPackageName () + "/com.yyh.service.HostMonitor" , FileUtils . createRootPath ());

} else if ( Intent . ACTION_USER_PRESENT .







请到「今天看啥」查看全文


推荐文章
开发者全社区  ·  邻家弟弟好厉害
22 小时前
开发者全社区  ·  清华状元在痛苦中释怀了
22 小时前
开发者全社区  ·  空姐会不会爱上了我?
昨天
开发者全社区  ·  华为夫妇一夜回到解放前
2 天前
开发者全社区  ·  梁文锋的流量密码
2 天前
寻找中国创客  ·  2016年最不容错过的十部电影 | 春节荐影
8 年前
儿童摄影引导技巧  ·  网红们的平坦小腹...都是骗人的!
7 年前
阅尽天下沧桑  ·  老 毕 饭 局 被 写 成 歌 词,火 了!
7 年前