Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Michael Mealy
Michael Mealy
13,247 Points

Need help with an Android Project

Can anyone help me, I am building android App that when a button is pressed it opens a calendar app already installed on the phone using intents. here is my Current Code so far.(Inside the OnClick Method).

 @Override
 public void onClick(View v) {

Intent calendarIntent = new Intent(Intent.ACTION_MAIN, null);
calendarIntent.addCategory(Intent.CATEGORY_APP_CALENDAR);

PackageManager packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(calendarIntent, 0);
boolean isIntentSafe = activities.size() > 0;

if(isIntentSafe) {
startActivity(calendarIntent);

}else{ Toast.makeText(this, "No Suitable Application is     installed",Toast.LENGTH_LONG).show();
    }

This Code Works fine, if you have got a min API of 15, but mine is 10. is there anyway to access a calendar App using an API lower than 15.

2 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Intent.CATEGORY_APP_CALENDAR is only available as of API 15. So you need to create your intent differenty for < API 15.

Kind of like this untested code:

Intent calendarIntent;
int apiVersion = android.os.Build.VERSION.SDK_INT;
if (apiVersion >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
    calendarIntent = new Intent(Intent.ACTION_MAIN, null);
    calendarIntent.addCategory(Intent.CATEGORY_APP_CALENDAR);
} 
else {
    calendarIntent = new Intent(Intent.ACTION_EDIT);  
    calendarIntent.setType("vnd.android.cursor.item/event");
}

http://stackoverflow.com/questions/4373074/how-to-launch-android-calendar-application-using-intent-froyo