Android利用Logcat监听应用程序本身被卸载
发布日期:2021-06-30 11:11:12 浏览次数:2 分类:技术文章

本文共 3708 字,大约阅读时间需要 12 分钟。

MainActivity如下:

package cc.testremoveapp;import android.os.Bundle;import android.app.Activity;import android.content.Intent;/** * Demo描述: * 监听应用程序本身被卸载 *  * 注意权限: * 
* * 参考资料: * http://blog.csdn.net/xyz_lmn/article/details/8330710 * Thank you very much */public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } //启动服务 private void init(){ Intent intent=new Intent(this, LogcatScannerService.class); startService(intent); } }

LogcatObserverInterface如下:

package cc.testremoveapp;//业务接口public interface LogcatObserverInterface {    public void handleLog(String logcatInfo);}

LogcatScannerService如下:

package cc.testremoveapp;import android.app.Service;import android.content.Intent;import android.os.IBinder;public class LogcatScannerService extends Service implements LogcatObserverInterface {	@Override	public void onCreate() {		super.onCreate();	}		@Override	public void onStart(Intent intent, int startId) {		super.onStart(intent, startId);		LogcatScannerThread logcatScannerThread=new LogcatScannerThread(this);		logcatScannerThread.start();	}		@Override	public IBinder onBind(Intent arg0) {		return null;	}		@Override	public void onDestroy() {		super.onDestroy();	}		/**	 * 实现LogcatObserverInterface接口中的方法	 */	@Override	public void handleLog(String logcatInfo) {		if (logcatInfo.contains("android.intent.action.DELETE")&& logcatInfo.contains(getPackageName())) {			/**			 * 注意事项:			 * LogCat有时会多次甚至一直输出卸载应用的信息			 * 所以在实际项目中需要对此处留意处理			 */			Intent intent = new Intent(LogcatScannerService.this,UninstallActivity.class);			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);			startActivity(intent);		}	}}

LogcatScannerThread如下:

package cc.testremoveapp;import java.io.DataInputStream;import java.io.InputStream;public class LogcatScannerThread extends Thread {		private LogcatObserverInterface mLogcatObserverInterface;		public LogcatScannerThread(LogcatObserverInterface logcatObserverInterface){		this.mLogcatObserverInterface=logcatObserverInterface;	}	@Override	public void run() {		super.run();		int waitValue;		String line = "";        String[] cmds = { "logcat", "-c" };        String shellCMD = "logcat";        Process process = null;        InputStream inputStream = null;        DataInputStream dataInputStream = null;        Runtime runtime = Runtime.getRuntime();        try {        	    mLogcatObserverInterface.handleLog(line);                waitValue = runtime.exec(cmds).waitFor();                mLogcatObserverInterface.handleLog("waitValue=" + waitValue + "\n Has do Clear logcat cache.");                process = runtime.exec(shellCMD);                inputStream = process.getInputStream();                dataInputStream = new DataInputStream(inputStream);                while ((line = dataInputStream.readLine()) != null) {                	if(mLogcatObserverInterface!=null){                		mLogcatObserverInterface.handleLog(line);                	}                }        } catch (Exception e) {                e.printStackTrace();		} finally {			try {				if (dataInputStream != null) {					dataInputStream.close();				}				if (inputStream != null) {					inputStream.close();				}				if (process != null) {					process.destroy();				}			} catch (Exception e) {				e.printStackTrace();			}		}	}}

UninstallActivity如下:

package cc.testremoveapp;import android.app.Activity;import android.os.Bundle;public class UninstallActivity extends Activity {	@Override	protected void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.uninstall);	}	}

main.xml如下:

uninstall.xml如下:

 

转载地址:https://it9527.blog.csdn.net/article/details/10450849 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Collections工具类常用方法
下一篇:手把手教你上传女神照片到服务器

发表评论

最新留言

不错!
[***.144.177.141]2024年04月09日 03时23分01秒