2

我想在按钮单击时打开 android 屏幕,现在我为此编写了一个程序,没有显示任何错误,也没有工作..

代码是..

public class MainActivity extends Activity {

Button powerOff;
int amountOfTime =20*1000;
Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);




    powerOff = (Button)findViewById(R.id.button1);
    powerOff.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


            // TODO Auto-generated method stub
            PowerManager.WakeLock mWLock;
             try {
                  System.out.println("Enter try Block");

                  PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
                  mWLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
                          PowerManager.ACQUIRE_CAUSES_WAKEUP |
                          PowerManager.ON_AFTER_RELEASE, "WakeLock");
                  mWLock.acquire();
             } catch(Exception e) {
                    Log.e("ScreenLock", "onStart()::acquire() failed " + e.toString());
             }


        }
    });


}

我想锁屏,怎么办???

4

2 回答 2

2

使用以下代码

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "我的标签"); wl.acquire();

在活动中你必须释放唤醒锁

wl.release();

在清单中使用以下权限

使用权限 android:name="android.permission.WAKE_LOCK"

于 2013-01-15T10:54:20.457 回答
0

我猜你已经添加了权限 uses-permission android:name="android.permission.WAKE_LOCK"

于 2013-01-15T07:48:06.907 回答