报警服务崩溃

时间:2015-07-15 20:24:43

标签: android service alarmmanager alarm android-pendingintent

我正在尝试创建一个警报,其中包含我已经存储在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.

这是什么意思?

2 个答案:

答案 0 :(得分:1)

您遇到的问题是因为hasProperty发送的Intent是从Activity发送的,因此其Service没有任务概念/返回堆。 logcat向您显示,您需要将标记Context添加到FLAG_ACTIVITY_NEW_TASK的{​​{1}},以便Intent启动。

所有这一切,你可能仍然会遇到问题,因为警报被用来启动Service。如果设备处于睡眠状态并且警报到期,系统将唤醒足够长的时间以使Activity在内部处理警报。如果警报Service用于AlarmManager,则它还会使设备保持足够长时间唤醒,以便注册的接收器被调用。如果PendingIntent适用于BroadcastReceiverPendingIntent,则不会执行相同的操作。本文将提供一些其他详细信息(和示例代码):http://po.st/7UpipA

答案 1 :(得分: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);