execlp()函数

          execlp函数简单的来说就是C语言中执行系统命令的函数
          execlp()会从PATH 环境变量所指的目录中查找符合参数file 的文件名, 找到后便执行该文件, 然后将第二个以后的参数当做该文件的argv[0], argv[1], ..., 最后一个参数必须用空指针(NULL)作结束.
          android开发中,execlp函数对应android的path路径为system/bin/目录下

调用格式:

execlp("am","am","start","--user","0","-a","android.intent.action.VIEW","-d","http://www.google.com.hk",(char*)NULL);

监听APP被卸载后打开浏览器跳到指定网址实例源码如下:

.h代码:

/*
* MonitorUnistall.h
*
*/
#include <jni.h>

#ifndef MONITORUNISTALL_H_
#define MONITORUNISTALL_H_

#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com.vkex.vkmonitoruninstall.MainActivity
* Method: uninstall
* Signature: (Ljava/lang/String;I)V
*/
JNIEXPORT void JNICALL Java_com_vkex_vkmonitoruninstall_MainActivity_uninstall
(JNIEnv *, jobject, jstring, jint);

#ifdef __cplusplus
}
#endif

#endif /* MONITORUNISTALL_H_ */

.cpp代码:

#include<MonitorUnistall.h>
#include <stdio.h>
#include <jni.h>
#include <malloc.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/inotify.h>
#include <fcntl.h>
#include <stdint.h>
#include <android/log.h>

#define LOG_TAG "System.out.c"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)

/**
* 返回值 char* 这个代表char数组的首地址
* Jstring2CStr 把java中的jstring的类型转化成一个c语言中的char 字符串
*/
char* Jstring2CStr(JNIEnv* env, jstring jstr) {
char* rtn = NULL;
jclass clsstring = env->FindClass("java/lang/String"); //String
jstring strencode = env->NewStringUTF("GB2312"); // 得到一个java字符串 "GB2312"
jmethodID mid = env->GetMethodID(clsstring, "getBytes",
"(Ljava/lang/String;)[B"); //[ String.getBytes("gb2312");
jbyteArray barr = (jbyteArray) env->CallObjectMethod(jstr, mid, strencode); // String .getByte("GB2312");
jsize alen = env->GetArrayLength(barr); // byte数组的长度
jbyte* ba = env->GetByteArrayElements(barr, JNI_FALSE);
if (alen > 0) {
rtn = (char*) malloc(alen + 1); //"\0"
memcpy(rtn, ba, alen);
rtn[alen] = 0;
}
env->ReleaseByteArrayElements(barr, ba, 0); //
return rtn;
}

JNIEXPORT void JNICALL Java_com_vkex_vkmonitoruninstall_MainActivity_uninstall(
JNIEnv * env, jobject obj, jstring packageDir, jint sdkVersion) {
// 1,将传递过来的java的包名转为c的字符串
char * pd = Jstring2CStr(env, packageDir);

// 2,创建当前进程的克隆进程
pid_t pid = fork();

// 3,根据返回值的不同做不同的操作,<0,>0,=0
if (pid < 0) {
// 说明克隆进程失败
LOGD("current crate process failure");
} else if (pid > 0) {
// 说明克隆进程成功,而且该代码运行在父进程中
LOGD("crate process success,current parent pid = %d", pid);
} else {
// 说明克隆进程成功,而且代码运行在子进程中
LOGD("crate process success,current child pid = %d", pid);

// 4,在子进程中监视/data/data/包名这个目录
//初始化inotify进程
int fd = inotify_init();
if (fd < 0) {
LOGD("inotify_init failed !!!");
exit(1);
}

//添加inotify监听器
int wd = inotify_add_watch(fd, pd, IN_DELETE);
if (wd < 0) {
LOGD("inotify_add_watch failed !!!");
exit(1);
}

//分配缓存,以便读取event,缓存大小=一个struct inotify_event的大小,这样一次处理一个event
void *p_buf = malloc(sizeof(struct inotify_event));
if (p_buf == NULL) {
LOGD("malloc failed !!!");
exit(1);
}

//开始监听
LOGD("start observer");
ssize_t readBytes = read(fd, p_buf, sizeof(struct inotify_event));

//read会阻塞进程,走到这里说明收到目录被删除的事件,注销监听器
free(p_buf);
inotify_rm_watch(fd, IN_DELETE);

// 应用被卸载了,通知系统打开用户反馈的网页
LOGD("app uninstall,current sdkversion = %d", sdkVersion);
if (sdkVersion >= 17) {
// Android4.2系统之后支持多用户操作,所以得指定用户
execlp("am", "am", "start", "--user", "0", "-a",
"android.intent.action.VIEW", "-d", "http://www.baidu.com",
(char*) NULL);
} else {
// Android4.2以前的版本无需指定用户
execlp("am", "am", "start", "-a", "android.intent.action.VIEW",
"-d", "http://www.baidu.com", (char*) NULL);
}

}
}

