android–PowerManager wakelock没有从服务中唤醒设备

android–PowerManager wakelock没有从服务中唤醒设备,第1张

概述我有一个应用程序,每分钟都有一个后台服务.如果它处于睡眠状态,我希望服务唤醒设备.我使用的是PowerManager,但设备没有唤醒.有什么想法吗?提前致谢.@OverrideprotectedvoidonHandleIntent(Intentintent){PowerManagerpm=(PowerManager)getSystemService(Context.PO

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

@OverrIDeprotected 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 timeCalendar cal = Calendar.getInstance();// add 5 minutes to the calendar objectcal.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 192837PendingIntent sender = PendingIntent.getbroadcast(        getApplicationContext(), 192837, intent,        PendingIntent.FLAG_UPDATE_CURRENT);// Get the AlarmManager serviceAlarmManager 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 minsam.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]

@OverrIDepublic 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();}

解决方法:

要么使用Commonsware的WakefulIntentService,要么这样做:

class YourService extends IntentService {    private static final String LOCK_name = YourService.class.getname()            + ".Lock";    private static volatile WakeLock lockStatic = null; // notice static    synchronized private static PowerManager.WakeLock getLock(Context context) {        if (lockStatic == null) {            PowerManager mgr = (PowerManager) context                    .getSystemService(Context.POWER_SERVICE);            lockStatic = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,                    LOCK_name);            lockStatic.setReferenceCounted(true);        }        return (lockStatic);    }    public static voID startYourService(Context ctxt, Intent i) { // STATIC         getLock(ctxt.getApplicationContext()).acquire();        ctxt.startService(i);    }    public YourService(String name) {        super(name);        setIntentRedelivery(true);    }    @OverrIDe    public int onStartCommand(Intent intent, int flags, int startID) {        PowerManager.WakeLock lock = getLock(this.getApplicationContext());        if (!lock.isHeld() || (flags & START_FLAG_REDEliVERY) != 0) {            lock.acquire();        }        super.onStartCommand(intent, flags, startID);        return (START_REDEliVER_INTENT);    }    @OverrIDe    protected voID onHandleIntent(Intent intent) {        try {            // do your thing        } finally {            PowerManager.WakeLock lock = getLock(this.getApplicationContext());            if (lock.isHeld()) lock.release();        }    }}

在你的接收器中:

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");YourService.startYourService(context, myIntent)

这实际上是@CommonsWare WakefulintentService的核心(不确定START_FLAG_REDEliVERY,我应该问其中一天)

总结

以上是内存溢出为你收集整理的android – PowerManager wakelock没有从服务中唤醒设备全部内容,希望文章能够帮你解决android – PowerManager wakelock没有从服务中唤醒设备所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1101765.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存