2012-12-21 9 views
5

Chcę otworzyć kalendarz z mojej aplikacji i przekazać do tego kalendarza parametr "Data".Jak otworzyć aplikację kalendarza na Androida i przejść do określonej daty?

Ten kalendarz wyświetli stronę odpowiadającą dacie daty.

Przeszukałem kod źródłowy kalendarza, ale nie znaleziono sposobów jego użycia.

public static void openCalendarApp(Context context) 
{ 
    Intent intent = context.getPackageManager().getLaunchIntentForPackage("com.android.calendar"); 
    context.startActivity(intent); 
} 

Odpowiedz

2

można użyć calendar view to zrobić ... użyć metody setDate(long date) aby ustawić datę w kalendarzu z datą chcesz

można też zrobić to przez dodawanie zdarzeń do kalendarza jak tak

zamiarem utworzyć kalendarz

Intent calIntent = new Intent(Intent.ACTION_INSERT); 
calIntent.setData(CalendarContract.Events.CONTENT_URI); 
startActivity(calIntent) 

Siew kalendarza dat i godzin

Intent calIntent = new Intent(Intent.ACTION_INSERT); 
calIntent.setType("vnd.android.cursor.item/event"); 
calIntent.putExtra(Events.TITLE, "Title here"); 
calIntent.putExtra(Events.EVENT_LOCATION, "Location here"); 
calIntent.putExtra(Events.DESCRIPTION, "Description here"); 
GregorianCalendar calDate = new GregorianCalendar(2012, 7, 15); 
calIntent.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, true); 
calIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, 
    calDate.getTimeInMillis()); 
calIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, 
    calDate.getTimeInMillis()); 
startActivity(calIntent); 

Przykładem tego może być postrzegane here

+0

Bardziej szczegółową pomoc tutaj:-http: //www.vogella. com/articles/AndroidCalendar/article.html –

+0

Wielkie dzięki! :) – user1921193

+0

z przyjemnością pomogę :) –

0

Oto jak obsługiwać zarówno stare i nowe wersje Androida:

@SuppressWarnings("deprecation") 
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) 
public static Intent prepareIntentForCalendar(final Context context, final Date date) { 
    Intent intent = null; 
    if (VERSION.SDK_INT >= VERSION_CODES.ICE_CREAM_SANDWICH) { 
     // go to date of the calendar app, as shown here: 
     // http://developer.android.com/guide/topics/providers/calendar-provider.html#intent-view 
     final Uri.Builder builder = CalendarContract.CONTENT_URI.buildUpon(); 
     builder.appendPath("time"); 
     ContentUris.appendId(builder, date.getTime()); 
     intent = new Intent(Intent.ACTION_VIEW).setData(builder.build()); 
     final PackageManager pm = context.getPackageManager(); 
     final ResolveInfo resolveActivity = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY 
       | PackageManager.GET_RESOLVED_FILTER); 
     if (resolveActivity == null) 
      return null; 
    } else { 
     intent = new Intent(Intent.ACTION_EDIT); 
     intent.setClassName("com.android.calendar", "com.android.calendar.AgendaActivity"); 
     intent.putExtra("beginTime", date.getTime()); 
     final PackageManager pm = context.getPackageManager(); 
     final ResolveInfo resolveActivity = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY 
       | PackageManager.GET_RESOLVED_FILTER); 
     if (resolveActivity == null) 
      intent = null; 
    } 
    if (intent != null) 
     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY 
       | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET | Intent.FLAG_ACTIVITY_MULTIPLE_TASK); 
    return intent; 
}