实习报告书
专 业 计算机科学与技术
系 别
报告题目 安卓
报告人 班 级
指导教师 带队教师
实习时间 实习地点
教务处监制
第二篇:安卓通讯录实训报告
安卓实训设计报告
安卓通讯录
设计题目: 安卓通讯录
班 级:
姓 名:
学 号:
指导老师:
日 期:
内容要求
一、题目分析,功能要求。
1.1 实验目的
熟悉Android软件开发的基本架构
利用Eclipse和ADT插件设计通讯录
1.2 功能
本手机通讯录工具主要实现五大功能:联系人的查询:字段查询,分组查询,字母排序查询;增加、删除联系人以及修改联系人信息;导入、导出联系人;发送联系人信息;设置。
二、实验设计
2.1 UI设计
我们用一个ListView来显示整个通讯录,其中用TextView显示每一记录,他们的xml文件分别为:main.xml(通讯录主界面),addres.xml(添加联系人界面),list_item.xml(浏览联系人界面),find.xml(查找联系人界面)。
2.2 数据库的设计
Android中的android.database.sqlite.SQLiteOpenHelper 类是一个专门用于数据库创建和版本管理的辅助类。因此,为了更好的管理数据库,这里我们创建一个继承自SQLiteOpenHelper的辅助类DBHelper来维护和更新数据库。
2.3功能的设计
为了在主界面中浏览联系人的信息,并且创建主要菜单栏,我们设计了MainActivity类,主要用于显示联系人信息和菜单栏,通过菜单栏,实现通讯录的相关功能。AddressBook类主要是为了实现联系人的添加功能,并且实现信息的保存后跳转到主界面。Findactivity类主要是为了实现联系人的查找功能,输入联系人的姓名,点击查找按钮,显示所查联系人的相关信息。
三、实验程序
见附录一。
四、实验效果图
五、总结
通过设计该通讯录,主要学习了UI设计、数据库的综合操作、动态菜单的使用以及各种权限的注册。通过本次设计,使我对Android平台的数据库操作有了更进一步的理解,同时也对Android系统有了更深入的了解。
附录一
//定义数据
publicclass ContactColumn implements BaseColumns
{
public ContactColumn()
{
}
//列名
publicstaticfinal String NAME = "name"; //姓名
publicstaticfinal String MOBILENUM = "mobileNumber"; //移动电话
publicstaticfinal String HOMENUM = "homeNumber"; //家庭电话
publicstaticfinal String ADDRESS = "address"; //地址
publicstaticfinal String EMAIL = "email"; //邮箱
publicstaticfinal String BLOG = "blog"; //博客
//列 索引值
publicstaticfinalint_ID_COLUMN = 0;
publicstaticfinalintNAME_COLUMN = 1;
publicstaticfinalintMOBILENUM_COLUMN = 2;
publicstaticfinalintHOMENUM_COLUMN = 3;
publicstaticfinalintADDRESS_COLUMN = 4;
publicstaticfinalintEMAIL_COLUMN = 5;
publicstaticfinalintBLOG_COLUMN = 6;
//查询结果
publicstaticfinal String[] PROJECTION ={
_ID,
NAME,
MOBILENUM,
HOMENUM,
ADDRESS,
EMAIL,
BLOG,
};
}
publicclass DBHelper extends SQLiteOpenHelper
{
publicstaticfinal String DATABASE_NAME = "mycontacts.db";//数据库名
publicstaticfinalintDATABASE_VERSION = 2; //版本
publicstaticfinal String CONTACTS_TABLE = "contacts"; //表名
//创建表
privatestaticfinal String DATABASE_CREATE =
"CREATE TABLE " + CONTACTS_TABLE +" ("
+ ContactColumn._ID+" integer primary key autoincrement,"
+ ContactColumn.NAME+" text,"
+ ContactColumn.MOBILENUM+" text,"
+ ContactColumn.HOMENUM+" text,"
+ ContactColumn.ADDRESS+" text,"
+ ContactColumn.EMAIL+" text,"
+ ContactColumn.BLOG+" text);";
public DBHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
publicvoid onCreate(SQLiteDatabase db)
{
db.execSQL(DATABASE_CREATE);
}
publicvoid onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS " + CONTACTS_TABLE);
onCreate(db);
}
}
// URI类型转换
public String getType(Uri uri)
{
switch (uriMatcher.match(uri))
{
caseCONTACTS:
return "vnd.android.cursor.dir/vnd.yarin.android.mycontacts";
caseCONTACT_ID:
return "vnd.android.cursor.item/vnd.yarin.android.mycontacts";
default:
thrownew IllegalArgumentException("Unsupported URI: " + uri);
}
}
// 删除指定数据列
@Override
publicint delete(Uri uri, String where, String[] selectionArgs)
{
int count;
switch (uriMatcher.match(uri))
{
caseCONTACTS:
count = contactsDB.delete(CONTACTS_TABLE, where, selectionArgs);
break;
caseCONTACT_ID:
String contactID = uri.getPathSegments().get(1);
count = contactsDB.delete(CONTACTS_TABLE,
ContactColumn._ID
+ "=" + contactID
+ (!TextUtils.isEmpty(where) ? " AND (" + where + ")" : ""),
selectionArgs);
break;
default:
thrownew IllegalArgumentException("Unsupported URI: " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
// 插入数据
public Uri insert(Uri uri, ContentValues initialValues)
{
if (uriMatcher.match(uri) != CONTACTS)
{
thrownew IllegalArgumentException("Unknown URI " + uri);
}
ContentValues values;
if (initialValues != null)
{
values = new ContentValues(initialValues);
Log.e(TAG + "insert", "initialValues is not null");
}
else
{
values = new ContentValues();
}
// 设置默认值
if (values.containsKey(ContactColumn.NAME) == false)
{
values.put(ContactColumn.NAME, "");
}
if (values.containsKey(ContactColumn.MOBILENUM) == false)
{
values.put(ContactColumn.MOBILENUM, "");
}
if (values.containsKey(ContactColumn.HOMENUM) == false)
{
values.put(ContactColumn.HOMENUM, "");
}
if (values.containsKey(ContactColumn.ADDRESS) == false)
{
values.put(ContactColumn.ADDRESS, "");
}
if (values.containsKey(ContactColumn.EMAIL) == false)
{
values.put(ContactColumn.EMAIL, "");
}
if (values.containsKey(ContactColumn.BLOG) == false)
{
values.put(ContactColumn.BLOG, "");
}
Log.e(TAG + "insert", values.toString());
long rowId = contactsDB.insert(CONTACTS_TABLE, null, values);
if (rowId > 0)
{
Uri noteUri = ContentUris.withAppendedId(CONTENT_URI, rowId);
getContext().getContentResolver().notifyChange(noteUri, null);
Log.e(TAG + "insert", noteUri.toString());
return noteUri;
}
thrownew SQLException("Failed to insert row into " + uri);
}
// 更新数据库
publicint update(Uri uri, ContentValues values, String where, String[] selectionArgs)
{
int count;
Log.e(TAG + "update", values.toString());
Log.e(TAG + "update", uri.toString());
Log.e(TAG + "update :match", "" + uriMatcher.match(uri));
switch (uriMatcher.match(uri))
{
caseCONTACTS:
Log.e(TAG + "update", CONTACTS + "");
count = contactsDB.update(CONTACTS_TABLE, values, where, selectionArgs);
break;
caseCONTACT_ID:
String contactID = uri.getPathSegments().get(1);
Log.e(TAG + "update", contactID + "");
count = contactsDB.update(CONTACTS_TABLE, values, ContactColumn._ID + "=" + contactID
+ (!TextUtils.isEmpty(where) ? " AND (" + where + ")" : ""), selectionArgs);
break;
default:
thrownew IllegalArgumentException("Unsupported URI: " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
}
publicboolean onPrepareOptionsMenu(Menu menu)
{
super.onPrepareOptionsMenu(menu);
finalboolean haveItems = getListAdapter().getCount() > 0;
if (haveItems)
{
Uri uri = ContentUris.withAppendedId(getIntent().getData(), getSelectedItemId());
Intent[] specifics = new Intent[2];
specifics[0] = new Intent(Intent.ACTION_EDIT, uri);
specifics[1] = new Intent(Intent.ACTION_VIEW, uri);
MenuItem[] items = new MenuItem[2];
//添加满足条件的菜单
Intent intent = new Intent(null, uri);
intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 0, 0, null, specifics, intent, 0, items);
if (items[0] != null)
{
//编辑联系人
items[0].setShortcut('1', 'e').setIcon(R.drawable.edituser).setTitle(R.string.editor_user);
}
if (items[1] != null)
{
//查看联系人
items[1].setShortcut('2', 'f').setTitle(R.string.view_user).setIcon(R.drawable.viewuser);
}
}
else
{
menu.removeGroup(Menu.CATEGORY_ALTERNATIVE);
}
returntrue;
}
"1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
package="com.yarin.android.MyContacts"
android:versionCode="1"
android:versionName="1.0">
"@drawable/icon" android:label="@string/app_name">
"ContactsProvider"
android:authorities="com.yarin.android.provider.ContactsProvider"/>
".MyContacts"
android:label="@string/app_name">
"android.intent.action.MAIN" />
"android.intent.category.LAUNCHER" />
".ContactEditor"
android:label="@string/editor_user">
"android.intent.action.EDIT" />
"android.intent.category.DEFAULT" />
"vnd.android.cursor.item/vnd.yarin.android.mycontacts" />
"android.intent.action.INSERT" />
"android.intent.category.DEFAULT" />
"vnd.android.cursor.dir/vnd.yarin.android.mycontacts" />
".ContactView"
android:label="@string/view_user">
"android.intent.action.VIEW" />
"android.intent.category.DEFAULT" />
"vnd.android.cursor.item/vnd.yarin.android.mycontacts" />
"android.intent.category.DEFAULT" />
"vnd.android.cursor.dir/vnd.yarin.android.mycontacts" />
"android.permission.CALL_PHONE">
"android.permission.SEND_SMS">
"android.permission.RECEIVE_SMS" />
"5" />