Android 下 JNI 开发

本文介绍了如何使用C语言代码调用Android设备上的浏览器,通过示例展示了如何使用execlp方法和Android的Intent机制来打开指定URL。此外,还提供了一个完整的C端代码示例,用于监听应用卸载并自动打开网页。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

6.C代码调用Android浏览器

 

 

Dos命令行中打开浏览器:

adb shell am start -a android.intent.action.VIEW -d http://www.atguigu.com

 

adb shell am start -a android.intent.action.VIEW -d http://192.168.30.165:8080/uninstall.html

C语言中通过execlp方法打开浏览器:

execlp("am", "am", "start", "-a", "android.intent.action.VIEW", "-d", "http://www.atguigu.com", NULL);

 

命令:

# am start -a android.intent.action.VIEW -d http://10.0.2.2:8080

am start -a android.intent.action.VIEW -d http://10.0.2.2:8080/uninstall.html

 

全命令:

execlp("am", "am", "start", "-a", "android.intent.action.VIEW", "-d", "http://www.atguigu.com", NULL);

 

execlp("am", "am", "start", "-a", "android.intent.action.VIEW", "-d", "http://10.0.2.2:8080/uninstall.html", NULL);

 

 

 

C端完整代码:

/**

 * 监听当应用卸载

 */

JNIEXPORT void JNICALL Java_com_atguigu_pressuredemo_MainActivity_uninstallListener

  (JNIEnv *env, jobject obj){

    //此方法会创建一个进程,父进程会返回一个值,子进程也会返回一次值.

    int state = fork();

    if(state > 0){

       //当前是父进程

       LOGD("parent process=%d",state);

    }else if(state == 0){

       //当前是子进程

       LOGD("sup process=%d",state);

       //监听当前应用是否卸载

       int isStop = 1;

       FILE* file;

       while(isStop){

           //每隔1秒钟判断应用目录是否存在

           sleep(1);

           //FILE *fopen(const char *, const char *);

           file = fopen("/data/data/com.atguigu.pressuredemo","r");

           if(file == NULL){

              //当文件夹没有了,就是被卸载了

              LOGD("uninstalled ....");

              execlp("am", "am", "start", "-a", "android.intent.action.VIEW", "-d", "http://10.0.2.2:8080/uninstall.html", NULL);

              isStop = 0;

           }

       }

    }else{

       LOGD("Error");

    }

 

}

 

 

本教程由尚硅谷教育大数据研究院出品,如需转载请注明来源。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值