【发布时间】: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