gpt4 book ai didi

java - 如何请求root权限

转载 作者:行者123 更新时间:2023-12-02 00:50:00 28 4
gpt4 key购买 nike

我想在我的 Kiosk 应用程序中请求 Root 权限。我读到,当您的设备获得 root 权限时,应用程序可以获得 super 用户权限。

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

这些是我在浏览指南时已经添加的内容: list .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;
}
}

使用显示的代码,我无法获得root权限。我似乎做了一些我无法反抗的错事?知道我如何才能获得设备管理员权限吗?

编辑:

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

这行代码似乎在 5.0 及更高版本中已不再使用。

使用第一个答案中的解决方案,应用程序仍然没有根访问权限,并且这部分代码会触发,因为我仍然不是 super 用户触发:

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

return false;
}

最佳答案

启动设置应用程序启用开发者模式返回主设置菜单一直向下滚动并点击“开发者选项”选项向下滚动并点击“根访问”选项点击“仅应用程序”或“应用程序和 ADB”选项

关于java - 如何请求root权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57866864/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com