Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android監聽應用自身被卸載

android監聽應用自身被卸載

編輯:關於Android編程

在實際開發中,常常需要監聽應用本身是否被卸載或相近的需求。在網上淘了很久都沒有看到實際的做法,最多就給出一個思路,可以通過捕捉系統日志來檢測到這個應用是否被卸載,繼而做相關的操作。

通過監聽Intent.ACTION_PACKAGE_REMOVED意圖只能監聽到其他應用程序是否被卸載,無法監聽自身!

        本例子也是通過監聽系統日志,來監聽應用本身是否被卸載。

LogcatObserver.java

  

public interface LogcatObserver { 
     
    public void handleLog(String info); 
} 

public interface LogcatObserver {
   
    public void handleLog(String info);
}
LogcatScannerService.java



import java.io.DataInputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import android.app.Service; 
import android.content.Intent; 
import android.os.IBinder; 
 
public class LogcatScannerService extends Service implements LogcatObserver { 
    @Override 
    public void onStart(Intent intent, int startId) { 
        super.onStart(intent, startId); 
        new AndroidLogcatScannerThread(this).start(); 
    } 
 
    @Override 
    public void handleLog(String info) { 
        if (info.contains("android.intent.action.DELETE") 
                && info.contains(getPackageName())) { 
            //do something yourself  
        } 
    } 
 
    private class AndroidLogcatScannerThread extends Thread { 
 
        private LogcatObserver mObserver; 
 
        public AndroidLogcatScannerThread(LogcatObserver observer) { 
            mObserver = observer; 
        } 
 
        @Override 
        public void run() { 
            String[] cmds = { "logcat", "-c" }; 
            String shellCmd = "logcat"; 
            Process process = null; 
            InputStream is = null; 
            DataInputStream dis = null; 
            String line = ""; 
            Runtime runtime = Runtime.getRuntime(); 
            try { 
                mObserver.handleLog(line); 
                int waitValue; 
                waitValue = runtime.exec(cmds).waitFor(); 
                mObserver.handleLog("waitValue=" + waitValue 
                        + "\n Has do Clear logcat cache."); 
                process = runtime.exec(shellCmd); 
                is = process.getInputStream(); 
                dis = new DataInputStream(is); 
                while ((line = dis.readLine()) != null) { 
                    if (mObserver != null) 
                        mObserver.handleLog(line); 
 
                } 
            } catch (InterruptedException e) { 
                e.printStackTrace(); 
            } catch (IOException ie) { 
            } finally { 
                try { 
                    if (dis != null) { 
                        dis.close(); 
                    } 
                    if (is != null) { 
                        is.close(); 
                    } 
                    if (process != null) { 
                        process.destroy(); 
                    } 
                } catch (Exception e) { 
                } 
            } 
        } 
    } 
     
    @Override 
    public IBinder onBind(Intent intent) { 
        return null; 
    } 
 
} 

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class LogcatScannerService extends Service implements LogcatObserver {
    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        new AndroidLogcatScannerThread(this).start();
    }

    @Override
    public void handleLog(String info) {
        if (info.contains("android.intent.action.DELETE")
                && info.contains(getPackageName())) {
            //do something yourself
        }
    }

    private class AndroidLogcatScannerThread extends Thread {

        private LogcatObserver mObserver;

        public AndroidLogcatScannerThread(LogcatObserver observer) {
            mObserver = observer;
        }

        @Override
        public void run() {
            String[] cmds = { "logcat", "-c" };
            String shellCmd = "logcat";
            Process process = null;
            InputStream is = null;
            DataInputStream dis = null;
            String line = "";
            Runtime runtime = Runtime.getRuntime();
            try {
                mObserver.handleLog(line);
                int waitValue;
                waitValue = runtime.exec(cmds).waitFor();
                mObserver.handleLog("waitValue=" + waitValue
                        + "\n Has do Clear logcat cache.");
                process = runtime.exec(shellCmd);
                is = process.getInputStream();
                dis = new DataInputStream(is);
                while ((line = dis.readLine()) != null) {
                    if (mObserver != null)
                        mObserver.handleLog(line);

                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (IOException ie) {
            } finally {
                try {
                    if (dis != null) {
                        dis.close();
                    }
                    if (is != null) {
                        is.close();
                    }
                    if (process != null) {
                        process.destroy();
                    }
                } catch (Exception e) {
                }
            }
        }
    }
   
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

}

        調用時只需startService(this,LogcatScannerService.class)既可。
        例子本身,經過測試確定可以監聽到應用本身被卸載的動作,但是在handleLog()方法中我能只能做些省時的操作,撸主測試可以發出短信,但是無法完成http的發送!

例子本身是通過不斷的讀日志來監聽應用本身是否被卸載,因而是非常好電和性能的!如果廣大網友有更好的想法可以一起討論下啊!

 


 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved