Android 警报服务崩溃

Android 警报服务崩溃,android,service,alarmmanager,alarm,android-pendingintent,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

我正在尝试创建一个警报,其中包含一个我已经存储在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);
@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@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的祝酒词也从未出现过

另一个问题,我是否必须从按钮启动服务?有没有一种不用按钮就能做到这一点的方法?我认为我的应用程序会更好地工作,如果我可以做到这一点没有按钮,因为时间信息是在线解析

我也希望我的闹钟每天都响。我目前有小时和分钟的信息。我需要一些信息让它每天都响吗

先谢谢你

更新

我添加了一个唤醒锁,它将唤醒我的手机进入我的服务,如下所示:

//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);
@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.

这意味着什么?

您看到的问题是,用于启动
活动的
意图
是从
服务
发送的,因此其
上下文
没有任务/后台堆栈的概念。logcat显示您需要将标志
标志活动新任务添加到
服务的
意图中,以便启动
活动

尽管如此,您可能仍然会遇到问题,因为警报正用于启动
服务。如果设备处于休眠状态且报警过期,系统将唤醒足够长的时间,以便
AlarmManager
在内部处理报警。如果报警的
pendingent
是针对
BroadcastReceiver
的,则它还将使设备保持足够长的唤醒时间,以便调用已注册的接收器。如果
挂起内容
用于
服务
活动
,则不会执行相同的操作。本文将提供一些附加细节(和示例代码):

这是您的问题

android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
当您想通过非活动上下文启动活动时,必须将
标志\u活动\u新任务
标志添加到启动者意图中,这样您的
启动命令
代码如下:

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

你把警报许可证放进舱单了吗?没有。什么许可证?我只有一个闹钟。不过我认为现在还不需要。我一开始就可以用这项服务来测试一下,只需要烤面包,不需要闹钟。但在我放置alarmmanager、日历、PendingEvent等之后,它就不起作用了,必须收到一个待定的意图。你在哪里收到的?嗯。。。我相信我没有这种东西。我所展示的就是我所拥有的。我怎么做接收器?那么我如何连接到接收器?非常感谢。它工作得非常好。现在的问题是,根据我的代码,我做得对吗?我觉得我所做的比人们根据我的研究所做的要简单得多。哦,这个闹钟并没有唤醒我锁定的屏幕。我该怎么做?我想这个问题会帮助你唤醒屏幕。请看我提供的最新更新。谢谢你的欢迎!当你遇到一个例外时,只要用谷歌搜索一下,你就会找到好的解决办法。我发现了这个: