3

I am building an app for the android, i dont need my app to have a calendar be part of it(plus it would defeat the purpose), my app allows the user to listen to a radio station, and needs the option to set an event to(remember to listen to the radio at a specific time), if the app has its own calendar then the event alarms will only go off if the user has the app open... pointless. i have been looking and cannot find, is there a way to use an intent or something else to open a google calendar or some other calendar that Android might have? i need to put my intent(/ other code) in my listener which i already have and looks like this for now

private View.OnClickListener reminder = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
                  // open calendar code goes here.

            }
};

i dont need the app to pre-fill in any field in the calendar just open it, i will leave the rest up to the user. all help is welcome and thank you

kyle
  • 53
  • 4
  • 11

1 Answers1

9

If you just want to open the calendar you can use and intent with EITHER of these component names (you might have to cater for both if you want to support older phones)

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);

If you want to go to the add event screen (which sounds better for your purpose) use something like:

 //all version of android
 Intent i = new Intent();

 // mimeType will popup the chooser any  for any implementing application (e.g. the built in calendar or applications such as "Business calendar"
 i.setType("vnd.android.cursor.item/event"); 

 // the time the event should start in millis. This example uses now as the start time and ends in 1 hour
 i.putExtra("beginTime", new Date().getTime()); 
 i.putExtra("endTime", new Date().getTime() + DateUtils.HOUR_IN_MILLIS);

 // the action
 i.setAction(Intent.ACTION_EDIT);
 startActivity(i);

(code is untested, copied from an existing project)

roflharrison
  • 2,300
  • 23
  • 26
  • how can i at run this ComponentName? or what do i do with this new ComponentName that i made? – kyle Feb 17 '11 at 22:02
  • sorry, I hit tab and it entered my comment before I added the juicy stuff – roflharrison Feb 17 '11 at 22:04
  • Neither of these are particularly safe. The second one is closer, as there is a greater chance that a device will happen to support that MIME type. However, there is no Calendar application in the Android OS. There may not be a Calendar application on some devices, and if there is one, it may not be `com.android.calendar`. Also, the core Android team is welcome to refactor their app and break this code, as evidenced by the two possible `Intents` already here. – CommonsWare Feb 17 '11 at 22:13
  • the above code causes my app to crash, just says has stopped unexpectedly, pls try again... i am testing this on a virtual for now.. do u think that i need to test it on a real one to know for sure? what would u suggest, what u posted describes exactly what i want to do, i just cant quite get it – kyle Feb 17 '11 at 22:17
  • AFAIK the emulator doesn't have a calendar application so it would not be able to find the application (per CommonsWare's comment) and would crash. @CommonsWare yes, yes and yes however I am not aware of a better way to open the calendar application otherwise I would use it :) – roflharrison Feb 17 '11 at 22:24
  • @Sam: Then don't open the calendar application. Find some other solution to your problem. – CommonsWare Feb 17 '11 at 22:28
  • thanks for your help i am going to just use the clock alarm to set a reminder, going for easy at this point, i learned a lot thanks again. – kyle Feb 17 '11 at 22:30