我是android编程的新手。在这段代码中,我有一个按钮,当我按下按钮时,屏幕应该关闭。任何人都可以帮我这个代码吗?
当我按下按钮时,即使我将其释放,也没有执行任何操作并且屏幕已开启。
我在清单文件中使用了以下权限,如下所示
uses-permission android:name="android.permission.WAKE_LOCK"
protected static final String TAG = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button powerButton = (Button) findViewById(R.id.powerButton);
final PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
final PowerManager.WakeLock wakeLock = powerManager.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK, "powerButton");
powerButton.setOnClickListener(new View.OnClickListener() {
@SuppressLint("Wakelock")
public void onClick(View v) {
try {
if (wakeLock != null) {
Log.d(TAG, "Acquire full screen wake lock.");
wakeLock.acquire();
}
if (powerManager.isScreenOn()) {
Log.d("TAG", "Wakelock successfully acquired");
} else {
Log.d("TAG", "Wackelock not acquired");
}
if (wakeLock.isHeld()) {
wakeLock.release();
Log.d("TAG", "wakelock released sucessfully");
}
} catch (Exception e) {
e.printStackTrace();
Log.e("onClick", e.getMessage());
}
}
});
}
}
请帮帮我。
提前感谢你。
答案 0 :(得分:0)
这是一个Sample,可以在按钮点击时关闭屏幕。该操作发生在mButtonStopListener
public class Main extends Activity {
private SensorManager mSensorManager;
private PowerManager mPowerManager;
private WindowManager mWindowManager;
private WakeLock mWakeLock;
private Button button;
private TextView textView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try{
// Get an instance of the SensorManager
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
// Get an instance of the PowerManager
mPowerManager = (PowerManager) getSystemService(POWER_SERVICE);
// Get an instance of the WindowManager
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
mWindowManager.getDefaultDisplay();
// Create a bright wake lock
mWakeLock = mPowerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, getClass()
.getName());
setContentView(R.layout.main);
textView = (TextView)findViewById(R.id.textView1);
button = (Button)findViewById(R.id.button1);
button.setOnClickListener(mButtonStopListener);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("onCreate", e.getMessage());
}
} // END onCreate
View.OnClickListener mButtonStopListener = new OnClickListener() {
public void onClick(View v) {
try {
mWakeLock.release();
textView.setText("mWakeLock.release()");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("onPause",e.getMessage());
}
}
};
@Override
protected void onResume() {
super.onResume();
/*
* when the activity is resumed, we acquire a wake-lock so that the
* screen stays on, since the user will likely not be fiddling with the
* screen or buttons.
*/
try {
mWakeLock.acquire();
textView.setText("mWakeLock.acquire()");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("onResume", e.getMessage());
}
}
@Override
protected void onPause() {
super.onPause();
// and release our wake-lock
try {
mWakeLock.release();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("onPause",e.getMessage());
}
}
}
确保包含
<uses-permission android:name="android.permission.HARDWARE_TEST"></uses-permission>
<uses-permission android:name="android.permission.VIBRATE"></uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
这应该从API级别7&amp;起来。
答案 1 :(得分:0)
在您从onClick
释放手指之前,不会调用powerButton
回调方法。因此,当您按下按钮时,没有执行任何操作。