Payday Loan Payday Loan

Hi, today i want to put all this stuff in one place, because i never found toghether and correct, this informations.
Mainly, the purpose of this tutorial is to create and set a reminder/alarm on a certain date. This reminder will appear like a notification with sound and vibration, even if we closed the app that setted this reminder/alarm.

In the Main.java class we will set the alarm. I will just show you how to set it, but you need to put this code in a button or somewhere else where you need it.

	Calendar cal = Calendar.getInstance();       //for using this you need to import java.util.Calendar;
 
				// add minutes to the calendar object
				cal.set(Calendar.MONTH, 4);
				cal.set(Calendar.YEAR, 2011);				
				cal.set(Calendar.DAY_OF_MONTH, 5);
				cal.set(Calendar.HOUR_OF_DAY, 21);
				cal.set(Calendar.MINUTE, 43);
//cal.set will set the alarm to trigger exactly at: 21:43, 5 May 2011
//if you want to trigger the alarm after let's say 5 minutes after is activated you need to put
//cal.add(Calendar.MINUTE, 5);
	Intent alarmintent = new Intent(getApplicationContext(), AlarmReceiver.class);
alarmintent.putExtra("title","Title of our Notification");
alarmintent.putExtra("note","Description of our  Notification");
//HELLO_ID is a static variable that must be initialised at the BEGINNING OF CLASS with 1;
 
//example:protected static int HELLO_ID =1;
PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(), HELLO_ID,
alarmintent,PendingIntent.FLAG_UPDATE_CURRENT|  Intent.FILL_IN_DATA);
//VERY IMPORTANT TO SET FLAG_UPDATE_CURRENT... this will send correct extra's informations to 
//AlarmReceiver Class
				// Get the AlarmManager service
 
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);

Now I will show you AlarmReceiver.java
This class must be declared in Manifest as a receiver because it will extend BroadcastReceiver.
So we put in Manifest before tag the following line:

 <receiver android:name=".AlarmReceiver"></receiver>

Code for AlarmReceiver.java. Please follow comments instructions also.

 
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Toast;
 
public class AlarmReceiver extends BroadcastReceiver {
 
	private static int NOTIFICATION_ID = 1;
 
	@Override
	public void onReceive(Context context, Intent intent) {
 
	        // NotificationManager mNotificationManager = 
(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
NotificationManager manger = (NotificationManager)     
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon, "Combi Note",
 System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(context, 
NOTIFICATION_ID, 
new Intent(context, AlarmReceiver.class), 0);
		Bundle extras=intent.getExtras();
		String title=extras.getString("title");
//here we get the title and description of our Notification
			//
		String note=extras.getString("note");
		notification.setLatestEventInfo(context, note, title, contentIntent);
		notification.flags = Notification.FLAG_INSISTENT;
		notification.defaults |= Notification.DEFAULT_SOUND;
//here we set the default sound for our 
//notification
 
		// The PendingIntent to launch our activity if the user selects this notification
		manger.notify(NOTIFICATION_ID++, notification);
 
 
 
	}
 
};

I hope i make it clear and simple how you can set a notification that can be a reminder or an alarm that will be activated even if we closed the app or processes.

||||| 4 I Like It! |||||

11 Responses so far.

  1. Dan says:

    Hey,
    I followed your steps but there is no notification generated. There is no error either.
    I copied the same code you gave with the alarm being set via a button.
    I added the receiver in the manifest also. Is there any other permission that needs to be given in the manifest.
    Thnx.
    Dan

    • Dan says:

      sorry… my bad…
      for month i had entered 8… it should have been 7 coz it’s august. Works fine now.

      thanx.
      Dan

  2. nishant says:

    Hi,
    I copied your code into my application but it is not working. Please check my code below and suggest me any solution.

