售卖小程序源代码的公司有很多,但是一般不建议购买小程序源代码。首先一点是别人写的代码,自己再看的时候非常吃力,因为每个人写代码的逻辑思维都不一样;另外一点是如果有BUG,排查出来很麻烦,浪费时间和精力。建议还是可以直接用小程序模版制作,只需要更改内容就行。
因为日历是系统自带的,所以读写它一定要申请权限,也就是在AndroidManifestxml加如下两行代码(一个读一个写):
<uses-permission android:name="androidpermissionREAD_CALENDAR"/>
<uses-permission android:name="androidpermissionWRITE_CALENDAR"/>
Android中日历用了三个URL,分别是日历用户的URL,事件的URL,事件提醒URL,三个URL在Android21之前是如下的样子:
calanderURL = "content://calendar/calendars";
calanderEventURL = "content://calendar/events";
calanderRemiderURL= "content://calendar/reminders";
但是在Android22版本以后,三个URL有了改变,变成如下的样子:
calanderURL = "content://comandroidcalendar/calendars";
calanderEventURL = "content://comandroidcalendar/events";
calanderRemiderURL = "content://comandroidcalendar/reminders";
简单的Demo,按照我的步骤一步一步的来:
第一步:新建一个Android工程命名为CalendarDemo
第二步:修改mainxml布局文件,增加了三个按钮,代码如下:
<xml version="10" encoding="utf-8">
<LinearLayout xmlns:android="http://schemasandroidcom/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/readUserButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Get a User"
/>
<Button
android:id="@+id/readEventButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Get a Event"
/>
<Button
android:id="@+id/writeEventButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Input a Event"
/>
</LinearLayout>
第三步:修改主核心程序CalendarDemojava,代码如下:
package comtutorcalendardemo;
import javautilCalendar;
import androidappActivity;
import androidcontentContentValues;
import androiddatabaseCursor;
import androidnetUri;
import androidosBuild;
import androidosBundle;
import androidviewView;
import androidviewViewOnClickListener;
import androidwidgetButton;
import androidwidgetToast;
public class CalendarDemo extends Activity implements OnClickListener {
private Button mReadUserButton;
private Button mReadEventButton;
private Button mWriteEventButton;
private static String calanderURL = "";
private static String calanderEventURL = "";
private static String calanderRemiderURL = "";
//为了兼容不同版本的日历,22以后url发生改变
static{
if(IntegerparseInt(BuildVERSIONSDK) >= 8){
calanderURL = "content://comandroidcalendar/calendars";
calanderEventURL = "content://comandroidcalendar/events";
calanderRemiderURL = "content://comandroidcalendar/reminders";
}else{
calanderURL = "content://calendar/calendars";
calanderEventURL = "content://calendar/events";
calanderRemiderURL = "content://calendar/reminders";
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
superonCreate(savedInstanceState);
setContentView(Rlayoutmain);
setupViews();
}
private void setupViews(){
mReadUserButton = (Button)findViewById(RidreadUserButton);
mReadEventButton = (Button)findViewById(RidreadEventButton);
mWriteEventButton = (Button)findViewById(RidwriteEventButton);
mReadUserButtonsetOnClickListener(this);
mReadEventButtonsetOnClickListener(this);
mWriteEventButtonsetOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v == mReadUserButton){
Cursor userCursor = getContentResolver()query(Uriparse(calanderURL), null,
null, null, null);
if(userCursorgetCount() > 0){
userCursormoveToFirst();
String userName = userCursorgetString(userCursorgetColumnIndex("name"));
ToastmakeText(CalendarDemothis, userName, ToastLENGTH_LONG)show();
}
}else if(v == mReadEventButton){
Cursor eventCursor = getContentResolver()query(Uriparse(calanderEventURL), null,
null, null, null);
if(eventCursorgetCount() > 0){
eventCursormoveToLast();
String eventTitle = eventCursorgetString(eventCursorgetColumnIndex("title"));
ToastmakeText(CalendarDemothis, eventTitle, ToastLENGTH_LONG)show();
}
}else if(v == mWriteEventButton){
//获取要出入的gmail账户的id
String calId = "";
Cursor userCursor = getContentResolver()query(Uriparse(calanderURL), null,
null, null, null);
if(userCursorgetCount() > 0){
userCursormoveToFirst();
calId = userCursorgetString(userCursorgetColumnIndex("_id"));
}
ContentValues event = new ContentValues();
eventput("title", "与苍井空小-姐动作交流");
eventput("description", "Frankie受空姐邀请,今天晚上10点以后将在Sheraton动作交流lol~");
//插入hoohbood@gmailcom这个账户
eventput("calendar_id",calId);
Calendar mCalendar = CalendargetInstance();
mCalendarset(CalendarHOUR_OF_DAY,10);
long start = mCalendargetTime()getTime();
mCalendarset(CalendarHOUR_OF_DAY,11);
long end = mCalendargetTime()getTime();
eventput("dtstart", start);
eventput("dtend", end);
eventput("hasAlarm",1);
Uri newEvent = getContentResolver()insert(Uriparse(calanderEventURL), event);
long id = LongparseLong( newEventgetLastPathSegment() );
ContentValues values = new ContentValues();
valuesput( "event_id", id );
//提前10分钟有提醒
valuesput( "minutes", 10 );
getContentResolver()insert(Uriparse(calanderRemiderURL), values);
ToastmakeText(CalendarDemothis, "插入事件成功!!!", ToastLENGTH_LONG)show();
}
}
}
第四步:在AndroidManifestxml中申请权限,代码如下:
<xml version="10" encoding="utf-8">
<manifest xmlns:android="http://schemasandroidcom/apk/res/android"
package="comtutorcalendardemo"
android:versionCode="1"
android:versionName="10">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="CalendarDemo"
android:label="@string/app_name">
<intent-filter>
<action android:name="androidintentactionMAIN" />
<category android:name="androidintentcategoryLAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="androidpermissionREAD_CALENDAR"/>
<uses-permission android:name="androidpermissionWRITE_CALENDAR"/>
</manifest>
第五步:运行上述Android工程,查看效果: