Android APP卸載守護,雙APP相互守護

來源:互聯網
上載者:User

標籤:

介紹一種當使用者或者其他軟體卸載你的應用時,其守護應用守護該應用重新安裝至系統的方案。


方案缺點:

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"; //需修改@Overridepublic 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>


程式應用:

在需要守護的應用裡添加如上代碼,因為是相互守護,需要在兩個應用裡都添加如上代碼。


Android APP卸載守護,雙APP相互守護

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.