    protected static int HELLO_ID =1;
    public void save(View view){
    EditText Ename=(EditText)findViewById(R.id.EventName);
    EditText date=(EditText)findViewById(R.id.date);
    EditText time=(EditText)findViewById(R.id.time);
    final ContentValues values;
    final DBhelper myDbHelper = new DBhelper(this);
    try {

    myDbHelper.createDataBase();
    myDbHelper.openDataBase();
    }
    catch (IOException ioe) {
    throw new Error(“Unable to create database”);
    }
    catch(SQLException sqle){
    throw sqle;
    }
    String e=Ename.getText().toString();
    String d=date.getText().toString();
    String t=time.getText().toString();

    int day=Integer.parseInt(d.substring(0,d.indexOf(“-”)));
    int month =Integer.parseInt(d.substring(d.indexOf(“-”)+1,d.lastIndexOf(“-”)));
    int year =Integer.parseInt(d.substring(d.lastIndexOf(“-”)+1));
    int hour=Integer.parseInt(t.substring(0,t.indexOf(“:”)));
    int min=Integer.parseInt(t.substring(t.indexOf(“:”)+1));
    values = new ContentValues();
    values.put(“EventName”, e);
    values.put(“Date”, d);
    values.put(“Time”, t);
    try{
    myDbHelper.insertRecord(values);
    Calendar cal = Calendar.getInstance(); //for using this you need to import java.util.Calendar;
    // add minutes to the calendar object
    cal.set(Calendar.MONTH, month);
    cal.set(Calendar.YEAR, year);
    cal.set(Calendar.DAY_OF_MONTH, day);
    cal.set(Calendar.HOUR_OF_DAY, hour);
    cal.set(Calendar.MINUTE, min);
    //cal.set will set the alarm to trigger exactly at: 21:43, 5 May 2011
    //if you want to trigger the alarm after let’s say 5 minutes after is activated you need to put
    //cal.add(Calendar.MINUTE, 5);
    Intent alarmintent = new Intent(getApplicationContext(), MyBroadcastReceiver.class);
    alarmintent.putExtra(“title”,e);
    alarmintent.putExtra(“note”,”Description of our Notification”);
    //HELLO_ID is a static variable that must be initialised at the BEGINNING OF CLASS with 1;
    //example:protected static int HELLO_ID =1;
    PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(), HELLO_ID,
    alarmintent,PendingIntent.FLAG_UPDATE_CURRENT| Intent.FILL_IN_DATA);
    //VERY IMPORTANT TO SET FLAG_UPDATE_CURRENT… this will send correct extra’s informations to
    //AlarmReceiver Class
    // Get the AlarmManager service
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
    Toast.makeText(this, “Reminder set “,
    Toast.LENGTH_LONG).show();

    Ename.setText(“”);
    date.setText(“”);
    time.setText(“”);
    }
    catch(Exception e1){
    e1.printStackTrace();
    }
    finally{
    myDbHelper.close();
    }
    finish();
    //setContentView(R.layout.main1);
    }

    and MyBroadcastreciever class is :
    public void onReceive(Context context, Intent intent) {
    //NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationManager manger = (NotificationManager)
    context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.icon, “Combi Note”,
    System.currentTimeMillis());
    PendingIntent contentIntent = PendingIntent.getActivity(context,NOTIFICATION_ID,new Intent(context, MyBroadcastReceiver.class), 0);
    Bundle extras=intent.getExtras();
    String title=extras.getString(“title”);
    //here we get the title and description of our Notification
    String note=extras.getString(“note”);
    notification.setLatestEventInfo(context, note, title, contentIntent);
    notification.flags = Notification.FLAG_INSISTENT;
    notification.defaults |= Notification.DEFAULT_SOUND;
    //here we set the default sound for our
    //notification
    // The PendingIntent to launch our activity if the user selects this notification
    manger.notify(NOTIFICATION_ID++, notification);
    }

    • nishant says:

      I think problem is in cal.getTimeInMillis()
      am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
      please help me if I am not correct.

      • fb_avatar admin says:

        you dont specified what is the error. Tell me what’s not working, it gives you some errors…or simply just not trigger the alarm?…please check the month setted also…0 is for January and so on. Also check LogCat.

        • Jürgen says:

          I also tried ths tutorial but also didn’t get an notification. I also took in consideration, that Januarry is 0. I have no errors at all but no notification…

          • Jürgen says:

            I also tried ths tutorial but also didn’t get an notification. I also took in consideration, that Januarry is 0. I have no errors at all but no notification… I now mentioned that the notification worked but it was set for 17:38 and i know mentioned it at 19:48

          • Jürgen says:

            Muss nun korrigieren. Funktioniert nun bei mir, jedoch konnte ich nicht herausfinden woran es lag funkt nun einfach.

            Doch was müsste ich tun, wenn ich diesen Alarm auch noch nach einem NEustart erhalten wolen würde. Wieso wird für die Notification ein Broadcastreceiver verwendet und wieso ist die Nachricht nach einem Neustart weg?

            lg

  3. Akki says:

    hi,
    thanks a lot .. i tried this code many times but forgot considering month range .. now its working fine :)

  4. Ranjit says:

    Its working friend,please provide the month entry correctly because its start from 0 so we have to put the month accordingly( like December=11)


Search

Popular

Sponsors