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。

4

2 回答 2

0

凯尔,

它应该是包 Context,但这有时可能取决于您在代码中创建Intentand的位置PendingIntent。你刚试过吗thisgetApplicationContext()

于 2011-03-04T18:23:01.473 回答
0

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

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

}

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

于 2011-03-08T16:39:08.273 回答