Android Set Alarm With Date and Time | Scheduling API 23

Android Set Alarm With Specific Date and Time 2017

Set an alarm is easy in android with specific duration but with specific date and time required little bit more code. In last example we learned how to trigger an alarm how alarm is work in android. Here is the complete android set alarm example with specific date time picker. Do not forget to check you have proper package name in you java class.

android set alarm

Android AlarmManager MainActivity.java example 2017

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

DatePicker datePicker; //Set your date
TimePicker timePicker; //set required time
Button setAlarm;
final static int RQS_1 = 4;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

datePicker = (DatePicker)findViewById(R.id.pickerdate);
timePicker = (TimePicker)findViewById(R.id.pickertime);

Calendar now = Calendar.getInstance();
datePicker.init(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH), null);

timePicker.setHour(now.get(Calendar.HOUR_OF_DAY));
timePicker.setMinute(now.get(Calendar.MINUTE));

setAlarm = (Button)findViewById(R.id.setalarm);
setAlarm.setOnClickListener(new View.OnClickListener(){

@Override
public void onClick(View arg0) {
Calendar current = Calendar.getInstance();

Calendar calendar = Calendar.getInstance();
calendar.set(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth(), timePicker.getHour(),
timePicker.getMinute(), 00);

if(calendar.compareTo(current) <= 0){
//The set Date/Time already added show an toast.
Toast.makeText(getApplicationContext(), “Invalid Date/Time”, Toast.LENGTH_LONG).show();
}
else{
setAlarm(calendar);
}
}
});
}

private void setAlarm(Calendar targetCal){

Intent intent = new Intent(getBaseContext(), AlarmReceiver.class); //ALARM IS SET
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
}
}

Android AlarmManager BroadcastReceiver example

Create a new java class name it AlarmReceiver. When your set alarm will be occur this BroadcastReceiver will be triggered. In this broadcastreceiver you can set your own things like in this example we set a notification to be shown when alarm occur. From this notification you can open a activity.

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.support.v7.app.NotificationCompat;
import android.widget.Toast;

public class AlarmReceiver extends BroadcastReceiver {

private static final int MY_NOTIFICATION_ID = 4;
NotificationManager nfm;
Notification ntf;

@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, “Alarm received!”, Toast.LENGTH_LONG).show();

Intent myIntent = new Intent(context, DoSomething.class); //SO THIS ACTIVITY IN SETTED ALARM TIME.
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, myIntent, Intent.FLAG_ACTIVITY_NEW_TASK);

ntf = new NotificationCompat.Builder(context)
.setContentTitle(“YOUR ALARM WORKED”)
.setContentText(“some deatails”)
.setTicker(“Notification Head”)
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent)
.setDefaults(Notification.DEFAULT_SOUND) //PLAY DEFAULT SOUND
.setAutoCancel(true) // REMOVE ALARM NOTIFICATION JUST BY SWIPE
.setSmallIcon(R.mipmap.ic_launcher) //SHOWED IN STATUS BAR
.build();

nfm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
nfm.notify(MY_NOTIFICATION_ID, ntf);
}
}

Android AlarmManager activity_main.xml code

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:orientation=”vertical”
android:background=”#797879″
tools:context=”.MainActivity” >

<ScrollView
android:layout_width=”match_parent”
android:layout_height=”match_parent”>

<LinearLayout
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:orientation=”vertical”
>
<DatePicker
android:id=”@+id/pickerdate”
android:layout_width=”match_parent”
android:layout_height=”300dp”
/>
<TimePicker
android:id=”@+id/pickertime”
android:layout_width=”match_parent”
android:layout_height=”250dp”/>

<Button
android:id=”@+id/setalarm”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:background=”#595959″
android:text=”Set Alarm”/>

</LinearLayout>
</ScrollView>
</LinearLayout>

Add another activity or open MainActivity from Notification

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class DoSomething extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(getApplicationContext(), “Do Something NOW”, Toast.LENGTH_LONG).show();
}
}

Android AlarmManager AndroidManifest.xml example code

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”attraction.mydate”>

<application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:supportsRtl=”true”
android:theme=”@style/AppTheme”>
<activity android:name=”.MainActivity”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
<activity
android:name=”.DoSomething”
android:label=”@string/app_name” >
</activity>
<receiver android:name=”.AlarmReceiver” android:process=”:remote” />
</application>
</manifest>

Here is complete android set alarm example tutorial. Visit here for official android Alarm code. This example have no deprecation in android API 23. You may face deprecation problem in older version like setCurentHour, setCurrentMinute, getCurrentHour, getCurrentMinute.

Visit NextGenEarn Blog for more such tutorial.

Add a Comment

Your email address will not be published. Required fields are marked *