android監聽應用自身被卸載

來源:互聯網
上載者:User

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

通過監聽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的發送!

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

 


 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.