在android中设置闹钟

时间:2014-04-16 16:25:21

标签: android alarmmanager alarm

我对Android世界的新手,我正在尝试在提醒应用程序中使用闹钟。当我设置时间和日期并按完成按钮时,会出现一个带有以下消息的对话框"遗憾的是,universalreminder已停止"

按下结束按钮时将执行的java代码

    finishBasic = (Button)findViewById(R.id.buttonforfinishinbirthdaybasic);
    finishBasic.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            cal_birthdaybasic.set(yeartopass,monthtopass,daytopass,hourtopass,minutetopass,00);
            Toast.makeText(getApplicationContext(), "asdsa", Toast.LENGTH_LONG).show();
            setAlarm(cal_birthdaybasic);
            Intent i = new Intent(Set_birthday_reminder.this,Newreminder.class);
            startActivity(i);

        }
    });

将带有时间和日期的日历变量cal_birthdaybasc传递给方法setAlarm(),

    public void setAlarm(Calendar targetCal){
    Toast.makeText(getApplicationContext(), "setAlarm is called", Toast.LENGTH_LONG).show();
    Intent intent = new Intent(getBaseContext(),AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
}

广播接收器如下

    public class AlarmReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context arg0, Intent arg1) {
    // TODO Auto-generated method stub
    MediaPlayer mp = MediaPlayer.create(arg0, R.raw.gunshot);
    mp.start();
    Toast.makeText(arg0, "Alarm Received", Toast.LENGTH_LONG).show();

}

}

当我运行此代码时,catlog中出现的错误是

    04-16 10:56:26.090: E/AndroidRuntime(1234): FATAL EXCEPTION: main
    04-16 10:56:26.090: E/AndroidRuntime(1234): Process: com.universalremainder, PID: 1234
    04-16 10:56:26.090: E/AndroidRuntime(1234): java.lang.NullPointerException
    04-16 10:56:26.090: E/AndroidRuntime(1234):     at com.universalremainder.Set_birthday_reminder$5.onClick(Set_birthday_reminder.java:108)
    04-16 10:56:26.090: E/AndroidRuntime(1234):     at android.view.View.performClick(View.java:4424)
    04-16 10:56:26.090: E/AndroidRuntime(1234):     at android.view.View$PerformClick.run(View.java:18383)
    04-16 10:56:26.090: E/AndroidRuntime(1234):     at android.os.Handler.handleCallback(Handler.java:733)
    04-16 10:56:26.090: E/AndroidRuntime(1234):     at android.os.Handler.dispatchMessage(Handler.java:95)
    04-16 10:56:26.090: E/AndroidRuntime(1234):     at android.os.Looper.loop(Looper.java:137)
    04-16 10:56:26.090: E/AndroidRuntime(1234):     at android.app.ActivityThread.main(ActivityThread.java:4998)
    04-16 10:56:26.090: E/AndroidRuntime(1234):     at java.lang.reflect.Method.invokeNative(Native Method)
    04-16 10:56:26.090: E/AndroidRuntime(1234):     at java.lang.reflect.Method.invoke(Method.java:515)
    04-16 10:56:26.090: E/AndroidRuntime(1234):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
    04-16 10:56:26.090: E/AndroidRuntime(1234):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
    04-16 10:56:26.090: E/AndroidRuntime(1234):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

初始化cal_birthdaybasic,如下所示

cal_birthdaybasic = Calendar.getInstance();

然后使用

cal_birthdaybasic.set(yeartopass,monthtopass,daytopass,hourtopass,minutetopass,00);