【问题标题】:Alarm service crashes报警服务崩溃
【发布时间】:2015-07-15 20:24:43
【问题描述】:

我正在尝试创建一个警报,其中包含我已经存储在 Parse 中的时间列表。

我有一个带有此代码的 AlarmService:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);
    Toast.makeText(this,"onStartCommand()",Toast.LENGTH_SHORT).show();

    Intent intent = new Intent(this, AlarmScreenActivity.class);
    startActivity(intent);

    return flags;
}

按下添加警报活动中的保存按钮后服务关闭,如下所示:

//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, AlarmService.class);
PendingIntent pendingIntent = PendingIntent.getService(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);

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

当时间到时,应用程序崩溃并且我的日志显示:

java.lang.RuntimeException: Unable to start service com.example......AlarmService@16859914 with Intent { flg=0x4 cmp=com.example.....AlarmService (has extras) }: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

我不太明白这个日志错误。有人可以解释并帮助我解决这个问题吗?

AlarmService 的 Toast 也从未出现过。

另一个问题,我必须从按钮启动我的服务吗?有没有办法在不使用按钮的情况下做到这一点?我认为如果我可以在没有按钮的情况下执行此操作,我的应用程序会更好地工作,因为时间信息在解析中是在线的。

我还希望我的闹钟每天响起。我目前有小时和分钟的信息。我需要一些信息来告诉它每天都熄灭吗?

提前谢谢你

更新

我添加了一个唤醒锁,这样它就会将我的手机唤醒到我的服务中,如下所示:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);
    Toast.makeText(this,"onStartCommand()",Toast.LENGTH_SHORT).show();

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

    //start reminder screen
    Intent intent = new Intent(this, AlarmScreenActivity.class);
    ReminderIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);

    return flags;
}

现在我有另一个日志,上面写着:

java.lang.RuntimeException: Unable to start service com.example....AlarmService@129539b6 with Intent { flg=0x4 cmp=com.....AlarmService (has extras) }: java.lang.IllegalArgumentException: Must specify a valid wake lock level.

这是什么意思?

【问题讨论】:

  • 你把报警权限放到manifest里面了吗?
  • 没有。什么是许可?我只有一个唤醒锁。不过我认为还不需要。
  • 一开始我可以使用该服务,只需要吐司,没有警报。但是我放了alarmmanager,calender,pendingintent等之后就不行了
  • 必须收到待处理的意图。你在哪里收到的?
  • 嗯...我相信我没有这样的东西。我所展示的就是我所拥有的。如何制作接收器?以及如何连接到接收器?

标签: android service alarmmanager alarm android-pendingintent


【解决方案1】:

这是你的问题

android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

当您想通过非活动上下文启动活动时,您必须将 FLAG_ACTIVITY_NEW_TASK 标志添加到启动意图,以便您的 onStartCommand 代码如下所示:

Intent intent = new Intent(this, AlarmScreenActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

【讨论】:

  • 非常感谢。它真的很好用。现在的问题是,根据我的代码,我这样做对吗?根据我的研究,感觉我所做的比人们所做的要简单得多。
  • 哦,这个闹钟不会唤醒我锁定的屏幕。我该怎么做?
  • 我觉得这个问题stackoverflow.com/questions/9966506/…会帮你唤醒屏幕
  • 请查看我提供的新更新。谢谢
  • 欢迎您!当您遇到异常时,只需 google 一下,您就会发现很好的 sloutions 。我找到了这个:stackoverflow.com/questions/15555630/…
【解决方案2】:

您看到的问题是因为发送Intent 以启动Activity 是从Service 发送的,所以它的Context 没有任务/返回堆栈的概念。 logcat 显示您需要将标志 FLAG_ACTIVITY_NEW_TASKService 添加到 Intent 以启动 Activity

话虽如此,您可能仍然会遇到问题,因为警报正在用于启动Service。如果设备处于睡眠状态并且警报到期,系统将唤醒足够长的时间以供AlarmManager 在内部处理警报。如果警报的PendingIntent 是针对BroadcastReceiver 的,那么它也会让设备保持足够长的唤醒时间,以便调用注册的接收器。如果PendingIntent 用于ServiceActivity,它不会做同样的事情。本文将提供一些额外的细节(和示例代码):http://po.st/7UpipA

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-22
    • 2013-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多