我正在为我的消费构建后台服务。我需要根据特定条件重新启动设备。
我知道要使应用程序/服务重新启动设备,它必须是系统应用程序。
因为我只能使用此应用程序,所以有一种方法可以将其用于我的设备并用于所述目的(根据条件重新启动)
如果可以,请您指出如何对应用程序进行签名,使其可以成为系统应用程序,以及是否可以像其他apk一样正常安装.apk
只要条件2满足,任何有关重新启动的代码参考也将帮助我启动项目
我正在使用带有Android 6.0的MI设备。我想在不植根设备的情况下实现这一目标。
添加代码
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.
}
答案 0 :(得分:2)
如果您希望设备重启(关闭电源然后再打开),请尝试使用PowerManager.reboot()
PowerManager powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
powerManager.reboot(null);
/**
* 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);
}
}