Android利用Logcat监听应用程序本身被卸载

MainActivity如下:

package cc.testremoveapp;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
/**
 * Demo描述:
 * 监听应用程序本身被卸载
 * 
 * 注意权限:
 * <uses-permission android:name="android.permission.READ_LOGS"></uses-permission>
 * 
 * 参考资料:
 * 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如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="监听应用本身被卸载"
        android:layout_centerInParent="true"
        />

</RelativeLayout>


uninstall.xml如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="确定卸载本应用?"
        android:layout_centerInParent="true"
        />

</RelativeLayout>


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谷哥的小弟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值