Android利用Logcat监听应用程序本身被卸载
16lz
2021-01-23
分类:Android 2013-08-28 15:31 318人阅读 评论(0) 收藏 举报
MainActivity如下:
[java] view plain copy- packagecc.testremoveapp;
- importandroid.os.Bundle;
- importandroid.app.Activity;
- importandroid.content.Intent;
- /**
- *Demo描述:
- *监听应用程序本身被卸载
- *
- *注意权限:
- *<uses-permissionandroid:name="android.permission.READ_LOGS"></uses-permission>
- *
- *参考资料:
- *http://blog.csdn.net/xyz_lmn/article/details/8330710
- *Thankyouverymuch
- */
- publicclassMainActivityextendsActivity{
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- init();
- }
- //启动服务
- privatevoidinit(){
- Intentintent=newIntent(this,LogcatScannerService.class);
- startService(intent);
- }
- }
LogcatObserverInterface如下:
- packagecc.testremoveapp;
- //业务接口
- publicinterfaceLogcatObserverInterface{
- publicvoidhandleLog(StringlogcatInfo);
- }
LogcatScannerService如下:
- packagecc.testremoveapp;
- importandroid.app.Service;
- importandroid.content.Intent;
- importandroid.os.IBinder;
- publicclassLogcatScannerServiceextendsServiceimplementsLogcatObserverInterface{
- @Override
- publicvoidonCreate(){
- super.onCreate();
- }
- @Override
- publicvoidonStart(Intentintent,intstartId){
- super.onStart(intent,startId);
- LogcatScannerThreadlogcatScannerThread=newLogcatScannerThread(this);
- logcatScannerThread.start();
- }
- @Override
- publicIBinderonBind(Intentarg0){
- returnnull;
- }
- @Override
- publicvoidonDestroy(){
- super.onDestroy();
- }
- /**
- *实现LogcatObserverInterface接口中的方法
- */
- @Override
- publicvoidhandleLog(StringlogcatInfo){
- if(logcatInfo.contains("android.intent.action.DELETE")&&logcatInfo.contains(getPackageName())){
- /**
- *注意事项:
- *LogCat有时会多次甚至一直输出卸载应用的信息
- *所以在实际项目中需要对此处留意处理
- */
- Intentintent=newIntent(LogcatScannerService.this,UninstallActivity.class);
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- startActivity(intent);
- }
- }
- }
LogcatScannerThread如下:
- packagecc.testremoveapp;
- importjava.io.DataInputStream;
- importjava.io.InputStream;
- publicclassLogcatScannerThreadextendsThread{
- privateLogcatObserverInterfacemLogcatObserverInterface;
- publicLogcatScannerThread(LogcatObserverInterfacelogcatObserverInterface){
- this.mLogcatObserverInterface=logcatObserverInterface;
- }
- @Override
- publicvoidrun(){
- super.run();
- intwaitValue;
- Stringline="";
- String[]cmds={"logcat","-c"};
- StringshellCMD="logcat";
- Processprocess=null;
- InputStreaminputStream=null;
- DataInputStreamdataInputStream=null;
- Runtimeruntime=Runtime.getRuntime();
- try{
- mLogcatObserverInterface.handleLog(line);
- waitValue=runtime.exec(cmds).waitFor();
- mLogcatObserverInterface.handleLog("waitValue="+waitValue+"\nHasdoClearlogcatcache.");
- process=runtime.exec(shellCMD);
- inputStream=process.getInputStream();
- dataInputStream=newDataInputStream(inputStream);
- while((line=dataInputStream.readLine())!=null){
- if(mLogcatObserverInterface!=null){
- mLogcatObserverInterface.handleLog(line);
- }
- }
- }catch(Exceptione){
- e.printStackTrace();
- }finally{
- try{
- if(dataInputStream!=null){
- dataInputStream.close();
- }
- if(inputStream!=null){
- inputStream.close();
- }
- if(process!=null){
- process.destroy();
- }
- }catch(Exceptione){
- e.printStackTrace();
- }
- }
- }
- }
UninstallActivity如下:
- packagecc.testremoveapp;
- importandroid.app.Activity;
- importandroid.os.Bundle;
- publicclassUninstallActivityextendsActivity{
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.uninstall);
- }
- }
main.xml如下:
- <RelativeLayoutxmlns: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如下:
- <RelativeLayoutxmlns: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>
更多相关文章
- 【Android】简单的接口回调
- Android framework学习参考资料
- 【APP】微信接口对Android与IOS验证方式的差异
- Android 接口回调机制详解
- [置顶] Android本地接口JNI的使用分析
- Android行驶证离线识别sdk接口