如何请求root权限

时间:2019-09-10 08:30:55

标签: java android security permissions root

我想在Kiosk应用程序中请求Root权限。我读过,当您的设备扎根时,应用程序可以获得超级用户权限。

所以现在的主要问题是我的应用程序未在请求超级用户权限,因此Kingo,King Root等未检测到它在请求su权限。我如何要求超级用户访问权限?我要求这样做的原因是因为我想用超级用户访问权限锁定我的应用程序。因为这将是一个信息亭应用程序。

这些是我在阅读指南时已经添加的内容: Manifest.xml

        <receiver
            android:name=".MyDeviceAdminReceiver"
            android:label="@string/app_name"
            android:permission="android.permission.BIND_DEVICE_ADMIN">
            <meta-data
                android:name="android.app.device_admin"
                android:resource="@xml/device_admin" />
        </receiver>

device_admin.xml

<device-admin xmlns:android="http://schemas.android.com/apk/res/android" >

    <uses-policies>
        <limit-password />
        <watch-login />
        <reset-password />
        <force-lock />
        <wipe-data />
        <expire-password />
        <encrypted-storage />
        <disable-camera />
        <disable-keyguard-features />
        <force-lock/>
    </uses-policies>

</device-admin>

MyDeviceAdminReceiver

public class MyDeviceAdminReceiver extends DeviceAdminReceiver {

    private static final String TAG = "DeviceOwnerApp";

    @Override
    public void onLockTaskModeEntering(Context context, Intent intent, String pkg) {
        Toast.makeText(context, "Lock task mode entered", LENGTH_LONG).show();
        Log.i(TAG, "Lock task mode entered");
        Log.i(TAG, "action  : " + intent.getAction());
        Log.i(TAG, "package : " + intent.getStringExtra(DeviceAdminReceiver.EXTRA_LOCK_TASK_PACKAGE));
    }

    @Override
    public void onLockTaskModeExiting(Context context, Intent intent) {
        Toast.makeText(context, "Lock task mode exited", LENGTH_LONG).show();
        Log.i(TAG, "Lock task mode exited");
        Log.i(TAG, "action  : " + intent.getAction());
        Log.i(TAG, "package : " + intent.getStringExtra(DeviceAdminReceiver.EXTRA_LOCK_TASK_PACKAGE));
    }
}

LollipopApplocker

public class LollipopAppLocker extends AppLocker {

    public LollipopAppLocker(Context context) {
        super(context);
    }

    @TargetApi(21)
    public boolean lockApp(Activity activity) {

        if (isAppInLockTaskMode() == true) {
            Toast.makeText(mContext, "Error: app is device owner", Toast.LENGTH_SHORT).show();
            return true;
        }


            ComponentName deviceAdmin = new ComponentName(mContext, MyDeviceAdminReceiver.class);
            new AlertDialog.Builder( mContext )
                    .setTitle( "Missing privilege" )
                    .setMessage( "App needs to be device admin for all functionallities to work properly" )
                    .setPositiveButton( android.R.string.yes, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    } );

        DevicePolicyManager mDpm = (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
        if (!mDpm.isAdminActive(deviceAdmin)) {
            Toast.makeText(mContext, "Error: app is not device admin", Toast.LENGTH_SHORT).show();

            return false;
        }

        if (mDpm.isDeviceOwnerApp(mContext.getPackageName())) {
            mDpm.setLockTaskPackages(deviceAdmin, new String[]{mContext.getPackageName()});
        } else {
            Toast.makeText(mContext, "Error: app is not device owner", Toast.LENGTH_SHORT).show();
            return false;
        }

        if (mDpm.isLockTaskPermitted(mContext.getPackageName())) {
            activity.startLockTask();
        } else {
            Toast.makeText(mContext, "kiosk_not_permitted", Toast.LENGTH_SHORT).show();
            return false;
        }

        return true;
    }

    public boolean isAppInLockTaskMode() {
        ActivityManager activityManager=(ActivityManager)mContext.getSystemService(mContext.ACTIVITY_SERVICE);
        if(Build.VERSION.SDK_INT >=Build.VERSION_CODES.M) { // When SDK version is 23
            int lockTaskMode=activityManager.getLockTaskModeState();
            return lockTaskMode != ActivityManager.LOCK_TASK_MODE_NONE ? true : false;
        }
        else if(Build.VERSION.SDK_INT >=Build.VERSION_CODES.LOLLIPOP &&
                Build.VERSION.SDK_INT< Build.VERSION_CODES.M) {
            //When SDK version <=21 and <23. This API is deprecated in 23.
            return activityManager.isInLockTaskMode();
        }
        else {
            return false;
        }
    }


    @TargetApi(21)
    public boolean unlockApp(Activity activity) {

           if (isAppInLockTaskMode()) {
            activity.stopLockTask();
        }

        return true;
    }
}

使用显示的代码,我无法获得根权限。我似乎做错了我无法抗拒的东西?知道如何以其他方式获取设备管理员权限吗?

编辑:

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

这行代码似乎在5.0及更高版本中有描述。

使用第一个答案中的解决方案,该应用仍然没有root用户访问权限,这部分代码会触发,因为我仍然不是超级用户,会触发:

        if (!mDpm.isAdminActive(deviceAdmin)) {
            Toast.makeText(mContext, "Error: app is not device admin", Toast.LENGTH_SHORT).show();

            return false;
        }

1 个答案:

答案 0 :(得分:0)

启动设置应用程序 启用开发人员模式 返回主设置菜单 一直向下滚动并点击“开发人员选项”选项 向下滚动并点击“ Root Access”选项 点击“仅应用程序”或“应用程序和亚行”选项