你好朋友需要帮助!
我正在使用Android,在我的应用程序中,需要一次设置多个提醒.像这样的东西
for( int i = 0; i < n; i++)
{
// Code to set Reminder
}
Run Code Online (Sandbox Code Playgroud)
目前我有以下代码,但一次仅适用于一个提醒.
StringTokenizer st=new StringTokenizer(strDateForReminder, "-");
cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(st.nextToken()));
cal.set(Calendar.MONTH, Integer.parseInt(st.nextToken())-1);
cal.set(Calendar.YEAR, Integer.parseInt(st.nextToken()));
String strTime= textView.getText().toString().trim();
// Toast.makeText(getApplicationContext(), "strTime= "+strTime, Toast.LENGTH_LONG).show();
String[] strTimeArray = strTime.split(getResources().getString(R.string.delimiter));
String[] strFirstTime=strTimeArray[0].split(":");
cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(strFirstTime[0]));
cal.set(Calendar.MINUTE, Integer.parseInt(strFirstTime[1]));
cal.set(Calendar.SECOND, 00);
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("beginTime", cal.getTimeInMillis());
intent.putExtra("endTime", cal.getTimeInMillis()+90*60*1000);
intent.putExtra("title", "Reminder");
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
请帮忙.提前致谢!
如果我理解正确,您使用活动的方法只允许您一次添加一个事件,因为用户必须与设备交互以确认它.你想要的是CalendarContract4.0中引入的新功能.
如果您不希望用户必须与日历应用程序进行交互,则可能更喜欢基于ContentProvider的方法.在Froyo和Gingerbread and Honeycomb版本中,您必须"知道"用于您想要与之交互的各个字段的名称.我们没有涵盖这种方法,因为它是官方不支持的,但您可以在我们的撰稿人Jim Blacker的网站上找到一篇好文章,网址为http://jimblackler.net/blog/?p=151.
通过Ice Cream Sandwich(Android 4,API级别14),新的CalendarContract类在各种嵌套类中保存了制作便携式日历应用程序所需的所有常量.这显示将日历事件直接插入用户的第一个日历(使用id 1); 显然,应该有一个下拉列表,列出用户在真实应用程序中的日历.
public void addEvent(Context ctx, String title, Calendar start, Calendar end) {
Log.d(TAG, "AddUsingContentProvider.addEvent()");
TextView calendarList =
(TextView) ((Activity) ctx).findViewById(R.id.calendarList);
ContentResolver contentResolver = ctx.getContentResolver();
ContentValues calEvent = new ContentValues();
calEvent.put(CalendarContract.Events.CALENDAR_ID, 1); // XXX pick)
calEvent.put(CalendarContract.Events.TITLE, title);
calEvent.put(CalendarContract.Events.DTSTART, start.getTimeInMillis());
calEvent.put(CalendarContract.Events.DTEND, end.getTimeInMillis());
calEvent.put(CalendarContract.Events.EVENT_TIMEZONE, "Canada/Eastern");
Uri uri = contentResolver.insert(CalendarContract.Events.CONTENT_URI, calEvent);
// The returned Uri contains the content-retriever URI for
// the newly-inserted event, including its id
int id = Integer.parseInt(uri.getLastPathSegment());
Toast.makeText(ctx, "Created Calendar Event " + id,
Toast.LENGTH_SHORT).show();
}
Run Code Online (Sandbox Code Playgroud)