》這篇文章。
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://blog.csdn.net/xyz_lmn* http://www.cnblogs.com/xyzlmn/* iOS入門群:83702688
* android開發進階群:241395671* 我的新浪微博:
@張興業TBOW* 我的郵箱:xy-zhang#163.com(#->@)*/