android實現卸載提示

來源:互聯網
上載者:User

       這篇文章是整理的以前用過的一個功能,由於多種原因現在停用了,也希望這篇文章能協助一些android入門的小童鞋。android是不提供監控卸載自己的功能的,這裡使用了監控android日誌的功能,android日誌相關知識可以參考《adb logcat 查看日誌》這篇文章。

        android卸載提示的思路是啟動一個服務監控android系統的列印日誌,當監控到"android.intent.action.DELETE"並且包含自己應用的包名時,提示給使用者。

監控代碼

public class AndroidLogcatScannerThread extends Thread {    private LogcatObserver observer;    public AndroidLogcatScannerThread(LogcatObserver observer) {            this.observer = observer;    }    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 {                    observer.handleLog(line);                    int waitValue;                    waitValue = runtime.exec(cmds).waitFor();                    observer.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) {                    //Log.d("Log","Log.Bestpay:"+line);                                        if(observer!=null)                            observer.handleLog(line);                                                  }            } catch (InterruptedException e) {                    e.printStackTrace();            } catch (IOException ie) {                    ie.printStackTrace();            } finally {                    try {                            if (dis != null) {                                    dis.close();                            }                            if (is != null) {                                    is.close();                            }                            if (process != null) {                                    process.destroy();                            }                    } catch (Exception e) {                            e.printStackTrace();                    }            }    }}

監控服務:

public class AndroidLogcatScannerService extends Service implements LogcatObserver{@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate();}@Overridepublic void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();}@Overridepublic void onStart(Intent intent, int startId) {// TODO Auto-generated method stubsuper.onStart(intent, startId);AndroidLogcatScannerThread scannerThread=new AndroidLogcatScannerThread(AndroidLogcatScannerService.this);scannerThread.start();}@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn null;}@Overridepublic void handleLog(String info) {// TODO Auto-generated method stubif (info.contains("android.intent.action.DELETE") && info.contains(getPackageName())) {            Intent intent = new Intent();            intent.setClass(AndroidLogcatScannerService.this, UninstallActivity.class);            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);            startActivity(intent);        }}}

上面的代碼基本實現了卸載提示,最後不要忘了許可權:

<uses-permission android:name="android.permission.READ_LOGS"></uses-permission>

代碼:

http://download.csdn.net/detail/xyz_lmn/4904797

 /**
* @author 張興業
* 郵箱:xy-zhang#163.com
* android開發進階群:278401545
* http://blog.csdn.net/xyz_lmn
*/

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.