AndroidManifest.xml 中 加权限 <uses-permission android:name="android.permission.WAKE_LOCK"/>
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
public class MainActivity extends Activity {
WakeLock wakeLock;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//初始化
PowerManager powerManager = (PowerManager)this.getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK,"My Lock");
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
protected void onResume() {
super.onResume();
//获取
wakeLock.acquire();
}
@Override
protected void onPause() {
super.onPause();
//释放
wakeLock.release();
}
}