After the Android app is uninstalled, automatically use the browser to open the specified connection

Source: Internet
Author: User

This article provides a way to "automatically open a specified connection using a browser after an Android app is uninstalled."
Principle: Somewhere in the Android program, based on the JNI call C code to open a sub-process monitoring application in the System file directory, once the application is uninstalled, the directory will be deleted by the system, triggering the child process to execute the relevant code (this example calls the browser to open a connection)
1. Create a JNI directory under the Android project
2. Create a file in the JNI directory observer.c

/* Copyright (C) theThe Android Open Source Project * * Licensed under the Apache License, Version2.0(The"License"); * May notUse ThisFile exceptinchCompliance withThe License. * May obtain a copy ofThe License at * *http://www.apache.org/licenses/license-2.0* * unless required byApplicable laworAgreed toinchWriting, software * Distributed under the License isDistributed onAn" as is"BASIS, * without warranties OR CONDITIONS of any KIND, either expressorImplied. * See the License forThe specific language governing permissions and* Limitations under the License. * */#include <string.h>#include <jni.h>#include <jni.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <android/log.h>#include <unistd.h>#include <sys/inotify.h>/ * Macro definition Begin * ///Qing0Macro#define Mem_zero (pDest, Destsize) memset (pDest, 0, Destsize)//Log macro Definition#define LOG_INFO (tag, msg) __android_log_write (android_log_info, Tag, msg)#define LOG_DEBUG (tag, msg) __android_log_write (android_log_debug, Tag, msg)#define LOG_WARN (tag, msg) __android_log_write (Android_log_warn, Tag, msg)#define LOG_ERROR (tag, msg) __android_log_write (android_log_error, Tag, msg)/ * global variable BEGIN * /static char c_tag[] ="OnEvent"; static Jboolean b_is_copy = Jni_true;/ * Natvie method exposed to Android code call * /Jstring java_com_zgy_catchuninstallself_uninstallobserver_startwork (jnienv* env, jobject thiz, jstring path, Jstrin G URL, jint version) {jstring Tag = (*env)Newstringutf (env, C_tag);//Initialize LOG log_debug( (*env)->getstringutfchars (env, tag, &b_is_copy), (*env)->getstringutfchars (env, (*env)->newst Ringutf (env, "init OK"), &b_is_copy)); //ForkChild process to perform a polling taskpid_t PID=Fork();if (PID < 0){//ErrorLog        Log_error( *env)->getstringutfchars (env, tag, &b_is_copy), (*env)->getstringutfchars (env, (*env)->newstringutf (env, "fork failed!!!"), &b_is_copy)); }Else if (pid = = 0){//Child process Register Directory Listenerint FileDescriptor=Inotify_init();if (FileDescriptor < 0){Log_debug( *env)->getstringutfchars (env, tag, &b_is_copy), (*env)->getstringutfchars (env, (*env)->newstringutf (env, "Inotify_init failed!!!"), &A mp;b_is_copy));Exit(1); }int Watchdescriptor;Watchdescriptor=Inotify_add_watch(FileDescriptor, (*env)->getstringutfchars (env, Path, NULL), In_delete);if (Watchdescriptor < 0){Log_debug( *env)->getstringutfchars (env, tag, &b_is_copy), (*env)->getstringutfchars (env, (*env)->newstringutf (env, "Inotify_add_watch failed!!!" ), &b_is_copy));Exit(1); }//Allocate cache to readEvent, Cache size = onestruct inotify_eventThe size, so one time to process aEvent        void*P_buf=malloc(sizeof (struct inotify_event));if (P_buf = = NULL){Log_debug( *env)->getstringutfchars (env, tag, &b_is_copy), (*env)->getstringutfchars (env, (*env)->newstringutf (env, "malloc failed!!!"), &b_is_copy)) ;Exit(1); }//Start monitoringLog_debug( *env)->getstringutfchars (env, tag, &b_is_copy), (*env)->getstringutfchars (env, (*env)->newstringutf (env, "start Observer"), &b_is_copy)); //Readwill block the process,size_t readbytes=Read(FileDescriptor, p_buf, sizeof (struct inotify_event)); Go here to show the event that the directory was deleted, unregister the listener Free(P_BUF);Inotify_rm_watch(FileDescriptor, In_delete); Directory does not existLog        Log_debug( *env)->getstringutfchars (env, tag, &b_is_copy), (*env)->getstringutfchars (env, (*env)->newstringutf (env, "uninstalled"), &b_is_copy));if (version >= ){//4.2 above system is more strict because of user rights management, need to add--User0EXECLP("AM", "AM", "Start", "--user", "0", "-a", "Android.intent.action.VIEW", "-D", (*env)->getstringutfchars (env, URL, NULL ), (char *) NULL); }Else{EXECLP("AM", "AM", "Start", "-a", "Android.intent.action.VIEW", "-D", (*env)->getstringutfchars (env, URL, null), (char *) NULL); }//EXTENSION: You can perform otherShellCommandam(i.e. Activity Manager), you can open a program, service,Broadcast IntentAnd so onElse{//parent process exits directly so that the child process isInitProcess adoption to avoid child process zombie}return (*env)-Newstringutf (ENV,"Hello from JNI!");}

3. Create a file in the JNI directory android.mk

Local_path :=$(Call My-dir)include $(Clear_vars)Local_module:=observerLocal_src_files:=observer.cLocal_c_includes:=$(Local_path)/includeLocal_ldlibs+= -L$(Sysroot)/usr/lib-lloginclude $(build_shared_library)

4. Create the Natvie method and invoke the Jni method in Android

package  com.zgy.catchuninstallself; public  class  uninstallobserver  { static  {system.loadlibrary ( "observe    R "); } public  static   Native  string startwork  (string path, string url,  version); //path:data/data/[packagenmae]; URL: Jump to the page, need HTTP//or https://start }  
"http://www.baidu.com";UninstallObserver.startWork(path, url, android.os.Build.VERSION.SDK_INT);

Note: In the case of a repeated invocation of the Jni method, I have not tested whether multiple sub-processes will be opened repeatedly, and this is left to everyone to test.

After the Android app is uninstalled, automatically use the browser to open the specified connection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.