android 服务wake_lock,android – PowerManager wakelock没有从服务中唤...

我有一个应用程序,每分钟都有一个后台服务.如果它处于睡眠状态,我希望服务唤醒设备.我使用的是PowerManager,但设备没有唤醒.有什么想法吗?提前致谢.

@Override

protected void onHandleIntent(Intent intent) {

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);

PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK,

"My Tag");

wl.acquire();

// do work as device is awake

wl.release();

}

[EDIT1]

这就是我从Activity启动服务的方式.

// get a Calendar object with current time

Calendar cal = Calendar.getInstance();

// add 5 minutes to the calendar object

cal.add(Calendar.MINUTE, 1);

Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);

intent.putExtra("alarm_message", "sending outstanding transactions");

// In reality, you would want to have a static variable for the

// request code instead of 192837

PendingIntent sender = PendingIntent.getBroadcast(

getApplicationContext(), 192837, intent,

PendingIntent.FLAG_UPDATE_CURRENT);

// Get the AlarmManager service

AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);

// am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);

// 86400000 = 24 hours

// 43200000 = 12 hours

// 3600000 = 1hr

// 1800000 = 30 mins

// 300000 = 5 mins

am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),60000,sender);

AlarmReceiver类

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.widget.Toast;

import android.os.Bundle;

public class AlarmReceiver extends BroadcastReceiver {

@Override

public void onReceive(Context context, Intent intent) {

try {

Bundle bundle = intent.getExtras();

String message = bundle.getString("alarm_message");

// Toast.makeText(context, message, Toast.LENGTH_SHORT).show();

Intent myIntent = new Intent(context,

SendOutstandingTransactions.class);

myIntent.setAction("com.carefreegroup.startatboot.MyService");

context.startService(myIntent);

} catch (Exception e) {

Toast.makeText(

context,

"There was an error somewhere, but we still received an alarm",

Toast.LENGTH_SHORT).show();

e.printStackTrace();

}

}

}

以下类调用我想在设备处于睡眠状态时启动的Activity.

SendOutstandingTransactions IntentService.

protected void onHandleIntent(Intent intent) {

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);

PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK,

"My Tag");

wl.acquire();

if (hasMessageDisplayed == false) {

Intent i = new Intent(this, DisplayMessageActivity.class);

i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

startActivity(i);

}

wl.release();

}

[EDIT2]

@Override

public void onDestroy() {

super.onDestroy();

PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);

PowerManager.WakeLock wl = pm.newWakeLock(

PowerManager.PARTIAL_WAKE_LOCK, "com.something.alarm");

// Acquire the lock

if (wl.isHeld()) wl.release();

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值