保持设备唤醒


保持设备唤醒两种类型:

1、保持屏幕常亮;

2、保持cpu运行;



1、保持屏幕常亮方法;


public class MainActivity extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  }
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:keepScreenOn="true">
    ...
</RelativeLayout>

2、保持cpu运行方法

为了使用唤醒锁,首先需要在应用的 Manifest 清单文件中增加 WAKE_LOCK 权限:

<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()来释放唤醒锁。当应用使用完毕时,应该释放该唤醒锁,以避免电量过度消耗。

使用WakefulBroadcastReceiver

<receiver android:name=".MyWakefulReceiver"></receiver>
public class MyWakefulReceiver extends WakefulBroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        // Start the service, keeping the device awake while the service is
        // launching. This is the Intent to deliver to the service.
        Intent service = new Intent(context, MyIntentService.class);
        startWakefulService(context, service);
    }
}
MyWakefulReceiver.completeWakefulIntent(intent);



参考:

http://hukai.me/android-training-course-in-chinese/background-jobs/scheduling/wake-lock.html











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值