Reputation: 343
I'm trying to create an alarm with a list of time to go off that I already have stored in Parse.
I have a AlarmService with this code:
@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;
}
The Service goes off after the Save Button in add an alarm activity is pressed as follow:
//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);
When the time is met, the app crashes and my Log says:
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?
I don't quite understand this log error. Can someone explain and help me solve this issue?
The Toast from the AlarmService also never appeared.
another question, do I have to initiate my service from the button? is there a way to do this without using a button? I think my app will work better if I can do this without the button since the time information is online in parse.
I also want my alarm to go off everyday. I currently have information of hour and minute. Do I need some information to tell it to go off everyday?
Thank you in advance
UPDATE
I added a wake lock so it will wake my phone into my Service as follow:
@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;
}
Now I have another Log that says:
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.
What does this mean?
Upvotes: 1
Views: 236
Reputation: 917
this is your problem
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
when you want to start an activity by a non activity context you have to add FLAG_ACTIVITY_NEW_TASK
flag to the starter intent so your onStartCommand
code would be like this :
Intent intent = new Intent(this, AlarmScreenActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Upvotes: 1
Reputation: 15775
The problem you are seeing is because the Intent
being sent to start the Activity
is being sent from Service
, so its Context
has no notion of task/back stack. The logcat shows you that you need to add the flag FLAG_ACTIVITY_NEW_TASK
to the Intent
from the Service
in order to get the Activity
started.
All of that being said, you are likely still going to run into issues since the alarm is being used to start the Service
. If the device is asleep and the alarm expires, the system will wake long enough for the AlarmManager
to process the alarm internally. If the alarm's PendingIntent
is for a BroadcastReceiver
then it will also keep the device awake long enough so the registered receiver gets called. It does not do the same thing if the PendingIntent
is for a Service
or Activity
. This article will help with some additional details (and sample code): http://po.st/7UpipA
Upvotes: 1