Skip to content Skip to sidebar Skip to footer

How Can I Call My Alarm At The Time Of Event Occurs In Android?

Explanation: I had created a calendar in my application.Which provides the functionality like add and delete event on the particular day of the month. This event is save

Solution 1:

You need to use the AlarmManager Class to notify the user through Push Notifications:

So, to get this working, you need to pass the time in milliseconds which represents your target date e.g : 24 Dec 2016 -> Convert to milliseconds and pass it to the AlaramManager

private Calendar calendar;
    calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
    calendar.set(Calendar.MINUTE, minute);
    calendar.set(Calendar.MONTH, month);
    calendar.set(Calendar.YEAR, year);
    calendar.set(Calendar.DAY_OF_MONTH, day);
    delay = calendar.getTimeInMillis();

publicclassSetUpNotification {

publicvoidsetAlarm(long delay, String taskName, String taskDetails, Context context)
{
    Intent alertIntent = new Intent(context, AlertReceiver.class);
    alertIntent.putExtra("TaskName", taskName);
    alertIntent.putExtra("TaskDetails", taskDetails);




    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    final int _id = (int) System.currentTimeMillis();

    Log.d("Delay", delay+"");
    Log.d("ID", _id+"");
    alarmManager.set(AlarmManager.RTC_WAKEUP, delay,
            PendingIntent.getBroadcast(context, _id, alertIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT));
}

}

publicclassAlertReceiverextendsBroadcastReceiver {
staticintx=0;

@OverridepublicvoidonReceive(Context context, Intent intent) {

    StringtaskName= intent.getStringExtra("TaskName");
    StringtaskDetails= intent.getStringExtra("TaskDetails");

    Log.d("BroadcastReceiver", "OnRecieve");
    createNotification(context, taskName , taskDetails, "EatThatFrog");
    x++;
    Log.d("X", x+"");
}

publicvoidcreateNotification(Context context, String msg, String msgText, String msgAlert){

    PendingIntentnotificIntent= PendingIntent.getActivity(context, 0,
            newIntent(context, DashboardActivity.class), 0);

    NotificationCompat.BuildermBuilder=
            (NotificationCompat.Builder) newNotificationCompat.Builder(context)

                    .setSmallIcon(R.drawable.notification_icon)
                    .setContentTitle(msg)
                    .setTicker(msgAlert)
                    .setContentText(msgText);

    mBuilder.setContentIntent(notificIntent);


    mBuilder.setDefaults(Notification.DEFAULT_SOUND);

    mBuilder.setAutoCancel(true);

    NotificationManagermNotificationManager=
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    mNotificationManager.notify(1 + x, mBuilder.build());

}

}

Solution 2:

You can alarm manager class and put your broadcast receiver to handle that event and for showing event in dialog you can make dialog as an activity which shows your event detail from database

For putting alarm

publicvoidsetAlarm(View v){
        Intentintent=newIntent(this,AlarmReceiver.class);
        PendingIntentpendingIntent= PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManageralarmManager= (AlarmManager)getSystemService(ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+ Delay,pendingIntent);


    }

in broad cast receiver

publicvoidonReceive(Context ctxt, Intent intent){
                Intent i=newIntent(ctxt, EventActivity.class);

                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                ctxt.startActivity(i);
              }

Post a Comment for "How Can I Call My Alarm At The Time Of Event Occurs In Android?"