Reputation: 89
Hello people please somebody help me,I want to set the event to the device calendar through my application.I need to fetch date from the edittext(which is user input) and on clickiing a button(say set date)it should be saved to the device calendar as a event.And on the said date it should remind the app user about the event.
I have tried a lot but I am not able to do this.Being a college student I tried my level best but really need somebody's assistance ..
Please people if anybody knows it let me know the solution(if example is provided then it would be a great help(pls dont just tell me about the api,use calendarprovider and that this,i need with proper example))..
Thanks in advance
I have opened the device calendar through following code.This allows me to set event manually but I want to set event automatically after clicking a button. Can anybody help me in this regard?? Intent i = new Intent();
//Froyo or greater (mind you I just tested this on CM7 and the less than froyo one worked so it depends on the phone...) cn = new ComponentName("com.google.android.calendar", "com.android.calendar.LaunchActivity");
//less than Froyo cn = new ComponentName("com.android.calendar", "com.android.calendar.LaunchActivity");
i.setComponent(cn); startActivity(i);
The above code displays the native calendar and allows user to set the event themselves. But I dont want this to happen,instead I want the native calendar to take user input & sets the event.So for this purpose I have implemented the following code: public void addCalendarEvent(){
Calendar cal = Calendar.getInstance();
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("beginTime", cal.getTimeInMillis());
intent.putExtra("allDay", true);
intent.putExtra("rrule", "FREQ=YEARLY");
intent.putExtra("UNTIL=20140404T080000Z",true);
intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
intent.putExtra("title", "A Test Event from android app");
intent.putExtra("description", "A Test Description from android app");
intent.putExtra("eventLocation", "Geolocation");
startActivity(intent);
}
I invoked this method within the button onClick() method. I hard coded the date as : intent.putExtra("UNTIL=20140404T080000Z",true); But this will not work.However, I don't want to hard code it,it should accept user input(dynamically it should work,say fetching the edittext data and passed through intent). Please can anybody help me in this regard??
Upvotes: 1
Views: 1468
Reputation: 452
Before using this answer single-handedly,you must do some searching on google. Thatz the only way you can learn something new in perfect way.
1) Alarm Manager
2) Broadcast Receiver
3) Intent and context of application
Alright here we go;
First you need to get Calendar object to set date and time into Android Alarm Manager. This method in MainActivity.java, in your button click event call this method while passing relevant parameters.
public void addReminder(int mYear, int mMonth, int mDay, int mHour, int mMinute){
Calendar c = Calendar.getInstance();
//set Reminder time and date into calendar object
c.set(Calendar.YEAR,mYear);
c.set(Calendar.MONTH, mMonth);//Don't use exact numeric value of the month, use one minus.Ex: April=>as 3
c.set(Calendar.DATE, mDay);
c.set(Calendar.HOUR_OF_DAY, mHour);
c.set(Calendar.MINUTE, mMinute);
c.set(Calendar.SECOND, 0);
//Unique Alarm ID creation
int alrmId=0;
alrmId=Integer.parseInt(mMonth+""+mDay+""+mHour+""+mMinute);
//Alarm task creation
Intent in=new Intent(this,ReminderReceiver.class);
in.putExtra("text", "You have a Reminder!");
in.putExtra("AlrmId", alrmId);
PendingIntent pi;
pi = PendingIntent.getBroadcast( this, alrmId, in,0 );
AlarmManager am;
am = (AlarmManager)(this.getSystemService( Context.ALARM_SERVICE ));
am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),pi);
}
Next, You need to have receiver to catch this Firing event and show notification, give a sound, vibrate a device. To do this i'll add ReminderReceiver class to project,
public class ReminderReceiver extends BroadcastReceiver {
// Vibrator object
public Vibrator vibrator;
long[] pattern = { 0L, 250L, 200L, 250L, 200L, 250L, 200L, 250L, 200L,
250L, 200L, 250L, 200L, 250L, 200L };
// Ringtone object
Uri ringT;
Ringtone ringTone;
@Override
public void onReceive(Context context, Intent intent) {
String remindText = intent.getStringExtra("text");
//Unique notification id for every alarm
int receiverID = intent.getIntExtra("AlrmId", 0);
// Notification creation
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
context).setSmallIcon(android.R.drawable.ic_popup_reminder)
.setContentTitle("Reminder").setContentText(remindText);
// Create vibrator pattern
vibrator = (Vibrator) context
.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(pattern, -1);// No repetition
// Notification tone creation and play
ringT = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
ringTone = RingtoneManager.getRingtone(context, ringT);
ringTone.play();
// Create toast and show on center of the screen
Toast toast = Toast.makeText(context, remindText, Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
// end of toast...
// Show status bar notification
NotificationManager mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(receiverID, mBuilder.build());
}
}
All done !!! Just one thing, set permission in Android Manifest.xml
<uses-permission android:name="android.permission.VIBRATE" />
And Register ReminderReceiver class, add this lines with in <application> </application>
tag (not inside of any other tags !!!),
<receiver android:name=".ReminderReceiver">
</receiver>
Hope this will help you.
Cheers !!!
I have googled a lot but i did not find the solution or any hints to achieve it.
And this code is also not working for me. It will not create any event in the native calendar. I checked in emulator, bluestack and also in my mobile (apk file is not getting created).
Please help me with any other solution or to improve this one
This Link may full fill your requirement.
Upvotes: 1