监听王者荣耀被安装,然后自动卸载

王者农药是世界上最好玩的游戏~

真的受不了了这个傻鸟游戏,赢一把输一把新英雄个个三头六臂机制独特,老英雄个个变异身体羸弱

只是传统(traditional)的长按图标卸载还是太吃操作了,免不了第五(five)人格迸发奇迹般地去应用商店给下载回来,吃到史后第五(five)人格消退继续卸载…有没有既简单又强势的方法推荐下?

有的 有的

今日下午突发奇想,是否存在一种广播,能够在一个新应用添加后于系统中传递着,且能够被我们的应用所敏锐地捕获到呢?

于直觉中诞生的PACKAGE_ADDED

对某一领域的经验越多,当事人的生活往往越不会枯燥,有许多妙不可言的感觉使人沉溺其中,美妙的直觉,便来源于此

妙啊

IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
filter.addDataScheme("package");
filter.addCategory(Intent.CATEGORY_DEFAULT);
context.registerReceiver(myReceiver, filter);

创建AppInstallReceiver类,实现系统广播的监听

直接上代(damn)码

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;

import java.io.DataOutputStream;
import java.io.IOException;

public class AppInstallReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // 获取包名
        String packageName = intent.getData().getSchemeSpecificPart();

        // 判断是否是王者荣耀的包名
        if ("com.tencent.tmgp.sgame".equals(packageName)) {
            // 如果是王者荣耀被安装,就卸载
                uninstallApp();
        }
    }

    private void uninstallApp() throws IOException {
        // 获取 root 权限
        Process process = Runtime.getRuntime().exec("su");
        DataOutputStream outputStream = new DataOutputStream(process.getOutputStream());

        // 执行卸载命令
        outputStream.writeBytes("pm uninstall com.tencent.tmgp.sgame\n");
        outputStream.flush();
        outputStream.writeBytes("exit\n");
        outputStream.flush();

        try {
            process.waitFor();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void registerReceiver(Context context) {
        IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
        filter.addDataScheme("package");
        context.registerReceiver(new AppInstallReceiver(), filter);
    }
}

使用

import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private static final String PACKAGE_NAME = "com.tencent.tmgp.sgame";  // 王者荣耀的包名

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 注册接收安装事件的广播
        AppInstallReceiver.registerReceiver(this);

        // 如果此时安装了王者荣耀,则立即卸载
        if (isAppInstalled(PACKAGE_NAME)) {
            uninstallApp();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 注销广播接收器
        AppInstallReceiver.unregisterReceiver(this);
    }

    /**
     * 检查指定包名的应用是否已安装
     */
    private boolean isAppInstalled(String packageName) {
        PackageManager packageManager = getPackageManager();
        try {
            packageManager.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
            return true; // 如果能找到该包名,表示已安装
        } catch (PackageManager.NameNotFoundException e) {
            return false; // 如果找不到,表示未安装
        }
    }

    /**
     * 强制卸载应用
     */
    private void uninstallApp() throws IOException {
        // 获取 root 权限
        Process process = Runtime.getRuntime().exec("su");
        DataOutputStream outputStream = new DataOutputStream(process.getOutputStream());

        // 执行卸载命令
        outputStream.writeBytes("pm uninstall " + PACKAGE_NAME + "\n");
        outputStream.flush();
        outputStream.writeBytes("exit\n");
        outputStream.flush();

        try {
            process.waitFor();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

编译生成的应用需要Root权限,请在您家孩子设备的超级用户管理器内进行授权

LICENSED UNDER CC BY-NC-SA 4.0
Comment