Android解除安裝程式之後跳轉到指定的反饋頁面

idaretobe發表於2015-01-14

轉一個其他人解除安裝監聽方面的例項:

/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include <jni.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <android/log.h>
#include <unistd.h>
#include <sys/inotify.h>

#include "com_example_uninstalldemos_NativeClass.h"

/* 巨集定義begin */
//清0巨集
#define MEM_ZERO(pDest, destSize) memset(pDest, 0, destSize)

#define LOG_TAG "onEvent"

//LOG巨集定義
#define LOGD(fmt, args...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, fmt, ##args)

JNIEXPORT jstring JNICALL Java_com_example_uninstalldemos_NativeClass_init(JNIEnv* env, jobject thiz) {

    //初始化log
    LOGD("init start...");

    //fork子程式,以執行輪詢任務
    pid_t pid = fork();
    if (pid < 0) {
        //出錯log
        LOGD("fork failed...");
    } else if (pid == 0) {
        //子程式註冊"/data/data/pym.test.uninstalledobserver"目錄監聽器
        int fileDescriptor = inotify_init();
        if (fileDescriptor < 0) {
            LOGD("inotify_init failed...");
            exit(1);
        }

        int watchDescriptor;
        watchDescriptor = inotify_add_watch(fileDescriptor,"/data/data/com.example.uninstalldemos", IN_DELETE);
        LOGD("watchDescriptor=%d",watchDescriptor);
        if (watchDescriptor < 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...");
        size_t readBytes = read(fileDescriptor, p_buf,sizeof(struct inotify_event));

        //read會阻塞程式,走到這裡說明收到目錄被刪除的事件,登出監聽器
        free(p_buf);
        inotify_rm_watch(fileDescriptor, IN_DELETE);

        //目錄不存在log
        LOGD("uninstall");

        //執行命令am start -a android.intent.action.VIEW -d http://shouji.360.cn/web/uninstall/uninstall.html
        execlp(
            "am", "am", "start", "-a", "android.intent.action.VIEW", "-d", 
            "http://shouji.360.cn/web/uninstall/uninstall.html", (char *)NULL);
        //4.2以上的系統由於使用者許可權管理更嚴格,需要加上 --user 0
        //execlp("am", "am", "start", "--user", "0", "-a",
        //"android.intent.action.VIEW", "-d", "https://www.google.com",(char *) NULL);

    } else {
        //父程式直接退出,使子程式被init程式領養,以避免子程式僵死
    }

    return (*env)->NewStringUTF(env, "Hello from JNI !");
}

相關文章