开发者

Android : how to set the calendar alert in android

开发者 https://www.devze.com 2023-03-10 07:01 出处:网络
Is it possible to show calendar alert in android, so that on the specified date the ale开发者_C百科rt should pop up and remind the user regarding the task.Sorry, there isn\'t currently a calendar API

Is it possible to show calendar alert in android, so that on the specified date the ale开发者_C百科rt should pop up and remind the user regarding the task.


Sorry, there isn't currently a calendar API in the SDK. You can however implement your own alarm with the AlarmManager showing your own UI at the time you schedule with it.


first of all to set the alert in calendar application you have to make the permit-ion :

Now, that the alarm receiver is set, lets take a look at the class that will set and cancel the alarms:

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());
        }
    }

}

You will notice that this is only a "one-shot" alarm. If you want to set a repeating alarm, it is explained in Android's documentation. However, I will write on that too if there is demand for it. Now, let's examine the code. For setting an alarm, you will need 4 things:

The class that's setting the alarm The class that will be called when the alarm "goes off" The time at which the alarm should go off A requestCode (which will use as a unique ID to identify the alarms) used in PendingIntent. For cancelling an alarm, you need 3 things:

The class that set the alarm The class that was to be called when the alarm "goes off" The requestCode you used for PendingIntent object. We have covered 2 things - the declaration of the receiver in our manifest file and the class that sets and cancels alarms. Now, we need to look at the class that will be called when the alarm goes off.

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());
        }
    }
}


AND YOU are able to use this other ans also...

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());
}
}
0

精彩评论

暂无评论...
验证码 换一张
取 消