MainActivity.java
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;
/**
* To display the given data as notification at particular date and time
*
* @author Harshal Kalavadiya
*/
public class TaskReminderActivity extends Activity {
/** Called when the activity is first created. */
private int mYear;
private int mMonth;
private int mDay;
private int mHour;
private int mMinute;
static final int DATE_DIALOG_ID = 1;
static final int TIME_DIALOG_ID = 0;
private Calendar c;
private EditText nameEdit;
private EditText descEdit;
private Context mContext;
private boolean dateFlag = false;
private boolean timeFlag = false;
private String time;
private String contentTitle;
private String contentText;
public static int notificationCount;
public Button dateButton;
public Button timeButton;
public Button reminButton;
public TextListener textListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mContext = this;
textListener = new TextListener();
dateButton= (Button)findViewById(R.id.dateButton);
timeButton= (Button)findViewById(R.id.timeButton);
reminButton= (Button)findViewById(R.id.reminderButton);
dateButton.setEnabled(false);
timeButton.setEnabled(false);
reminButton.setEnabled(false);
nameEdit = (EditText)findViewById(R.id.nameEditText);
nameEdit.addTextChangedListener(textListener);
descEdit = (EditText)findViewById(R.id.DescEditText);
descEdit.addTextChangedListener(textListener);
c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
}
public class TextListener implements TextWatcher{
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
if(descEdit.getText().length()==0 | nameEdit.getText().length()==0){
dateButton.setEnabled(false);
timeButton.setEnabled(false);
reminButton.setEnabled(false);
}
else if(descEdit.getText().length()>0 & nameEdit.getText().length()>0){
dateButton.setEnabled(true);
timeButton.setEnabled(true);
reminButton.setEnabled(true);
}
}
}
public void onReminderClicked(View view){
if(dateFlag & timeFlag == true){
notificationCount = notificationCount+1;
dateFlag = false;
timeFlag = false;
time = mYear+"-"+mMonth+"-"+mDay+" "+mHour+"-"+mMinute;
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh-mm");
Date dt = null;
try {
dt = df.parse(time);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long when = dt.getTime();
contentTitle = nameEdit.getText().toString();
contentText = descEdit.getText().toString();
AlarmManager mgr = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent(mContext, ReminderAlarm.class);
notificationIntent.putExtra("Name",contentTitle );
notificationIntent.putExtra("Description",contentText );
notificationIntent.putExtra("NotifyCount",notificationCount );
PendingIntent pi = PendingIntent.getBroadcast(mContext, notificationCount, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mgr.set(AlarmManager.RTC_WAKEUP,when, pi);
Toast.makeText(mContext, "Your Reminder Activated", Toast.LENGTH_LONG).show();
contentTitle = "";
contentText = "";
descEdit.setText("");
nameEdit.setText("");
}
else if(dateFlag == false | timeFlag == false){
Toast.makeText(mContext, "Please choose Date & Time", Toast.LENGTH_SHORT).show();
}
}
public void onTimeClicked(View view){
showDialog(TIME_DIALOG_ID);
}
public void onDateClicked(View view){
showDialog(DATE_DIALOG_ID);
}
@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
switch (id) {
case TIME_DIALOG_ID:
return new TimePickerDialog(this,
mTimeSetListener, mHour, mMinute, false);
case DATE_DIALOG_ID:
return new DatePickerDialog(this,
mDateSetListener,
mYear, mMonth, mDay);
}
return super.onCreateDialog(id);
}
private DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
mYear = year;
mMonth = monthOfYear+1;
mDay = dayOfMonth;
dateFlag = true;
}
};
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mHour = hourOfDay;
mMinute = minute;
timeFlag = true;
}
};
}
ReminderAlarm.java
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.widget.Toast;
/**
* Trigger Status bar Notification alert at particular date and time
*
* @author Kumaresan Palanisamy
*/
public class ReminderAlarm extends BroadcastReceiver{
private NotificationManager mNotificationManager;
private Notification notification;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = intent.getStringExtra("Name");
CharSequence message = intent.getStringExtra("Description");
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
notification = new Notification(R.drawable.ic_launcher,"Notification", System.currentTimeMillis());
notification.setLatestEventInfo(context, from, message, contentIntent);
mNotificationManager.notify(Integer.parseInt(intent.getExtras().get("NotifyCount").toString()), notification);
Toast.makeText(context, "New Notification Received", Toast.LENGTH_LONG).show();
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/nameLayout">
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="@string/Name"/>
<EditText
android:id="@+id/nameEditText"
android:hint="Enter Name "
android:layout_height="45dp"
android:layout_width="200dp" />
</LinearLayout>
<LinearLayout
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/descLayout"
android:layout_below="@id/nameLayout">
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="@string/Desc"/>
<EditText
android:id="@+id/DescEditText"
android:hint="Enter Description"
android:layout_height="45dp"
android:layout_width="200dp" />
</LinearLayout>
<LinearLayout
android:layout_marginTop="20dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@id/descLayout"
android:gravity="center_horizontal"
>
<Button
android:id="@+id/dateButton"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:text="@string/Date"
android:onClick="@string/DateClick"
/>
<Button
android:id="@+id/timeButton"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:text="@string/Time"
android:onClick="@string/Timeclick"/>
<Button
android:id="@+id/reminderButton"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:text="@string/Reminder"
android:onClick="@string/ReminderClick"/>
</LinearLayout>
</RelativeLayout>
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;
/**
* To display the given data as notification at particular date and time
*
* @author Harshal Kalavadiya
*/
public class TaskReminderActivity extends Activity {
/** Called when the activity is first created. */
private int mYear;
private int mMonth;
private int mDay;
private int mHour;
private int mMinute;
static final int DATE_DIALOG_ID = 1;
static final int TIME_DIALOG_ID = 0;
private Calendar c;
private EditText nameEdit;
private EditText descEdit;
private Context mContext;
private boolean dateFlag = false;
private boolean timeFlag = false;
private String time;
private String contentTitle;
private String contentText;
public static int notificationCount;
public Button dateButton;
public Button timeButton;
public Button reminButton;
public TextListener textListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mContext = this;
textListener = new TextListener();
dateButton= (Button)findViewById(R.id.dateButton);
timeButton= (Button)findViewById(R.id.timeButton);
reminButton= (Button)findViewById(R.id.reminderButton);
dateButton.setEnabled(false);
timeButton.setEnabled(false);
reminButton.setEnabled(false);
nameEdit = (EditText)findViewById(R.id.nameEditText);
nameEdit.addTextChangedListener(textListener);
descEdit = (EditText)findViewById(R.id.DescEditText);
descEdit.addTextChangedListener(textListener);
c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
}
public class TextListener implements TextWatcher{
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
if(descEdit.getText().length()==0 | nameEdit.getText().length()==0){
dateButton.setEnabled(false);
timeButton.setEnabled(false);
reminButton.setEnabled(false);
}
else if(descEdit.getText().length()>0 & nameEdit.getText().length()>0){
dateButton.setEnabled(true);
timeButton.setEnabled(true);
reminButton.setEnabled(true);
}
}
}
public void onReminderClicked(View view){
if(dateFlag & timeFlag == true){
notificationCount = notificationCount+1;
dateFlag = false;
timeFlag = false;
time = mYear+"-"+mMonth+"-"+mDay+" "+mHour+"-"+mMinute;
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh-mm");
Date dt = null;
try {
dt = df.parse(time);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long when = dt.getTime();
contentTitle = nameEdit.getText().toString();
contentText = descEdit.getText().toString();
AlarmManager mgr = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent(mContext, ReminderAlarm.class);
notificationIntent.putExtra("Name",contentTitle );
notificationIntent.putExtra("Description",contentText );
notificationIntent.putExtra("NotifyCount",notificationCount );
PendingIntent pi = PendingIntent.getBroadcast(mContext, notificationCount, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mgr.set(AlarmManager.RTC_WAKEUP,when, pi);
Toast.makeText(mContext, "Your Reminder Activated", Toast.LENGTH_LONG).show();
contentTitle = "";
contentText = "";
descEdit.setText("");
nameEdit.setText("");
}
else if(dateFlag == false | timeFlag == false){
Toast.makeText(mContext, "Please choose Date & Time", Toast.LENGTH_SHORT).show();
}
}
public void onTimeClicked(View view){
showDialog(TIME_DIALOG_ID);
}
public void onDateClicked(View view){
showDialog(DATE_DIALOG_ID);
}
@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
switch (id) {
case TIME_DIALOG_ID:
return new TimePickerDialog(this,
mTimeSetListener, mHour, mMinute, false);
case DATE_DIALOG_ID:
return new DatePickerDialog(this,
mDateSetListener,
mYear, mMonth, mDay);
}
return super.onCreateDialog(id);
}
private DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
mYear = year;
mMonth = monthOfYear+1;
mDay = dayOfMonth;
dateFlag = true;
}
};
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mHour = hourOfDay;
mMinute = minute;
timeFlag = true;
}
};
}
ReminderAlarm.java
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.widget.Toast;
/**
* Trigger Status bar Notification alert at particular date and time
*
* @author Kumaresan Palanisamy
*/
public class ReminderAlarm extends BroadcastReceiver{
private NotificationManager mNotificationManager;
private Notification notification;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = intent.getStringExtra("Name");
CharSequence message = intent.getStringExtra("Description");
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
notification = new Notification(R.drawable.ic_launcher,"Notification", System.currentTimeMillis());
notification.setLatestEventInfo(context, from, message, contentIntent);
mNotificationManager.notify(Integer.parseInt(intent.getExtras().get("NotifyCount").toString()), notification);
Toast.makeText(context, "New Notification Received", Toast.LENGTH_LONG).show();
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/nameLayout">
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="@string/Name"/>
<EditText
android:id="@+id/nameEditText"
android:hint="Enter Name "
android:layout_height="45dp"
android:layout_width="200dp" />
</LinearLayout>
<LinearLayout
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/descLayout"
android:layout_below="@id/nameLayout">
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="@string/Desc"/>
<EditText
android:id="@+id/DescEditText"
android:hint="Enter Description"
android:layout_height="45dp"
android:layout_width="200dp" />
</LinearLayout>
<LinearLayout
android:layout_marginTop="20dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@id/descLayout"
android:gravity="center_horizontal"
>
<Button
android:id="@+id/dateButton"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:text="@string/Date"
android:onClick="@string/DateClick"
/>
<Button
android:id="@+id/timeButton"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:text="@string/Time"
android:onClick="@string/Timeclick"/>
<Button
android:id="@+id/reminderButton"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:text="@string/Reminder"
android:onClick="@string/ReminderClick"/>
</LinearLayout>
</RelativeLayout>
No comments:
Post a Comment