我有类似的要求。我无法实现该应用程序不应该安装在有根设备上,但我使用了一种解决方法:
- 检查您的设备是否植根于您的活动的
onResume
.
- 如果它已root,只需向他显示警报“此设备已root。您无法使用此应用程序。”,然后退出应用程序。
例子:
@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
是一个实用程序类,如果设备被植根,则返回。
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 种。你可以使用任何你认为好的方法。
希望这可以帮助。
PS:我已将此调用放在所有活动中onResume
,因为用户(意图黑客)可以安装应用程序,导航到其他活动,然后是根设备。