简体   繁体   中英

How to ask for root permission

I want to ask for Root permissions in my Kiosk app. I've read that an App can get superUser permissions when you'r device is rooted.

So the main problem right now is that my app is not asking for superUser permission so Kingo, King Root etc. are not detecting it asking for su rights. How do i ask for SuperUser Access? The reason I ask for this is because I want to lock my app with the superUser access. As this will be a kiosk app.

These are the things I added already when I walked through a guide: 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;
    }
}

With the shown code, I can't get root rights. and I seem to do something wrong which I can't fight? Any idea how I can get device admin rights otherwise?

Edit:

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

This line of code seems depricated with 5.0 and onwards.

Using the Solution from the first answer the app still has no root access and this part of code fires as I'm still not a superuser fires:

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

            return false;
        }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM