EnjoyLife

루팅단말기일 경우 실행 차단 본문

안드로이드 개발/개발팁

루팅단말기일 경우 실행 차단

Aiden96 2023. 10. 30. 15:03

 

코틀린 버전

 private fun isRooted(): Boolean {
        val buildTags = android.os.Build.TAGS
        if (buildTags != null && buildTags.contains("test-keys")) {
            return true
        }

        try {
            val file = File("/system/app/Superuser.apk")
            if (file.exists()) {
                return true
            }
        } catch (e: Exception) {
        }

        val rootBinaryPaths = arrayOf(
            "/sbin/su", "/system/bin/su", "/system/xbin/su", "/system/sd/xbin/su",
            "/system/bin/failsafe/su", "/data/local/su", "/su/bin/su"
        )
        for (path in rootBinaryPaths) {
            try {
                val file = File(path)
                if (file.exists()) {
                    return true
                }
            } catch (e: Exception) {
            }
        }

        return false
    }

자바 버전 

public boolean isRooted() {
    Process process = null;
    try {
        process = Runtime.getRuntime().exec("su -c ls /data");
        return process.waitFor() == 0;
    } catch (Exception e) {
        return false;
    } finally {
        if (process != null) {
            process.destroy();
        }
    }
}
if(isRooted()){

            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
            alertDialogBuilder
                    .setMessage("루트권한을 가진 디바이스에서는 실행할 수 없습니다.")
                    .setCancelable(false)
                    .setPositiveButton("확인",
                            new DialogInterface.OnClickListener() {
                                public void onClick(
                                        DialogInterface dialog, int id) {

                                    moveTaskToBack(true);
                                    finish();
                                    android.os.Process.killProcess(android.os.Process.myPid());

                                }
                            });
            AlertDialog alertDialog = alertDialogBuilder.create();

            alertDialog.show();

            return;
        }