gpt4 book ai didi

android - 以编程方式重启设备

转载 作者:行者123 更新时间:2023-11-29 18:50:16 25 4
gpt4 key购买 nike

我正在构建仅供我使用的后台服务。我需要根据特定条件重启我的设备。

  1. 我了解要重启设备的应用程序/服务,它需要是系统应用程序。

  2. 既然我只会使用这个应用程序,有没有办法为我的设备签名并将其用于所述目的(根据条件重新启动)

  3. 如果是,请指点如何对一个应用进行签名,使其成为一个系统应用,如果.apk可以像其他apk一样正常安装

  4. 如果条件 2 满足,任何重启的代码引用也将帮助我启动项目

我用的是小米手机,Android 6.0。我想在不对我的设备进行 root 的情况下实现这一目标。

添加代码

        if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.REBOOT)
!= PackageManager.PERMISSION_GRANTED) {


System.out.println("Permission Ask");

if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
Manifest.permission.REBOOT)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
Toast.makeText(MainActivity.this,"Explain",Toast.LENGTH_LONG).show();
} else {

ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.REBOOT},
MY_PERMISSIONS_REQUEST_REBOOT);
}

} else {
// Permission has already been granted
Toast.makeText(MainActivity.this,"You have been granter permission",Toast.LENGTH_LONG).show();
}

@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {

if (requestCode == MY_PERMISSIONS_REQUEST_REBOOT) {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.

Toast.makeText(MainActivity.this,"Permission Done",Toast.LENGTH_LONG).show();
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.

Toast.makeText(MainActivity.this, "Permission Not Done", Toast.LENGTH_LONG).show();

}
}

// other 'case' lines to check for other
// permissions this app might request.
}

最佳答案

如果你想让设备重启(关机再开机),试试PowerManager.reboot()

PowerManager powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
powerManager.reboot(null);

android.os.PowerManager:

  /**
* Reboot the device. Will not return if the reboot is successful.
* <p>
* Requires the {@link android.Manifest.permission#REBOOT} permission.
* </p>
*
* @param reason code to pass to the kernel (e.g., "recovery") to
* request special boot modes, or null.
*/
public void reboot(String reason) {
try {
mService.reboot(false, reason, true);
} catch (RemoteException e) {
}
}

如果您想完全关闭设备,请使用 PowerManagerService.shutdown()

  IPowerManager powerManager = IPowerManager.Stub.asInterface(
ServiceManager.getService(Context.POWER_SERVICE));
try {
powerManager.shutdown(false, false);
} catch (RemoteException e) {
}




/**
* Shuts down the device.
*
* @param confirm If true, shows a shutdown confirmation dialog.
* @param wait If true, this call waits for the shutdown to complete and does not return.
*/
@Override // Binder call
public void shutdown(boolean confirm, boolean wait) {
mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null);

final long ident = Binder.clearCallingIdentity();
try {
shutdownOrRebootInternal(true, confirm, null, wait);
} finally {
Binder.restoreCallingIdentity(ident);
}
}

关于android - 以编程方式重启设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51056155/

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