我正在开发一个Android应用程序,我们需要在某些情况下关闭设备.
我已经在很多地方读到你需要一个带根电话才能这样做.然后,您可以使用Java的API发出"reboot"命令:
try {
Process proc = Runtime.getRuntime()
.exec(new String[]{ "su", "-c", "reboot -p" });
proc.waitFor();
} catch (Exception ex) {
ex.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
我实际上已经在Cyanogenmod 10设备(三星Galaxy S3)中尝试过它,它可以工作.但是,我们不希望使用root设备来关闭它,因为最终用户将能够执行我们公司不允许的非预期事情.
另一方面,我们的申请是由制造商的证书签署的,在这种情况下是Cyanogen的.我已经读过,通过使用制造商的证书签署您的应用程序,您应该能够发出特权命令(就像root一样).但是,即使我将我的应用程序安装为使用制造商证书签名的系统应用程序,上述代码也不起作用:
如果我离开命令的"su"部分,则会显示"超级用户请求"屏幕,但这是我们试图避免的.
如果我删除"su"部分(只是离开"reboot -p"),则会默默忽略该命令.
因此,我们无法使用我们的系统应用程序关闭我们的设备,该系统应用程序使用了显示器的证书.所以我的问题是,我该怎么做呢?
EDITED
顺便说一句,万一有人不确定它:应用程序已正确签名并作为系统应用程序安装,因为我们实际上可以访问一些受限制的API,例如PowerManager.goToSleep()
如果您想让设备重启(关机再打开),请尝试 PowerManager.reboot()
PowerManager powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
powerManager.reboot(null);
Run Code Online (Sandbox Code Playgroud)
/**
* 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) {
}
}
Run Code Online (Sandbox Code Playgroud)
UPDATE
如果您希望设备完全关闭,请使用PowerManagerService.shutdown():
IPowerManager powerManager = IPowerManager.Stub.asInterface(
ServiceManager.getService(Context.POWER_SERVICE));
try {
powerManager.shutdown(false, false);
} catch (RemoteException e) {
}
Run Code Online (Sandbox Code Playgroud)
com.android.server.power.PowerManagerService:
/**
* 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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10060 次 |
| 最近记录: |