下面列出了怎么用android.os.IPowerManager的API类实例代码及写法,或者点击链接到github查看源代码。
public Object createService(ContextImpl ctx) {
IBinder b = ServiceManager.getService(POWER_SERVICE);
IPowerManager service = IPowerManager.Stub.asInterface(b);
if (service == null) {
Log.wtf(TAG, "Failed to get power manager service.");
}
return new PowerManager(ctx.getOuterContext(),
service, ctx.mMainThread.getHandler());
}
public Object createService(ContextImpl ctx) {
IBinder b = ServiceManager.getService(POWER_SERVICE);
IPowerManager service = IPowerManager.Stub.asInterface(b);
if (service == null) {
Log.wtf(TAG, "Failed to get power manager service.");
}
return new PowerManager(ctx.getOuterContext(),
service, ctx.mMainThread.getHandler());
}
/**
* Perform a full reboot of the system.
*/
void rebootSystem(String reason) {
Slog.i(TAG, "Rebooting system because: " + reason);
IPowerManager pms = (IPowerManager)ServiceManager.getService(Context.POWER_SERVICE);
try {
pms.reboot(false, reason, false);
} catch (RemoteException ex) {
}
}
@Override
public PowerManager createService(ContextImpl ctx) throws ServiceNotFoundException {
IBinder b = ServiceManager.getServiceOrThrow(Context.POWER_SERVICE);
IPowerManager service = IPowerManager.Stub.asInterface(b);
return new PowerManager(ctx.getOuterContext(),
service, ctx.mMainThread.getHandler());
}
private static void lockScreenWithIPowerManager() {
Log.v(TAG, "lockScreenWithIPowerManager()");
IPowerManager iPowerManager = IPowerManager.Stub.asInterface(ServiceManager.getService(Context.POWER_SERVICE));
if (iPowerManager == null) return;
//Go to sleep reason code: Going to sleep due by application request.
final int GO_TO_SLEEP_REASON_APPLICATION = 0;
long uptimeMillis = SystemClock.uptimeMillis();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
iPowerManager.goToSleep(uptimeMillis, GO_TO_SLEEP_REASON_APPLICATION, 0);
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
iPowerManager.goToSleep(uptimeMillis, GO_TO_SLEEP_REASON_APPLICATION);
else
iPowerManager.goToSleep(uptimeMillis);
}
@SuppressWarnings("SameParameterValue")
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private static void shutdownWithIPowerManager(boolean confirm, boolean wait) {
Log.v(TAG, "shutdownWithIPowerManager(...)");
IPowerManager iPowerManager = Objects.requireNonNull(IPowerManager.Stub.asInterface(ServiceManager.getService(Context.POWER_SERVICE)));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// @param reason code to pass to android_reboot() (e.g. "userrequested"), or null.
// @hide The value to pass as the 'reason' argument to android_reboot().
@SuppressWarnings("SpellCheckingInspection") final String SHUTDOWN_USER_REQUESTED = "userrequested";
iPowerManager.shutdown(confirm, SHUTDOWN_USER_REQUESTED, wait);
return;
}
iPowerManager.shutdown(confirm, wait);
}
private void setBrightness(int brightness) {
try {
IPowerManager power = IPowerManager.Stub.asInterface(
ServiceManager.getService("power"));
if (power != null) {
power.setTemporaryScreenBrightnessSettingOverride(brightness);
}
} catch (RemoteException doe) {
}
}
private void setBrightness(int brightness) {
try {
IPowerManager power = IPowerManager.Stub.asInterface(
ServiceManager.getService("power"));
if (power != null) {
power.setTemporaryScreenBrightnessSettingOverride(brightness);
}
} catch (RemoteException doe) {
}
}
private static boolean doSleep() {
try {
IPowerManager adapter = IPowerManager.Stub.asInterface(ServiceManager.getService("power")); // service list | grep IPowerManager
adapter.goToSleep(SystemClock.uptimeMillis(), 0, 0);
return true;
} catch (Throwable e) {
//Log.e("CmdGoToSleep.doSleep", Log.getStackTraceString(e));
PPApplication.recordException(e);
return false;
}
}
public Object createService(ContextImpl ctx) {
IBinder b = ServiceManager.getService(POWER_SERVICE);
IPowerManager service = IPowerManager.Stub.asInterface(b);
return new PowerManager(service, ctx.mMainThread.getHandler());
}
public Object createService(ContextImpl ctx) {
IBinder b = ServiceManager.getService(POWER_SERVICE);
IPowerManager service = IPowerManager.Stub.asInterface(b);
return new PowerManager(service, ctx.mMainThread.getHandler());
}
public Object createService(ContextImpl ctx) {
IBinder b = ServiceManager.getService(POWER_SERVICE);
IPowerManager service = IPowerManager.Stub.asInterface(b);
return new PowerManager(ctx.getOuterContext(),
service, ctx.mMainThread.getHandler());
}
public Object createService(ContextImpl ctx) {
IBinder b = ServiceManager.getService(POWER_SERVICE);
IPowerManager service = IPowerManager.Stub.asInterface(b);
return new PowerManager(ctx.getOuterContext(),
service, ctx.mMainThread.getHandler());
}
public Object createService(ContextImpl ctx) {
IBinder b = ServiceManager.getService(POWER_SERVICE);
IPowerManager service = IPowerManager.Stub.asInterface(b);
return new PowerManager(ctx.getOuterContext(),
service, ctx.mMainThread.getHandler());
}
PowerManagerShellCommand(IPowerManager service) {
mInterface = service;
}