欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Android App卸載后跳轉(zhuǎn)到指定的反饋頁面的方法

 更新時間:2016年04月28日 17:26:53   作者:尼古拉斯_趙四  
這篇文章主要介紹了Android App卸載后跳轉(zhuǎn)到指定的反饋頁面的方法,關(guān)鍵點是相關(guān)線程要判斷在目錄被消除以前作出響應(yīng),需要的朋友可以參考下

很多人也許會問:360被卸載之后會跳轉(zhuǎn)到指定的反饋頁面,是怎么弄的?

其實這個問題的核心就在于:應(yīng)用被卸載了,如果能夠做到后續(xù)的代碼邏輯繼續(xù)執(zhí)行

我們再來仔細(xì)分析一下場景和流程
一個應(yīng)用被用戶卸載肯定是有理由的,而開發(fā)者卻未必能得知這一重要的理由,畢竟用戶很少會主動反饋建議,多半就是用得不爽就卸,如果能在被卸載后獲取到用戶的一些反饋,那對開發(fā)者進(jìn)一步改進(jìn)應(yīng)用是非常有利的。目前據(jù)我所知,國內(nèi)的Android應(yīng)用中實現(xiàn)這一功能的只有360手機衛(wèi)士、360平板衛(wèi)士,那么如何實現(xiàn)這一功能的?

我們可以把實現(xiàn)卸載反饋的問題轉(zhuǎn)化為監(jiān)聽自己是否被卸載,只有得知自己被卸載,才可以設(shè)計相應(yīng)的反饋處理流程。以下的列表是我在研究這一問題的思路:

1、注冊BroadcastReceiver,監(jiān)聽"android.intent.action.PACKAGE_REMOVED"系統(tǒng)廣播
結(jié)果:NO。未寫代碼,直接分析,卸載的第一步就是退出當(dāng)前應(yīng)用的主進(jìn)程,而此廣播是在已經(jīng)卸載完成后才發(fā)出的,此時主進(jìn)程都沒有了,去哪onReceive()呢?

2、若能收到"將要卸載XX包"的系統(tǒng)廣播,在主進(jìn)程被退出之前就搶先進(jìn)行反饋處理就好了,可惜沒有這樣的系統(tǒng)廣播,不過經(jīng)過調(diào)研,倒是發(fā)現(xiàn)了一個辦法,讀取系統(tǒng)log,當(dāng)日志中包含"android.intent.action.DELETE"和自己的包名時,意味著自己將要被卸載。
結(jié)果:NO。調(diào)試時發(fā)現(xiàn)此方法有兩個缺陷,(1)點擊設(shè)置中的卸載按鈕即發(fā)出此Intent,此時用戶尚未在彈框中確認(rèn)卸載;(2)pm命令卸載不出發(fā)此Intent,意味著被諸如手機安全管家,豌豆莢等軟件卸載時,無法提前得知卸載意圖。

3、由于時間點不容易把控,所以干脆不依賴系統(tǒng)廣播或log,考慮到卸載過程會刪除"/data/data/包名"目錄,我們可以用線程直接輪詢這個目錄是否存在,以此為依據(jù)判斷自己是否被卸載。
結(jié)果:NO。同方法1,主進(jìn)程退出,相應(yīng)的線程必定退出,線程還沒等到判斷目錄是否存在就已經(jīng)被銷毀了。

4、改用C端進(jìn)程輪詢"/data/data/包名"目錄是否存在
結(jié)果:YES。借助Java端進(jìn)程fork出來的C端進(jìn)程在應(yīng)用被卸載后不會被銷毀。

解決的方案確定了,下面來看一下代碼吧:

#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) { 
    //出錯log 
    LOGD("fork failed..."); 
  } else if (pid == 0) { 
    //子進(jìn)程注冊"/data/data/pym.test.uninstalledobserver"目錄監(jiān)聽器 
    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); 
    } 
    //開始監(jiān)聽 
    LOGD("start observer..."); 
    size_t readBytes = read(fileDescriptor, p_buf,sizeof(struct inotify_event)); 
 
    //read會阻塞進(jìn)程,走到這里說明收到目錄被刪除的事件,注銷監(jiān)聽器 
    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,這個相關(guān)的內(nèi)容可以自行百度一下~~
這里有一個很重要的知識,也是解決這個問題的關(guān)鍵所在,就是Linux中父進(jìn)程死了,但是子進(jìn)程不會死,而是被init進(jìn)程領(lǐng)養(yǎng)。所以當(dāng)我們應(yīng)用(進(jìn)程)卸載了,但是我們fork的子進(jìn)程并不會銷毀,所以我們上述的邏輯代碼就可以放到這里來做了。(學(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(); 
    } 
  } 
 
} 

開啟一個服務(wù),在這個服務(wù)中我們可以看到,用到了一個很重要的一個類FileObserver,也是用來監(jiān)聽文件的變更的,這個和上面的inotify功能差不多。關(guān)于這個類的具體用法和介紹,可以自行百度呀~~

運行:
我們將應(yīng)用安裝之后,打開log進(jìn)行檢測日志:

adb logcat -s onEvent
2016428172750821.png (621×474)

相關(guān)文章

最新評論