简体   繁体   English

当手机处于休眠状态时,BroadcastReceiver不会触发

[英]BroadcastReceiver not firing when phone is sleeping

I have a app that takes care of certain silence functions, but I can't make it work when the phone is sleeping and not connected to the computer. 我有一个负责某些静音功能的应用程序,但是当手机处于睡眠状态且未连接到计算机时,我无法使其正常运行。 Everything works fine when the screen is on, but it stops working when the phone sleeps. 萤幕开启时,一切正常,但手机进入休眠状态后,一切停止运作。 Any idea what I'm doing wrong? 知道我在做什么错吗? I have checked 100 other posts here, but I just can't identify my problem. 我在这里检查了100个其他帖子,但我无法确定我的问题。

My Alarm manager that makes the intent: 意图的我的警报管理器:

Intent intent2 = new Intent(this, Silence.class);
PendingIntent pendingIntent2 = PendingIntent.getBroadcast(this, 1000 + id, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am2 = (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
am2.setRepeating(AlarmManager.RTC_WAKEUP, cal2.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent2);

Broadcast Receiver: 广播接收器:

public class MainSilenceCatcherVibrate extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent1) {
    WakeLocker.acquire(context);
    System.out.println("test");
    WakeLocker.release();
    }
}

Manifest usage: 清单用法:

<uses-permission android:name="android.permission.WAKE_LOCK" /> 
<receiver android:name=".Silence"></receiver>
<activity android:name=".ContactActivity"></activity>

i just cant identify my problem 我只是无法确定我的问题

Your event will happen once immediately, then not again for a week ( AlarmManager.INTERVAL_DAY * 7, ). 您的事件将立即发生一次,然后持续一周( AlarmManager.INTERVAL_DAY * 7, )。 Perhaps you did not wait a week. 也许您没有等待一个星期。

Note that you do not need WakeLocker here -- so long as all your work is in onReceive() , the system will hold a WakeLock for you. 请注意,这里不需要WakeLocker只要所有工作都在onReceive() ,系统就会为您保留WakeLock If you have work that will take more than a millisecond or so, you will not want to do that in onReceive() , but in that case I would recommend WakefulBroadcastReceiver or my WakefulIntentService over rolling your own WakeLock solution. 如果您的工作耗时超过一毫秒左右,则不希望在onReceive() ,但在那种情况下,我建议您使用WakefulBroadcastReceiverWakefulIntentService自己的WakeLock解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM