Android APP卸载守护,双APP相互守护

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


方案缺点:

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;
	}

添加权限

<uses-permission android:name="android.permission.INSTALL_PACKAGES" />

添加receiver

<receiver android:name=".ProtectReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_REMOVED" />
                <data android:scheme="package" />
            </intent-filter>
</receiver>


程序应用:

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


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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值