Android APP卸载守护,双APP相互守护
发布日期:2021-06-24 18:45:03 浏览次数:2 分类:技术文章

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

  hot3.png

介绍一种当用户或者其他软件卸载你的应用时,其守护应用守护该应用重新安装至系统的方案。

方案缺点:

1、当具有root权限的删除时无法守护 (rm data/app/*.apk)

2、当为非root手机时,采用显示安装,用户可选择取消安装

方案优点:

1、使用传统手法卸载软件时具有root权限时可顽固守护

2、双应用守护,无法卸载其中任何一个

方案原理:

采用BroadcastReceiver 接收拦截的应用卸载的消息,(由于系统原因,当接收到消息时,系统已经卸载了该应用才发出的消息,所以无法像短信拦截一样直接拦截)。

1、 接收到应用卸载信息

2、判断包名是否为需要守护的包名

3、如果是则启动重新安装

4、使用静默安装

5、如果失败使用普通安装

方案代码:

BroadcastReceiver

public class ProtectReceiver extends BroadcastReceiver {	private static final String TAG = "ProtectReceiver";	private String ACTION_PACKAGE_REMOVED = "android.intent.action.PACKAGE_REMOVED";	private String PRO_APK_PATH = "/sdcard/test.apk"; //需修改	@Override	public void onReceive(Context context, Intent intent) {		if (ACTION_PACKAGE_REMOVED.equals(intent.getAction())) {		        //需修改			if ("package:com.rapida.test".equals(intent.getDataString())) {				reInstallApp(context);			}		}	}	private void reInstallApp(Context context) {		if (!installSlient(context, PRO_APK_PATH)) {			install(context, PRO_APK_PATH);		}	}	private void install(Context context, String filePath) {		Intent i = new Intent(Intent.ACTION_VIEW);		i.setDataAndType(Uri.parse("file://" + filePath),				"application/vnd.android.package-archive");		i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);		context.startActivity(i);	}	private boolean installSlient(Context context, String filePath) {		String[] args = { "pm", "install", "-r", filePath };		ProcessBuilder processBuilder = new ProcessBuilder(args);		Process process = null;		BufferedReader successResult = null;		BufferedReader errorResult = null;		StringBuilder successMsg = new StringBuilder();		StringBuilder errorMsg = new StringBuilder();		boolean result = false;		try {			process = processBuilder.start();			successResult = new BufferedReader(new InputStreamReader(					process.getInputStream()));			errorResult = new BufferedReader(new InputStreamReader(					process.getErrorStream()));			String s;			while ((s = successResult.readLine()) != null) {				successMsg.append(s);			}			while ((s = errorResult.readLine()) != null) {				errorMsg.append(s);			}		} catch (IOException e) {			e.printStackTrace();			result = false;		} catch (Exception e) {			e.printStackTrace();			result = false;		} finally {			try {				if (successResult != null) {					successResult.close();				}				if (errorResult != null) {					errorResult.close();				}			} catch (IOException e) {				e.printStackTrace();			}			if (process != null) {				process.destroy();			}		}		if (successMsg.toString().contains("Success")				|| successMsg.toString().contains("success")) {			result = true;		} else {			result = false;		}		return result;	}

添加权限

添加receiver

            
                
                
            

程序应用:

在需要守护的应用里添加如上代码,因为是相互守护,需要在两个应用里都添加如上代码。

转载于:https://my.oschina.net/oldmou/blog/389750

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

上一篇:web前端javascript数组去重
下一篇:Qunee for HTML5 v1.5正式发布

发表评论

最新留言

很好
[***.229.124.182]2024年04月03日 11时12分42秒