Hola tengo el siguente codigo donde estoy tratando de usar el metodo shutdown para apagar un dispositivo android desde java pero al momento de usar el metodo shutdown me dice lo siguiente: Cannot resolve method 'shutdown' in 'DevicePolicyManager' en android estudio, no encuento la manera de apagar el dispositivo:
public class MainActivity extends AppCompatActivity {
private EditText passwordEditText;
private Button shutdownButton;
private DevicePolicyManager devicePolicyManager;
private ComponentName componentName;
private AppBarConfiguration appBarConfiguration;
private ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
passwordEditText = findViewById(R.id.password_edit_text);
shutdownButton = findViewById(R.id.shutdown_button);
// Initialize device policy manager and component name
devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
componentName = new ComponentName(this, DeviceAdminReceiver.class);
shutdownButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String password = passwordEditText.getText().toString();
// Check if the password is correct
if (password.equals("mypassword")) {
// Request admin permission to shutdown the device
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
powerManager.reboot(null);
devicePolicyManager.lockNow();
devicePolicyManager.shutdown(componentName);
if (devicePolicyManager.isAdminActive(componentName)) {
//devicePolicyManager.reboot(componentName);
Toast.makeText(MainActivity.this, "Incorrect password", Toast.LENGTH_SHORT).show();
} else {
// Request admin permission
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "Please activate device admin");
startActivityForResult(intent, 0);
}
} else {
Toast.makeText(MainActivity.this, "Incorrect password", Toast.LENGTH_SHORT).show();
}
}
});
}
}