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

原创
2015/03/21 16:15
阅读数 5.5K

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


方案缺点:

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>


程序应用:

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


展开阅读全文
加载中
点击加入讨论🔥(9) 发布并加入讨论🔥
打赏
9 评论
71 收藏
0
分享
返回顶部
顶部