0

Very Good Morning to All ..Am created Android Apps to prepare Schedule task..for my task I wants to create Notification Remainder for My Task..I dont have any coding to create that...I google this topic But I didint Get Any proper solution..Please Help Me anyone...Thanks in advance..

Here My coding..for insert datetime in edittext box:

MainActivity.java:

 EditText edtdate;
  private ImageButton ib;
    private Calendar cal;
    private int day ,month ,year;

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_inbox);
edtdate = (EditText) findViewById(R.id.edtdate);

cal = Calendar.getInstance();
        day = cal.get(Calendar.DAY_OF_MONTH);
        month = cal.get(Calendar.MONTH);
        year = cal.get(Calendar.YEAR);
     cal.add(Calendar.HOUR, heure);                         
        Intent intent1 = new Intent(this, MyBroadcastReceiver.class);                       
        PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent1,   
      PendingIntent.FLAG_UPDATE_CURRENT);                       
        AlarmManager am = (AlarmManager) this .getSystemService(Context.ALARM_SERVICE);                         
        am.setRepeating(AlarmManager.RTC_WAKEUP, 
                cal.getTimeInMillis(), 
                heure*3600*1000,  sender);

      custom = new CustomDateTimePicker(this, new     
      CustomDateTimePicker.ICustomDateTimeListener() {
          @Override
          public void onSet(Dialog dialog, Calendar calendarSelected,
                  Date dateSelected, int year, String monthFullName,
                  String monthShortName, int monthNumber, int date,
                  String weekDayFullName, String weekDayShortName,
                  int hour24, int hour12, int min, int sec,
                  String AM_PM)
          {
              ((EditText) findViewById(R.id.edtdate))
                      .setText(calendarSelected
                                      .get(Calendar.DAY_OF_MONTH)
                              + "/" + (monthNumber+1) + "/" + year
                              + ", " + hour12 + ":" + min
                              + " " + AM_PM);
          }
          @Override
          public void onCancel() {
          }
      });
          }
      });
 custom.set24HourFormat(false);

        custom.setDate(Calendar.getInstance());

        findViewById(R.id.imageButton1).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        custom.showDialog();
                    }
                });
}
  CustomDateTimePicker custom;
Android
  • 35
  • 2
  • 7

2 Answers2

1

@odiiil You can use alarm manager for scheduling notification on specific datetime

Ashok Kateshiya
  • 659
  • 4
  • 10
0

for creating a notification use a broadcastReceiver :

public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

    Intent resultIntent = new Intent(context,
            MainActivity.class);

    resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

    // Because clicking the notification opens a new ("special") activity,
    // there's no need to create an artificial back stack.
    PendingIntent resultPendingIntent = PendingIntent.getActivity(context,
            0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Notification.Builder mBuilder = new Notification.Builder(context)
    .setSmallIcon(R.drawable.courier_blanc)
    .setContentTitle("Title")
    .setContentText("Message");

    mBuilder.setAutoCancel(true);   
    mBuilder.setPriority(Notification.PRIORITY_MAX);
    mBuilder.setContentIntent(resultPendingIntent);

    // Sets an ID for the notification
    int mNotificationId = 001;
    // Gets an instance of the NotificationManager service
    NotificationManager mNotifyMgr = (NotificationManager) context
            .getSystemService(Application.NOTIFICATION_SERVICE);


    // Builds the notification and issues it.
    mNotifyMgr.notify(mNotificationId, mBuilder.build());

} }

Say if it helps. If you have some questions or if it's not really what you want, say it :)

EDIT :

        int heure = 1;
        int minute = 30;
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.HOUR, heure); 
        cal.add(Calendar.MINUTE, minute);

        Intent intent = new Intent(this, MyBroadcastReceiver.class);

        PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager am = (AlarmManager) this
                .getSystemService(Context.ALARM_SERVICE);

        am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), minute*60*1000+heure*3600*1000,  sender);
odiiil
  • 276
  • 1
  • 2
  • 9
  • Thanks for your reply..Notifiction class is ok...In My Apps Am using edittext to add datetime for my task ..once i clicked the add button..wants to send notification remainder every day to user mail id untill the task should be finished..hope you understand my qs.. – Android Jul 24 '14 at 06:44
  • so use an Alarm Manager to do his : `Calendar cal = Calendar.getInstance();cal.add(Calendar.HOUR, heure); Intent intent = new Intent(this, MyBroadcastReceiver.class); PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager am = (AlarmManager) this .getSystemService(Context.ALARM_SERVICE); am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), heure*3600*1000, sender);` – odiiil Jul 24 '14 at 06:53
  • sorry I haven't put the code right but copy paste it and say if it's what you want (not sure to understand...) – odiiil Jul 24 '14 at 06:55
  • Hmm Thanks for instant reply ..Ok having alarm manager for my task after inserted datetime..but after we inserted datetime it will send notification to user mail id daily...? if means help me – Android Jul 24 '14 at 07:00
  • 1
    you have to stop your notification : `Intent notificationIntent = new Intent(this, ActivityStopReciever.class); notificationIntent.setFlags(Notification.FLAG_AUTO_CANCEL); PendingIntent intent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);` and in your activity to stop receiver you have to call cancel [link]http://developer.android.com/reference/android/app/NotificationManager.html#cancel(int[link] and look here for stoping the alarm :http://stackoverflow.com/questions/4963271/android-alarmmanager-problem-with-setting-resetting-an-alarm/5055517#5055517 – odiiil Jul 24 '14 at 07:07
  • Thanks for your Reply, Actually i need to send notification alarm to their mail id ..daily...i dont want to cancel.. – Android Jul 24 '14 at 07:32
  • If I understand you have to put your Alarm after the user inserted datetime and it will work – odiiil Jul 24 '14 at 07:35
  • Sorry No response For that notification coding..If i debug Its doesn't shows anything..See My edited coding..Whats That Int "heure" – Android Jul 24 '14 at 10:47
  • "heure" is the time in hour of how often you want the alarm to be repeated. – odiiil Jul 24 '14 at 11:46
  • if you want , instead of : `heure*3600*1000` put `10*1000` ,so you will have a notification every 10 seconds. Maybe, you will see it – odiiil Jul 24 '14 at 11:48
  • Is coding is right ..? i didn't get any response when we add datetime – Android Jul 24 '14 at 11:53
  • do you enter in your class MyBroadcastReceiver ? (put Logcat) You have to know if the problem is the alarm or the notification – odiiil Jul 24 '14 at 11:56
  • my above edited coding is right? i didnt get any response ..asusual task is added..without created notificatioon and alarm.. – Android Jul 24 '14 at 12:01
  • I think The Alarm is Not WOrking – Android Jul 24 '14 at 12:06
  • You have to delete this : `cal.add(Calendar.HOUR, heure); ` because you chose to get real time. But I think your code is good. It's working for me.. – odiiil Jul 24 '14 at 12:10
  • will you please post that coding.? becoz for me its doesnt shows any response .. when will the alarm notifict created .. after insert date oly knw .but no response ..no need to change MyBroadcastReceiver class know.. – Android Jul 24 '14 at 12:38
  • 1
    i've put all my code here... (i edit my answer) have you put this in your manifest ? ` ` – odiiil Jul 24 '14 at 12:45
  • Hai .. Sory to Again disturb You...For You How Its working ..I mean There is alert notification is raised? on Which time you set..?.. For Me There is no response for that time .. how to check that..? – Android Jul 25 '14 at 09:08