如何保持手机唤醒
来源:互联网 发布:windows预览体验 报错 编辑:程序博客网 时间:2024/04/29 15:55
手机的正常行为是闲置一段时间后屏幕变暗,然后熄灭,然后CPU关闭。
有些场景需要改变这种行为,例如播放视频时希望屏幕不要熄灭;
正在进行一些后台操作比如下载东西的时候希望CPU不要停止;
保持屏幕点亮:
在activity中执行如下code(不要在service或者其他组件调用)
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
这种做法不需要权限,一般你也无需clean这个flag,系统会管理一切。
或者在activity的layout中设置属性,这和上面的方法是一样的。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true">
...
</RelativeLayout>
保持CPU打开
需要通过PowerManager拿到wake locks,这种方式一般不用再activity中,一般用在后台service中,用于在屏幕熄灭的时候让CPU继续开启。
首先需要声明权限<uses-permission android:name="android.permission.WAKE_LOCK" />
申请
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
Wakelock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"MyWakelockTag");
wakeLock.acquire();
释放
wakeLock.release();
如果用法是你的broadcast receiver中启动一个service,这个service需要保持CPU开启,那么可以使用WakefulBroadcastReceiver。
WakefulBroadcastReceiver会创建和管理一个PARTIAL_WAKE_LOCK,它保证启动的service执行期间CPU是开启的。
(这儿提到了PARTIAL_WAKE_LOCK,一共有4种lock。)
ValueCPUScreenKeyboardPARTIAL_WAKE_LOCKOnOffOffSCREEN_DIM_WAKE_LOCKOnDimOffSCREEN_BRIGHT_WAKE_LOCKOnBrightOffFULL_WAKE_LOCKObBrightBrightpublic class SimpleWakefulController extends Activity {
Toast mToast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wakeful_alarm_controller);
// Watch for button clicks.
Button button = (Button)findViewById(R.id.schedule);
button.setOnClickListener(mScheduleListener);
}
private View.OnClickListener mScheduleListener = new View.OnClickListener() {
public void onClick(View v) {
// When the alarm goes off, we want to broadcast an Intent to our
// BroadcastReceiver. Here we make an Intent with an explicit class
// name to have our own receiver (which has been published in
// AndroidManifest.xml) instantiated and called, and then create an
// IntentSender to have the intent executed as a broadcast.
Intent intent = new Intent(SimpleWakefulController.this, SimpleWakefulReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(SimpleWakefulController.this,
0, intent, 0);
// We want the alarm to go off 30 seconds from now.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 90);
// Schedule the alarm!
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
// Tell the user about what we did.
if (mToast != null) {
mToast.cancel();
}
mToast = Toast.makeText(SimpleWakefulController.this, R.string.simple_wakeful_scheduled,
Toast.LENGTH_LONG);
mToast.show();
}
};
}
public class SimpleWakefulReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// This is the Intent to deliver to our service.
Intent service = new Intent(context, SimpleWakefulService.class);
// Start the service, keeping the device awake while it is launching.
Log.i("SimpleWakefulReceiver", "Starting service @ " + SystemClock.elapsedRealtime());
startWakefulService(context, service);
}
}
public class SimpleWakefulService extends IntentService {
public SimpleWakefulService() {
super("SimpleWakefulService");
}
@Override
protected void onHandleIntent(Intent intent) {
// At this point SimpleWakefulReceiver is still holding a wake lock
// for us. We can do whatever we need to here and then tell it that
// it can release the wakelock. This sample just does some slow work,
// but more complicated implementations could take their own wake
// lock here before releasing the receiver's.
//
// Note that when using this approach you should be aware that if your
// service gets killed and restarted while in the middle of such work
// (so the Intent gets re-delivered to perform the work again), it will
// at that point no longer be holding a wake lock since we are depending
// on SimpleWakefulReceiver to that for us. If this is a concern, you can
// acquire a separate wake lock here.
for (int i=0; i<5; i++) {
Log.i("SimpleWakefulReceiver", "Running service " + (i+1)
+ "/5 @ " + SystemClock.elapsedRealtime());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
}
Log.i("SimpleWakefulReceiver", "Completed service @ " + SystemClock.elapsedRealtime());
SimpleWakefulReceiver.completeWakefulIntent(intent);
}
}
- 如何保持手机唤醒
- 如何让手机保持唤醒状态
- rpm 包的Linux 如何让屏幕保持唤醒状态
- android 屏幕保持唤醒
- android 屏幕保持唤醒
- android屏幕保持唤醒
- Android 屏幕保持唤醒
- android 屏幕保持唤醒
- 保持屏幕唤醒
- Android 屏幕保持唤醒
- 保持设备唤醒
- 保持设备唤醒
- 保持屏幕唤醒方法
- 保持设备唤醒
- android唤醒屏幕/保持屏幕唤醒
- 如何让安卓手机实现任意键唤醒屏幕教程
- android 保持屏幕唤醒状态
- android 屏幕保持唤醒 不锁屏
- [leetcode 276] Paint Fence
- 浅谈Android客户端项目框架
- SQL基础知识4
- Spring JDBC 的使用方法
- Objective-C之方法调用机制(消息传递)
- 如何保持手机唤醒
- maven添加第三方jar包进本地maven库
- Qt 容器类学习
- generator eclipse插件的安装mybatis
- javascript、css、html来制作交互式网页的一些必备技巧
- MySQL 加锁处理分析
- Tomcat虚拟根目录与虚拟目录
- magento常用方法整理
- 技术练级攻略