在用户指定的时间启动应用程序?

发布于 2024-10-20 15:00:35 字数 1489 浏览 4 评论 0原文

我有一个可以播放广播电台的应用程序,现在我想集成一个闹钟,以便在闹钟响起时播放广播电台。我一直在研究警报管理器,这似乎是最好的方法。

我的应用程序有一个闹钟按钮,它调用一个对话框来设置闹钟。如果设置了闹钟,我需要让我的应用程序在指定时间启动。但是,我在处理这部分代码时遇到了问题:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

写下不归期 2024-10-27 15:00:35

Kyle,

它应该是包上下文,但这有时取决于您在代码中创建 IntentPendingIntent 的位置。您刚刚尝试过 thisgetApplicationContext() 吗?

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()?

烟雨扶苏 2024-10-27 15:00:35

事实证明,我还需要创建一个新类...上下文的一部分是通过创建一个变量来工作的,如果其他人需要它,这里是我必须创建的新服务的代码。

//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
    }

}

然后不要忘记将其添加到您的清单中

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文