更新时间:2022-11-06 17:08:07
我也有类似的要求。我不能做到这一点的应用程序不应安装根设备的,但我用一个变通为:
I had a similar requirement. I couldn't achieve that app should not be installed on rooted device, but I used a work around for that:
onResume
。 示例:
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
if(new DeviceUtils().isDeviceRooted(getApplicationContext())){
showAlertDialogAndExitApp("This device is rooted. You can't use this app.");
}
}
public void showAlertDialogAndExitApp(String message) {
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Alert");
alertDialog.setMessage(message);
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
}
});
alertDialog.show();
}
DeviceUtis.java
是如果一个设备是植根与否而返回的实用工具类。
DeviceUtis.java
was a Utility class which returned if a device is rooted or not.
public class DeviceUtils {
public Boolean isDeviceRooted(Context context){
boolean isRooted = isrooted1() || isrooted2();
return isRooted;
}
private boolean isrooted1() {
File file = new File("/system/app/Superuser.apk");
if (file.exists()) {
return true;
}
return false;
}
// try executing commands
private boolean isrooted2() {
return canExecuteCommand("/system/xbin/which su")
|| canExecuteCommand("/system/bin/which su")
|| canExecuteCommand("which su");
}
}
我们曾用5种方法进行测试,和我刚才2所示这里。您可以使用任何方法你觉得不错的。
We had used 5 methods for testing, and I have just shown 2 here. You can use any of methods you find good.
希望这有助于。
PS:我已经把这个调用的所有活动的 onResume
登录(与黑客的意图),可以安装应用程序,浏览到一些其他的活动,然后根设备。
P.S: I have put this call in all activity's onResume
as user (with intention of hacking) can install application, navigate to some other activity, and then root device.