按钮点击屏幕锁定

时间:2013-01-15 07:08:48

标签: android

我想按下按钮点击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());
             }


        }
    });


}

我想锁定屏幕,我该怎么办?

2 个答案:

答案 0 :(得分: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"

答案 1 :(得分:0)

我猜你已经添加了许可 uses-permission android:name =“android.permission.WAKE_LOCK”