I have an app that plays radio stations, and I now want to integrate an alarm clock such that it plays a radio station when the alarm goes off. I have been looking into the Alarm Manager, which seems to be the best way to do it.
My app has an alarm button which calls a dialog to set the alarm. If an alarm is set, I need to have my app start at the specified time. However, I am having trouble with this section of code:
Intent intent = new Intent("some Context", null, null, MainActivity.class);
PendingIntent pendInt = PendingIntent.getBroadcast("some Context", 0, intent, 0);
AlarmManager alarmService = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmService.set(AlarmManager.RTC_WAKEUP, timeToSound, pendInt);
Specifically, I am confused about what context
needs to be. I have seen many examples, but none really explain it in detail. I would appreciate it if someone could shed some light on this matter.
@Override
protected Dialog onCreateDialog(int id) {
Dialog d = null;
switch (id) {
case LINEUP_DIALOG_ID:
d = new LineupDialog(this);
d.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
WindowManager.LayoutParams lp = d.getWindow().getAttributes();
lp.dimAmount = 0.5f;
d.getWindow().setAttributes(lp);
break;
this calls my dialog^
private View.OnClickListener lineupAction = new View.OnClickListener() {
@Override
public void onClick(View v) {
//showAlarm();
showDialog(LINEUP_DIALOG_ID);
}
};
both of these are in my mainActivity.
then i have an xml file that holds the layouts (can be provided if needed.. just allows the user to chose a time and checkbo开发者_如何转开发x, then save) save button has an onclickListener--- it is in my LineupDialog class that extends my NavDialog, and my navdialog just extends Dialog.Kyle,
It should be the package Context, but this can sometimes depend on where in your code you are creating the Intent
and PendingIntent
. Have you just tried this
or getApplicationContext()
?
turns out that i needed to make a new class also... the part of the context worked by making a variable, and just if anyone else needs it, here is my code for the new service that i had to make.
//imports here
public class AlarmService extends Service{
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
//System.out.println("hello im in the alarmService Binder");
return null;
}
@Override
public void onStart(Intent intent, int startid) {
Intent mainAct = new Intent("android.intent.action.MAIN");
//System.out.println("hello im in the alarmService onStart " + mainAct);
mainAct.setClass(AlarmService.this, MainActivity.class);
mainAct.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mainAct.putExtra("alarm","true");
startActivity(mainAct);
}
@Override
public void onCreate() {
//System.out.println("hello im in the alarmService onCreate");
//code to execute when the service is first created
}
}
then dont forget to add it to your manifest
精彩评论