Search code examples
androidcalendarsamsung-mobile

Not able to View Calendar


In my application, I want to open device default calendar using code/programatically. I am using this code :

private void viewAllCalender() {
        // TODO Auto-generated method stub
        Intent i = new Intent();
        if(Build.VERSION.SDK_INT >= 8 && Build.VERSION.SDK_INT <= 14){
            i.setClassName("com.android.calendar","com.android.calendar.LaunchActivity");
        }else if(Build.VERSION.SDK_INT >= 15){    
            i.setClassName("com.google.android.calendar", "com.android.calendar.LaunchActivity");
        }else{
            i.setClassName("com.android.calendar","com.android.calendar.LaunchActivity");
        }
        startActivity(i);
    }

IT IS WORKING FOR ALL THE DEVICES BUT IT IS NOT WORKING IN SAMSUNG S3 - (BUILD SDK VERSION - 17)

Please help me to figure out what the problem is??

Thanks


Solution

  • You gotta realise that you can't expect an Android device to have a certain application. Not even the play app can be expected to be installed. The correct way of doing this is simply not using .setClassName and then let the user decide what to do.

    There is a dozen different calendar apps and the phone manufactures each have their own...

    Edit

    If you want to add an event to calendar you could use my CalendarOrganizer which handles a lot of these issues:

    public class CalendarOrganizer {
        private final static int ICE_CREAM_BUILD_ID = 14;
        /**
         * Creates a calendar intent going from startTime to endTime
         * @param startTime
         * @param endTime
         * @param context
         * @return true if the intent can be handled and was started, 
         * false if the intent can't be handled
         */
        public static boolean createEvent(long startTime, long endTime, String title, String description, 
                String location, boolean isAllDay, Context context) {
            Intent intent = new Intent(Intent.ACTION_EDIT);
            int sdk = android.os.Build.VERSION.SDK_INT;
            if(sdk < ICE_CREAM_BUILD_ID) {
                // all SDK below ice cream sandwich
                intent.setType("vnd.android.cursor.item/event");
                intent.putExtra("beginTime", startTime);
                intent.putExtra("endTime", endTime);
                intent.putExtra("title", title);
                intent.putExtra("description", description);
                intent.putExtra("eventLocation", location);
                intent.putExtra("allDay", isAllDay);
    
    //          intent.putExtra("rrule", "FREQ=YEARLY");
            } else {
                // ice cream sandwich and above
                intent.setType("vnd.android.cursor.item/event");
                intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, startTime);
                intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime);
                intent.putExtra(Events.TITLE, title);
                intent.putExtra(Events.ACCESS_LEVEL, Events.ACCESS_PRIVATE);
                intent.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY , isAllDay);
                intent.putExtra(Events.DESCRIPTION, description);
                intent.putExtra(Events.EVENT_LOCATION, location);
    
    //          intent.putExtra(Events.RRULE, "FREQ=DAILY;COUNT=10") 
            }
            try {
                context.startActivity(intent);
                return true;
            } catch(Exception e) {
                return false;
            }
        }
    }