Android反射调用goToSleep

goToSleep的函数原型为 public void goToSleep(long time)

public void goToSleep(Context context) {
        PowerManager powerManager= (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        try {
            powerManager.getClass().getMethod("goToSleep", new Class[]{long.class}).invoke(powerManager, SystemClock.uptimeMillis());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }

这里需要注意的是:只有系统级别的应用才能使goToSleep生效,并需要声明权限

android:name="android.permission.DEVICE_POWER"

 

你可能感兴趣的:(Android反射调用goToSleep)