在用户指定的时间启动应用程序?
我有一个可以播放广播电台的应用程序,现在我想集成一个闹钟,以便在闹钟响起时播放广播电台。我一直在研究警报管理器,这似乎是最好的方法。
我的应用程序有一个闹钟按钮,它调用一个对话框来设置闹钟。如果设置了闹钟,我需要让我的应用程序在指定时间启动。但是,我在处理这部分代码时遇到了问题:
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);
具体来说,我对 context
需要什么感到困惑。我看过很多例子,但没有一个真正详细地解释它。如果有人能对此事有所了解,我将不胜感激。
更多可能有帮助的代码...
@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;
这调用了我的对话框^
private View.OnClickListener lineupAction = new View.OnClickListener() {
@Override
public void onClick(View v) {
//showAlarm();
showDialog(LINEUP_DIALOG_ID);
}
};
这两个都在我的 mainActivity 中。
然后我有一个保存布局的 xml 文件(如果需要可以提供..只允许用户选择时间和复选框,然后保存)
保存按钮有一个 onclickListener——它位于我的 LineupDialog 类中,它扩展了我的 NavDialog,而我的 navdialog 只是扩展了 Dialog。
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.
more code that might help...
@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 checkbox, then save)
save button has an onclickListener--- it is in my LineupDialog class that extends my NavDialog, and my navdialog just extends Dialog.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Kyle,
它应该是包上下文,但这有时取决于您在代码中创建
Intent
和PendingIntent
的位置。您刚刚尝试过this
或getApplicationContext()
吗?Kyle,
It should be the package Context, but this can sometimes depend on where in your code you are creating the
Intent
andPendingIntent
. Have you just triedthis
orgetApplicationContext()
?事实证明,我还需要创建一个新类...上下文的一部分是通过创建一个变量来工作的,如果其他人需要它,这里是我必须创建的新服务的代码。
然后不要忘记将其添加到您的清单中
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.
then dont forget to add it to your manifest