Android:如何在android中设置日历提醒

时间:2011-06-03 04:58:34

标签: android

是否可以在android中显示日历提醒,以便在指定日期弹出警报并提醒用户该任务。

3 个答案:

答案 0 :(得分:1)

很抱歉,SDK中目前没有日历API。但是,您可以使用AlarmManager在您安排自己的UI时实现自己的警报。

答案 1 :(得分:0)

首先要在日历应用程序中设置警报,你必须使用permit-ion:

现在,警报接收器已设置,让我们看看将设置和取消警报的类:

package SomeApp.SomeApp;

import java.util.Calendar;

import java.lang.String;

import android.app.AlarmManager;
import android.app.ListActivity;
import android.app.PendingIntent;
import android.os.Bundle;
import android.util.Log;

import android.content.Intent;
import android.widget.Toast;

/**
 * When this code is run only one alert will be displayed even though 2 alerts were
 * were setup (as one of them will be cancelled later on
 */
public class SomeApp extends ListActivity {
    /* for logging - see my tutorial on debuggin Android apps for more detail */
    private static final String TAG = "SomeApp "; 

    protected Toast mToast; 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.alert_list);
        try {

        Calendar cal = Calendar.getInstance();

        Intent intent        = new Intent(SomeApp.this, AReceiver.class);
        PendingIntent sender = PendingIntent.getBroadcast(this, 1234567, intent, 0);
        PendingIntent sende2 = PendingIntent.getBroadcast(this, 123123, intent, 0);

        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis()+30000, sender); // to be alerted 30 seconds from now
        am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis()+15000, sende2); // to be alerted 15 seconds from now

        /* To show how alarms are cancelled we will create a new        Intent and a new PendingIntent with the
        * same requestCode as the PendingIntent alarm we want to cancel. In this case, it is 1234567.
            * Note: The intent and PendingIntent have to be the same as the ones used to create the alarms.
            */
        Intent intent1        = new Intent(SomeApp.this, AReceiver.class);
        PendingIntent sender1 = PendingIntent.getBroadcast(this, 1234567, intent1, 0);
        AlarmManager am1 = (AlarmManager) getSystemService(ALARM_SERVICE);
        am1.cancel(sender1);

        } catch (Exception e) {
            Log.e(TAG, "ERROR IN CODE:"+e.toString());
        }
    }

}

您会注意到这只是一次“一次性”警报。如果要设置重复警报,请在Android文档中进行说明。但是,如果有需求,我也会写一下。现在,让我们检查代码。要设置闹钟,您需要4件事:

正在设置闹钟的班级 警报“熄灭”时将调用的类 闹钟响起的时间 PendingIntent中使用的requestCode(将用作标识警报的唯一ID)。 要取消闹钟,您需要做三件事:

设置闹钟的类 警报“熄灭”时要调用的类 您用于PendingIntent对象的requestCode。 我们已经介绍了两件事 - 清单文件中的接收器声明以及设置和取消警报的类。现在,我们需要查看警报响起时将要调用的类。

package someApp.someApp;

import java.util.Calendar;

import android.content.Context;
import android.content.BroadcastReceiver;
import android.util.Log;
import android.widget.Toast;

/** All receiver classes must extend BroadcastReceiver */
public class AReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context con, Intent in) {
        try {
            /* Display an alert */
            Toast.makeText(con, "hello my jello ", Toast.LENGTH_LONG).show();
        } catch (Exception r) {
            Toast.makeText(con, "You were supposed to do something"
                    +" now but I can't retrieve what it was.",
                    Toast.LENGTH_SHORT).show();
            Log.e("ALARM_RECEIVER", r.toString());
        }
    }
}

答案 2 :(得分:0)

你也可以使用这个其他的...

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); 

Calendar cal = Calendar.getInstance();

cal.add(Calendar.DAY_OF_YEAR, 0);
cal.set(Calendar.HOUR_OF_DAY, 9);
cal.set(Calendar.MINUTE, 01);
cal.set(Calendar.SECOND, 0);
setAlarm(cal);

cal.set(Calendar.HOUR_OF_DAY, 12);
cal.set(Calendar.MINUTE, 30);
setAlarm(cal);

//etc

}

public void setAlarm(Calendar cal) {

try {   

Intent intent        = new Intent(Alarm.this, Alarm1.class);
PendingIntent sender = PendingIntent.getBroadcast(this, 1234567, intent, 0);
PendingIntent sende2 = PendingIntent.getBroadcast(this, 123123, intent, 0);

AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender); // to be alerted 30 seconds from now
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sende2); // to be alerted 15 seconds from now

/* To show how alarms are cancelled we will create a new        Intent and a new PendingIntent with the
* same requestCode as the PendingIntent alarm we want to cancel. In this case, it is 1234567.
    * Note: The intent and PendingIntent have to be the same as the ones used to create the alarms.
    */
Intent intent1        = new Intent(Alarm.this, Alarm1.class);
PendingIntent sender1 = PendingIntent.getBroadcast(this, 1234567, intent1, 0);
AlarmManager am1 = (AlarmManager) getSystemService(ALARM_SERVICE);
am1.cancel(sender1);


} catch (Exception e) {
    Log.e(TAG, "ERROR IN CODE:"+e.toString());
}
}