Android Alarm Manager to set a Remainder with Notificationand Sound
Screen Shots:
Coding:
AlarmActivity.java
package com.example.alarm_manager;
import android.app.Activity;
import android.os.Bundle;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
public class AlarmActivity extends Activity {
/** Called when the activity is first created. */
/* Author J M */
TimePicker TimePicker;
DatePicker DatePicker;
Button Setalarm;
TimePickerDialog timePickerDialog;
final static int RQS_1 = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DatePicker =(DatePicker)findViewById(R.id.datePicker1);
TimePicker=(TimePicker)findViewById(R.id.timePicker1);
Calendar now = Calendar.getInstance();
DatePicker.init(
now.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH),
null);
TimePicker.setCurrentHour(now.get(Calendar.HOUR_OF_DAY));
TimePicker.setCurrentMinute(now.get(Calendar.MINUTE));
Setalarm = (Button) findViewById(R.id.Setalarm);
Setalarm.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Calendar current = Calendar.getInstance();
Calendar cal = Calendar.getInstance();
cal.set(DatePicker.getYear(),
DatePicker.getMonth(),
DatePicker.getDayOfMonth(),
TimePicker.getCurrentHour(),
TimePicker.getCurrentMinute(),
00);
if(cal.compareTo(current) <= 0){
//The set Date/Time already passed
Toast.makeText(getApplicationContext(),
"Invalid Date/Time",
Toast.LENGTH_LONG).show();
}else{
setAlarm(cal);
}
}});
}
private void setAlarm(Calendar targetCal) {
//
Toast.makeText(AlarmActivity.this, "Alarm is set at " + targetCal.getTime(),
Toast.LENGTH_LONG).show();
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getBaseContext(), RQS_1, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(),pendingIntent);
}
}
AlarmReceiver.java
package com.example.alarm_manager;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Vibrator;
import android.sax.StartElementListener;
import android.telephony.SmsManager;
import android.widget.Toast;
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Your Time is up!!!!!", Toast.LENGTH_LONG).show();
Vibrator vib=(Vibrator)context.getSystemService(context.VIBRATOR_SERVICE); //for Vibration
vib.vibrate(2000);
Intent i=new Intent(context,song.class); //song class contain media song
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
song.java
package com.example.alarm_manager;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.media.MediaPlayer;
import android.os.Bundle;
public class song extends Activity{
MediaPlayer m;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.song);
m=MediaPlayer.create(getApplicationContext(), R.raw.gaji);
//create a folder raw inside res folder. and put 1 .mp3 song
m.start();
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(song.this);
dlgAlert.setTitle("Remainder !");
dlgAlert.setMessage("Your time is up !");
dlgAlert.setPositiveButton("OK",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
m.stop();
dialog.cancel();
}
});
dlgAlert.show();
}
}
Layout:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="20dp" >
<TextView
android:id="@+id/alarmprompt"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TimePicker
android:id="@+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<DatePicker
android:id="@+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/Setalarm"
android:layout_width="117dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Set Alarm" />
</LinearLayout>
song.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.alarm_manager"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.VIBRATE"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".AlarmActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="AlarmReceiver"></receiver>
<activity android:name="song"></activity>
</application>
</manifest>
Thanks man :)
ReplyDeleteThank you, this tutorial worked perfectly
ReplyDeleteThank you. You can follow my youtube channel too http://www.youtube.com/8bitzs and do subscribe.
Deletesuperior papers about the particular technology you can say that how to use or utilize technology. First get technology then get understanding to use it.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteIs it worked?
Deletehow to set multiple remainders using sqlite?
ReplyDeleteGreat effort. Thank you
ReplyDelete