添加屏幕待机方法

import android.os.SystemClock;
android.os.PowerManager;
//添加屏幕待机方法
    public static void goToSleep(Context context,long time){
     context.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
      final AlertDialog dialog = new AlertDialog.Builder(context)
      .setIcon(android.R.drawable.ic_dialog_alert)
      .setTitle(com.android.internal.R.string.sleep)
      .setMessage(com.android.internal.R.string.sleep_confirm)
      .setPositiveButton(com.android.internal.R.string.yes,
                                   new DialogInterface.OnClickListener() {
                                       public void onClick
                                           (DialogInterface dialog, int which){
                                           PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
                                           pm.goToSleep(SystemClock.uptimeMillis());
                                       }
                                   })
            .setNegativeButton(com.android.internal.R.string.no, null)
            .create();
      dialog.getWindow().setType
            (WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
        dialog.getWindow().addFlags
            (WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
        dialog.show();
    
      }

你可能感兴趣的:(android,OS)