【问题标题】:Wake Lock doesnt work唤醒锁不起作用
【发布时间】:2015-07-16 03:36:25
【问题描述】:

我正在尝试创建一个警报,其中包含我已经存储在 Parse 中的时间列表。使用我现在使用的代码,时间到了就什么都不会发生。我曾经使用过一项旨在进行另一项活动的服务,但唤醒锁不起作用。所以,我现在正在尝试使用 Activity。但是现在,时间一到,什么都不会发生。有人可以帮我吗?谢谢

我有一个带有此代码的警报活动:

@Override
public void onCreate(Bundle savedInstateState) {
    super.onCreate(savedInstateState);

    //wake lock
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    mWakeLock = pm.newWakeLock(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, "My Wake Log");
    mWakeLock.acquire();

    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
                    WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                    WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN |
                    WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                    WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    setContentView(R.layout.activity_reminder_screen);

}

按下添加闹钟活动中的保存按钮后,活动关闭,如下所示:

//set time into calendar instance
Calendar calendar= Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,mAlarmDetails.timeHour);
calendar.set(Calendar.MINUTE,mAlarmDetails.timeMinute);

AlarmManager reminder = (AlarmManager)getSystemService(Context.ALARM_SERVICE);

Intent intent = new Intent(this, Alarm.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);

reminder.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),pendingIntent);

提前谢谢你

【问题讨论】:

  • 在 Manifest 中设置唤醒锁的权限

标签: android alarm android-pendingintent android-alarms wakelock


【解决方案1】:

试试这个代码,

PendingIntent operation = PendingIntent.getActivity(getBaseContext(), 1, 
                                              intent, Intent.FLAG_ACTIVITY_NEW_TASK);

/** Getting a reference to the System Service ALARM_SERVICE */
AlarmManager alarmManager = (AlarmManager) getBaseContext()
                           .getSystemService(ALARM_SERVICE);
GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day, hour, minute);

/**
 * Converting the date and time in to milliseconds elapsed since
 * epoch
 */
long alarm_time = calendar.getTimeInMillis();
/** Setting an alarm, which invokes the operation at alart_time */
alarmManager.set(AlarmManager.RTC_WAKEUP, alarm_time, operation);

【讨论】:

  • 这个公历是什么?我没有年份和月份的信息。我每天都在做闹钟。
  • 我这样做了,但我得到了 log cat 错误“java.lang.IllegalArgumentException:必须指定有效的唤醒锁定级别。”
  • 我认为这个链接会帮助你wptrafficanalyzer.in/blog/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-14
相关资源
最近更新 更多