詳解Android App卸載后跳轉(zhuǎn)到指定的反饋?lái)?yè)面的方法
很多人也許會(huì)問(wèn):360被卸載之后會(huì)跳轉(zhuǎn)到指定的反饋?lái)?yè)面,是怎么弄的?
其實(shí)這個(gè)問(wèn)題的核心就在于:應(yīng)用被卸載了,如果能夠做到后續(xù)的代碼邏輯繼續(xù)執(zhí)行
我們?cè)賮?lái)仔細(xì)分析一下場(chǎng)景和流程
一個(gè)應(yīng)用被用戶卸載肯定是有理由的,而開(kāi)發(fā)者卻未必能得知這一重要的理由,畢竟用戶很少會(huì)主動(dòng)反饋建議,多半就是用得不爽就卸,如果能在被卸載后獲取到用戶的一些反饋,那對(duì)開(kāi)發(fā)者進(jìn)一步改進(jìn)應(yīng)用是非常有利的。目前據(jù)我所知,國(guó)內(nèi)的Android應(yīng)用中實(shí)現(xiàn)這一功能的只有360手機(jī)衛(wèi)士、360平板衛(wèi)士,那么如何實(shí)現(xiàn)這一功能的?
我們可以把實(shí)現(xiàn)卸載反饋的問(wèn)題轉(zhuǎn)化為監(jiān)聽(tīng)自己是否被卸載,只有得知自己被卸載,才可以設(shè)計(jì)相應(yīng)的反饋處理流程。以下的列表是我在研究這一問(wèn)題的思路:
1、注冊(cè)BroadcastReceiver,監(jiān)聽(tīng)"android.intent.action.PACKAGE_REMOVED"系統(tǒng)廣播
結(jié)果:NO。未寫(xiě)代碼,直接分析,卸載的第一步就是退出當(dāng)前應(yīng)用的主進(jìn)程,而此廣播是在已經(jīng)卸載完成后才發(fā)出的,此時(shí)主進(jìn)程都沒(méi)有了,去哪onReceive()呢?
2、若能收到"將要卸載XX包"的系統(tǒng)廣播,在主進(jìn)程被退出之前就搶先進(jìn)行反饋處理就好了,可惜沒(méi)有這樣的系統(tǒng)廣播,不過(guò)經(jīng)過(guò)調(diào)研,倒是發(fā)現(xiàn)了一個(gè)辦法,讀取系統(tǒng)log,當(dāng)日志中包含"android.intent.action.DELETE"和自己的包名時(shí),意味著自己將要被卸載。
結(jié)果:NO。調(diào)試時(shí)發(fā)現(xiàn)此方法有兩個(gè)缺陷,(1)點(diǎn)擊設(shè)置中的卸載按鈕即發(fā)出此Intent,此時(shí)用戶尚未在彈框中確認(rèn)卸載;(2)pm命令卸載不出發(fā)此Intent,意味著被諸如手機(jī)安全管家,豌豆莢等軟件卸載時(shí),無(wú)法提前得知卸載意圖。
3、由于時(shí)間點(diǎn)不容易把控,所以干脆不依賴系統(tǒng)廣播或log,考慮到卸載過(guò)程會(huì)刪除"/data/data/包名"目錄,我們可以用線程直接輪詢這個(gè)目錄是否存在,以此為依據(jù)判斷自己是否被卸載。
結(jié)果:NO。同方法1,主進(jìn)程退出,相應(yīng)的線程必定退出,線程還沒(méi)等到判斷目錄是否存在就已經(jīng)被銷毀了。
4、改用C端進(jìn)程輪詢"/data/data/包名"目錄是否存在
結(jié)果:YES。借助Java端進(jìn)程fork出來(lái)的C端進(jìn)程在應(yīng)用被卸載后不會(huì)被銷毀。
解決的方案確定了,下面來(lái)看一下代碼吧:
#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子進(jìn)程,以執(zhí)行輪詢?nèi)蝿?wù)
pid_t pid = fork();
if (pid < 0) {
//出錯(cuò)log
LOGD("fork failed...");
} else if (pid == 0) {
//子進(jìn)程注冊(cè)"/data/data/pym.test.uninstalledobserver"目錄監(jiān)聽(tīng)器
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,緩存大小=一個(gè)struct inotify_event的大小,這樣一次處理一個(gè)event
void *p_buf = malloc(sizeof(struct inotify_event));
if (p_buf == NULL) {
LOGD("malloc failed...");
exit(1);
}
//開(kāi)始監(jiān)聽(tīng)
LOGD("start observer...");
size_t readBytes = read(fileDescriptor, p_buf,sizeof(struct inotify_event));
//read會(huì)阻塞進(jìn)程,走到這里說(shuō)明收到目錄被刪除的事件,注銷監(jiān)聽(tīng)器
free(p_buf);
inotify_rm_watch(fileDescriptor, IN_DELETE);
//目錄不存在log
LOGD("uninstall");
//執(zhí)行命令am start -a android.intent.action.VIEW -d http://shouji.#/web/uninstall/uninstall.html
execlp(
"am", "am", "start", "-a", "android.intent.action.VIEW", "-d",
"http://shouji.#/web/uninstall/uninstall.html", (char *)NULL);
//4.2以上的系統(tǒng)由于用戶權(quán)限管理更嚴(yán)格,需要加上 --user 0
//execlp("am", "am", "start", "--user", "0", "-a",
//"android.intent.action.VIEW", "-d", "https://www.google.com",(char *) NULL);
} else {
//父進(jìn)程直接退出,使子進(jìn)程被init進(jìn)程領(lǐng)養(yǎng),以避免子進(jìn)程僵死
}
return (*env)->NewStringUTF(env, "Hello from JNI !");
}
這里面主要是用到了Linux中的inotify,這個(gè)相關(guān)的內(nèi)容可以自行百度一下~~
這里有一個(gè)很重要的知識(shí),也是解決這個(gè)問(wèn)題的關(guān)鍵所在,就是Linux中父進(jìn)程死了,但是子進(jìn)程不會(huì)死,而是被init進(jìn)程領(lǐng)養(yǎng)。所以當(dāng)我們應(yīng)用(進(jìn)程)卸載了,但是我們fork的子進(jìn)程并不會(huì)銷毀,所以我們上述的邏輯代碼就可以放到這里來(lái)做了。(學(xué)習(xí)了)
Android應(yīng)用程序代碼:
MyActivity.java
package com.example.uninstalldemos;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, SDCardListenSer.class);
startService(intent);
NativeClass nativeObj = new NativeClass();
nativeObj.init();
}
static {
Log.d("onEvent", "load jni lib");
System.loadLibrary("hello-jni");
}
}
SDCardListenSer.java
package com.example.uninstalldemos;
import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.os.FileObserver;
import android.os.IBinder;
import android.util.Log;
import java.io.File;
import java.io.IOException;
public class SDCardListenSer extends Service {
SDCardListener[] listenners;
@SuppressLint("SdCardPath")
@Override
public void onCreate() {
SDCardListener[] listenners = {
new SDCardListener("/data/data/com.example.uninstalldemos", this),
new SDCardListener(Environment.getExternalStorageDirectory() + File.separator + "1.txt", this) };
this.listenners = listenners;
Log.i("onEvent", "=========onCreate============");
for (SDCardListener listener : listenners) {
listener.startWatching();
}
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "1.txt");
Log.i("onEvent", "dddddddddddddddddddddd nCreate============");
if (file.exists())
file.delete();
/*try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}*/
}
@Override
public void onDestroy() {
for (SDCardListener listener : listenners) {
listener.stopWatching();
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
class SDCardListener extends FileObserver {
private String mPath;
private final Context mContext;
public SDCardListener(String parentpath, Context ctx) {
super(parentpath);
this.mPath = parentpath;
this.mContext = ctx;
}
@Override
public void onEvent(int event, String path) {
int action = event & FileObserver.ALL_EVENTS;
switch (action) {
case FileObserver.DELETE:
Log.i("onEvent", "delete path: " + mPath + File.separator + path);
//openBrowser();
break;
case FileObserver.MODIFY:
Log.i("onEvent", "更改目錄" + mPath + File.separator + path);
break;
case FileObserver.CREATE:
Log.i("onEvent", "創(chuàng)建文件" + mPath + File.separator + path);
break;
default:
break;
}
}
protected void openBrowser() {
Uri uri = Uri.parse("http://aoi.androidesk.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
mContext.startActivity(intent);
}
public void exeShell(String cmd) {
try {
Runtime.getRuntime().exec(cmd);
} catch (Throwable t) {
t.printStackTrace();
}
}
}
開(kāi)啟一個(gè)服務(wù),在這個(gè)服務(wù)中我們可以看到,用到了一個(gè)很重要的一個(gè)類FileObserver,也是用來(lái)監(jiān)聽(tīng)文件的變更的,這個(gè)和上面的inotify功能差不多。關(guān)于這個(gè)類的具體用法和介紹,可以自行百度呀~~
運(yùn)行:
我們將應(yīng)用安裝之后,打開(kāi)log進(jìn)行檢測(cè)日志:
adb logcat -s onEvent

- iOS和Android用同一個(gè)二維碼實(shí)現(xiàn)跳轉(zhuǎn)下載鏈接的方法
- Android viewpager在最后一頁(yè)滑動(dòng)之后跳轉(zhuǎn)到主頁(yè)面的實(shí)例代碼
- Android開(kāi)發(fā)之a(chǎn)ctiviti節(jié)點(diǎn)跳轉(zhuǎn)
- Android 實(shí)現(xiàn)閃屏頁(yè)和右上角的倒計(jì)時(shí)跳轉(zhuǎn)實(shí)例代碼
- Android TextView中文本點(diǎn)擊文字跳轉(zhuǎn) (代碼簡(jiǎn)單)
- Android如何跳轉(zhuǎn)到應(yīng)用商店的APP詳情頁(yè)面
相關(guān)文章
Android中TelephonyManager類的用法案例詳解
這篇文章主要介紹了Android中TelephonyManager類的用法,以獲取Android手機(jī)硬件信息為例詳細(xì)分析了TelephonyManager類的使用技巧,需要的朋友可以參考下2015-09-09
Android中buildToolVersion與CompileSdkVersion的區(qū)別
今天小編就為大家分享一篇關(guān)于Android中buildToolVersion與CompileSdkVersion的區(qū)別,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12
android初學(xué)者必須掌握的Activity狀態(tài)的四大知識(shí)點(diǎn)(必讀)
本篇文章主要介紹了android activity的四種狀態(tài),詳細(xì)的介紹了四種狀態(tài),包括Running狀態(tài)、Paused狀態(tài)、Stopped狀態(tài)、Killed狀態(tài),有興趣的可以了解一下。2016-11-11
百度地圖實(shí)現(xiàn)小車規(guī)劃路線后平滑移動(dòng)功能
這篇文章主要介紹了百度地圖實(shí)現(xiàn)小車規(guī)劃路線后平滑移動(dòng)功能,本文是小編寫(xiě)的一個(gè)demo,通過(guò)效果圖展示的非常直白,需要的朋友可以參考下2020-01-01
解析Android開(kāi)發(fā)優(yōu)化之:對(duì)界面UI的優(yōu)化詳解(三)
本篇文章主要討論一下復(fù)雜界面中常用的一種技術(shù)——界面延遲加載技術(shù)2013-05-05
Android studio創(chuàng)建第一個(gè)app
這篇文章主要為大家詳細(xì)介紹了如何使用Android studio創(chuàng)建你的第一個(gè)項(xiàng)目Hello World,感興趣的小伙伴們可以參考一下2016-05-05
Android中獲取資源 id 及資源 id 的動(dòng)態(tài)獲取
這篇文章主要介紹了 Android中獲取資源 id 及資源 id 的動(dòng)態(tài)獲取的相關(guān)資料,需要的朋友可以參考下2017-01-01
Android編程中FileOutputStream與openFileOutput()的區(qū)別分析
這篇文章主要介紹了Android編程中FileOutputStream與openFileOutput()的區(qū)別,結(jié)合實(shí)例形式分析了FileOutputStream與openFileOutput()的功能,使用技巧與用法區(qū)別,需要的朋友可以參考下2016-02-02
Android 用Time和Calendar獲取系統(tǒng)當(dāng)前時(shí)間源碼分享(年月日時(shí)分秒周幾)
這篇文章主要介紹了Android 用Time和Calendar獲取系統(tǒng)當(dāng)前時(shí)間源碼分享,包括年月日時(shí)分秒周幾的源碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-01-01

