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>


 

上一篇:一行命令:从 ELK 迁移日志服务


下一篇:Aliyun LOG Java Producer 快速入门