android.mk 代码:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := MonitorUninstall
LOCAL_SRC_FILES := MonitorUninstall.cpp
LOCAL_LDLIBS += -llog

include $(BUILD_SHARED_LIBRARY)

MainActivity代码:

public class MainActivity extends Activity {

static {
System.loadLibrary("MonitorUninstall");
}

public native void uninstall(String packageDir, int sdkVersion);

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

String packageDir = "/data/data/" + getPackageName();
int sdkVersion = android.os.Build.VERSION.SDK_INT;
uninstall(packageDir, sdkVersion);
}

}

 

android JNI调用 execlp函数的更多相关文章

  1. android JNI调用(转)

    Android jni开发资料--NDK环境搭建 android开发人员注意了 谷歌改良了ndk的开发流程,对于Windows环境下NDK的开发,如果使用的NDK是r7之前的版本,必须要安装Cygwi ...

  2. Android中调用C++函数的一个简单Demo

    这里我不想多解释什么,对于什么JNI和NDK的相关内容大家自己去百度或谷歌.我对Android的学习也只是个新手.废话少说直接进入正题. 一.在Eclipse中创建一个Android Applicat ...

  3. android JNI调用机制

    JNI的出现使得开发者既可以利用Java语言跨平台.类库丰 富.开发便捷等特点,又可以利用Native语言的高效. JNI是JVM实现中的一部分,因此Native语言和Java代码都运行在JVM的宿主 ...

  4. android JNI调用(Android Studio 3.0.1)(转)

    最近回头复习了一下android 的jni调用,却发现按以前的方法调用失败,一怒之下就重新摸索,碰了几次壁,发现网上好多教程都不能成功调用,于是记录一下现在AS版本成功好用的调用方法. 这里设定你的n ...

  5. Android Jni调用浅述

    声明:欢迎转载,转载时请注明出处!http://blog.csdn.net/flydream0/article/details/7371692 1 简述 JNI是Java Native Interfa ...

  6. Android Jni 调用

    Chap1:JNI完全手册... 3 Chap2:JNI-百度百科... 11 Chap 3:javah命令帮助信息... 16 Chap 4:用javah产生一个.h文件... 17 Chap5:j ...

  7. android JNI 调用NDK方法

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  8. Android jni helloworld

    新建Android项目,设置布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android& ...

  9. [置顶] Android JNI必须掌握的五点

      1:JNI是什么? Java NativeInterface(JNI)是Java提供的一个很重要的特性.它使得用诸如C/C++等语言编写的代码可以与运行于Java虚拟机(JVM)中的 Java代码 ...

随机推荐

  1. 【转】各个层次的gcc警告 #pragma GCC diagnostic ignored "-Wunused-parameter" --不错

    原文网址:http://blog.csdn.net/lizzywu/article/details/9419145 各个层次的gcc警告从上到下覆盖 变量(代码)级:指定某个变量警告 int a __ ...

  2. bzoj1750 [Usaco2005 qua]Apple Catching

    Description It is a little known fact that cows love apples. Farmer John has two apple trees (which ...

  3. 《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅵ

    · 学后心得体会与部分习题实现 心得体会: 曾经只是了解了优先队列的基本性质,并会调用C++ STL库中的priority_queue以及 java.util.PriorityQueue<E&g ...

  4. Python 异步IO、IO多路复用

    事件驱动模型 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  5. 面试时如何优雅的谈论OC

    在面试中,我们经常会遇到一些原理性的问题,很常识但很难用通俗的语言解释清楚,这也是大部分业务级程序员经常失误的地方.虽然写了多年代码,但是核心思想不清,导致自己的后续发展受限,这是一个优秀的程序员和普 ...

  6. Handsontable Read-only cells

    一,列只读

  7. Prototypes analyze(二叉排序树,不同树形个数)

    Prototypes analyze 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 ALpha Ceiling Manufacturers (ACM) is ana ...

  8. NFinal学习笔记 03—代码生成器

    NFinal代码生成器与其他的代码生成器不太一样,只需要运行模块下的WebComplier.aspx即可生成最终的web层代码.包括数据库的操作,Router类, 调试文件等.附上一段代码与大家分享 ...

  9. System.Threading.Timer的使用技巧

    转自:http://www.360doc.com/content/11/0812/11/1039473_139824496.shtml# System.Threading.Timer timer = ...

  10. SSH连接LINUX乱码解决方法

    1.vi /etc/sysconfig/i18n 将内容改为 LANG="zh_CN.GB18030" LANGUAGE="zh_CN.GB18030:zh_CN.GB2 ...