Android移动应用开发.docx
- 文档编号:8059175
- 上传时间:2023-01-28
- 格式:DOCX
- 页数:104
- 大小:51.28KB
Android移动应用开发.docx
《Android移动应用开发.docx》由会员分享,可在线阅读,更多相关《Android移动应用开发.docx(104页珍藏版)》请在冰豆网上搜索。
Android移动应用开发
Android移动应用开发
实验指导书
软件学院软件工程系
包晗
实验内容
实验一广播消息处理
【实验类型】验证性
【实验要求】必做
【实验目的】
1、掌握广播的概念;
2、掌握BroadcastReceiver生命周期;
3、掌握广播类的常用方法。
【实验内容】
实验题1.练习发送普通广播。
创建一个Android工程,活动名称为main.java,新建一个广播类,起名为MyReceive1。
【参考程序】
main.java
packagecom.demo;
importandroid.app.Activity;
importandroid.content.BroadcastReceiver;
importandroid.content.Context;
importandroid.content.Intent;
importandroid.content.IntentFilter;
importandroid.os.Bundle;
importandroid.util.Log;
importandroid.view.View;
importandroid.widget.Button;
importandroid.widget.Toast;
publicclassmainextendsActivity{
Buttonbtn1,btn2,btn3,btn4,btn5;
staticfinalStringUser_ACTION="com.testBroadcastReceiver.MyAction";
staticfinalStringUser_ACTION2="com.testBroadcastReceiver.MyAction2";
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1=(Button)this.findViewById(R.id.Button1);
btn1.setOnClickListener(newClickEvent());
btn2=(Button)this.findViewById(R.id.Button2);
btn2.setOnClickListener(newClickEvent());
btn3=(Button)this.findViewById(R.id.Button3);
btn3.setOnClickListener(newClickEvent());
btn4=(Button)this.findViewById(R.id.Button4);
btn4.setOnClickListener(newClickEvent());
btn5=(Button)this.findViewById(R.id.Button5);
btn5.setOnClickListener(newClickEvent());
}
classClickEventimplementsView.OnClickListener{
publicvoidonClick(Viewv){
if(v==btn1)//发送普通广播
{
Intentintent=newIntent(User_ACTION);
sendBroadcast(intent);
}
elseif(v==btn2)//发送顺序广播
{
Intentintent=newIntent(User_ACTION);
sendOrderedBroadcast(intent,null);
}
elseif(v==btn3)//发送持续广播
{
Intentintent=newIntent(User_ACTION);
sendStickyBroadcast(intent);
}
elseif(v==btn4)//动态注册Broadcastreceiver
{
IntentFilterit=newIntentFilter();
it.addAction(User_ACTION);
it.setPriority(10);
registerReceiver(mydynamic,it);
//Toast.makeText(getParent(),"消息处理器注册成功",1000);
IntentFilterit2=newIntentFilter();
it2.addAction(User_ACTION);
it2.setPriority(4);
registerReceiver(mydynamic2,it2);
}
elseif(v==btn5)//发送包含数据的intent
{
Intentintent=newIntent(User_ACTION2);
intent.putExtra("name","hyl");
sendBroadcast(intent);
}
}
/*
*接收动态注册广播的BroadcastReceiver
*/
privateBroadcastReceivermydynamic=newBroadcastReceiver(){
publicvoidonReceive(Contextcontext,Intentintent){
Stringaction=intent.getAction();
Toast.makeText(context,"动态MyReceive接收到:
"+action,1000).show();
Log.i("MyReceive_dynic","currentpriorityis10");
Bundleb=newBundle();
b.putString("name","hyl");
setResultExtras(b);
}
};
}
/*
*接收动态注册广播的BroadcastReceiver
*/
privateBroadcastReceivermydynamic2=newBroadcastReceiver(){
publicvoidonReceive(Contextcontext,Intentintent){
Stringaction=intent.getAction();
Toast.makeText(context,"动态MyReceive2接收到:
"+action,1000).show();
Log.i("MyReceive_dynic2","currentpriorityis4");
Bundleb=newBundle();
b.putString("name","hyl");
setResultExtras(b);
}
};
}
MyReceive1.java
packagecom.demo;
importandroid.content.BroadcastReceiver;
importandroid.content.Context;
importandroid.content.Intent;
importandroid.os.Bundle;
importandroid.util.Log;
importandroid.widget.Toast;
publicclassMyReceive1extendsBroadcastReceiver{
publicvoidonReceive(Contextcontext,Intentintent){
Stringaction=intent.getAction();
Toast.makeText(context,"MyReceive1接收到:
"+action,1000).show();
Log.i("MyReceive1","currentpriorityis0");
//获取上一receiver的处理结果
Bundleb=getResultExtras(true);
if(b!
=null){
Stringname=(String)b.getString("name");
Toast.makeText(context,"上一节点处理数据:
"+name,1000).show();
if(name!
=null){
if(name.equals("hyl"))abortBroadcast();}
}
}
}
布局页面参考书中代码书写。
实验题2.练习发送顺序广播。
新建一个广播类,起名为MyReceive2。
按照书中代码完成类书写。
实验题3.练习发送持续广播。
新建一个广播类,起名为MyReceive3。
按照书中代码完成类书写。
【思考题】
1.各种广播的差别在哪里?
实验二使用内容提供者
【实验类型】验证性
【实验要求】必做
【实验目的】
1、掌握内容提供者(ContentProvider)的概念;
2、掌握ContentProvider的生命周期;
3、了解自定义ContentProvider的使用步骤.
【实验内容】
实验题1.参照书中代码,完成获取联系人的程序。
实验题2.完成一个自定义ContentProvider,实现书籍名的读取。
【参考程序】
main.java
packagecom.demo.provider;
importandroid.app.Activity;
importandroid.content.ContentResolver;
importandroid.content.ContentValues;
importandroid.database.Cursor;
import.Uri;
importandroid.os.Bundle;
importandroid.provider.ContactsContract;
importandroid.util.Log;
publicclassmainextendsActivity{
/**Calledwhentheactivityisfirstcreated.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ContentResolvercr=this.getContentResolver();
ContentValuescv=newContentValues();
cv.put("name","android程序设计基础");
cr.insert(TestContentProviderMetaData.BookTableMetaData.CONTENT_URI,cv);
cv.put("name","JavaEE编程技术"+
"");
cr.insert(TestContentProviderMetaData.BookTableMetaData.CONTENT_URI,cv);
Log.i("test","beginloaddata");
Cursorcursor=cr.query(
TestContentProviderMetaData.BookTableMetaData.CONTENT_URI,
null,null,null,null);
//获得_ID字段的索引
intidIndex=cursor
.getColumnIndex(TestContentProviderMetaData.BookTableMetaData._ID);
//获得Name字段的索引
intnameIndex=cursor
.getColumnIndex(TestContentProviderMetaData.BookTableMetaData.BOOK_NAME);
Stringresult="";
Stringtempid=null;
//遍历Cursor提取数据
for(cursor.moveToFirst();(!
cursor.isAfterLast());cursor.moveToNext()){
result=result+cursor.getString(idIndex)+"\t";
result=result+""+cursor.getString(nameIndex)+"\t\n";
if(tempid==null)tempid=cursor.getString(idIndex);
}
Log.i("test",result);
Log.i("test","beginupdatedata");
UrimyURI=Uri.withAppendedPath(TestContentProviderMetaData.BookTableMetaData.CONTENT_SINGLE_URI,tempid);
ContentValuesvalues=newContentValues();
values.put("author","haoyulong");
values.put("name","JavaEE编程技术");
cr.insert(myURI,values);
}
}
TestContentProvider.java
packagecom.demo.provider;
importjava.util.HashMap;
importcom.demo.provider.TestContentProviderMetaData.BookTableMetaData;
importandroid.content.ContentProvider;
importandroid.content.ContentResolver;
importandroid.content.ContentUris;
importandroid.content.ContentValues;
importandroid.content.Context;
importandroid.content.UriMatcher;
importandroid.database.Cursor;
importandroid.database.SQLException;
importandroid.database.sqlite.SQLiteDatabase;
importandroid.database.sqlite.SQLiteOpenHelper;
importandroid.database.sqlite.SQLiteQueryBuilder;
importandroid.database.sqlite.SQLiteDatabase.CursorFactory;
import.Uri;
importandroid.provider.BaseColumns;
importandroid.text.TextUtils;
importandroid.util.Log;
publicclassTestContentProviderextendsContentProvider{
publicstaticfinalStringTAG="TestContentProvider";
publicDatabaseHelperopenHelper=null;
//--创建表列名与JavaBean映射.
publicstaticHashMap
static{
sBookProjectionMap=newHashMap
sBookProjectionMap.put(BookTableMetaData._ID,BookTableMetaData._ID);
sBookProjectionMap.put(BookTableMetaData.BOOK_NAME,BookTableMetaData.BOOK_NAME);
sBookProjectionMap.put(BookTableMetaData.BOOK_ISBN,BookTableMetaData.BOOK_ISBN);
sBookProjectionMap.put(BookTableMetaData.BOOK_AUTHOR,BookTableMetaData.BOOK_AUTHOR);
sBookProjectionMap.put(BookTableMetaData.CREATED_DATE,BookTableMetaData.CREATED_DATE);
sBookProjectionMap.put(BookTableMetaData.MODIFIED_DATE,BookTableMetaData.MODIFIED_DATE);
}
//--创建URI最佳匹配器
privatestaticUriMatchersUriMatcher=null;
//--注册URI请求类型
privatestaticfinalintINCOMMING_BOOK_COLLECTION_URI_INDICATOR=1;
privatestaticfinalintINCOMMING_SINGLE_BOOK_URI_INDICATOR=2;
static{
sUriMatcher=newUriMatcher(UriMatcher.NO_MATCH);
sUriMatcher.addURI(TestContentProviderMetaData.AUTHORITY,"books",INCOMMING_BOOK_COLLECTION_URI_INDICATOR);
sUriMatcher.addURI(TestContentProviderMetaData.AUTHORITY,"books/#",INCOMMING_SINGLE_BOOK_URI_INDICATOR);
}
@Override
publicintdelete(Uriuri,StringwhereClause,String[]whereArgs){
Log.i(TAG,"del");
//TODOAuto-generatedmethodstub
SQLiteDatabasedb=openHelper.getWritableDatabase();
intcount=0;
switch(sUriMatcher.match(uri)){
caseINCOMMING_BOOK_COLLECTION_URI_INDICATOR:
count=db.delete(BookTableMetaData.TABLE_NAME,whereClause,whereArgs);
break;
caseINCOMMING_SINGLE_BOOK_URI_INDICATOR:
StringrowID=uri.getPathSegments().get
(1);
Stringwhere=BookTableMetaData._ID+"="+rowID+(!
TextUtils.isEmpty(whereClause)?
"AND("+whereClause+')':
"");
count=db.delete(BookTableMetaData.TABLE_NAME,where,whereArgs);
break;
default:
thrownewIllegalArgumentException("UnknownURI"+uri);
}
this.getContext().getContentResolver().notifyChange(uri,null);
returncount;
}
@Override
publicStringgetType(Uriuri){
switch(sUriMatcher.match(uri)){
caseINCOMMING_BOOK_COLLECTION_URI_INDICATOR:
returnBookTableMetaData.CONTENT_TYPE;
caseINCOMMING_SINGLE_BOOK_URI_INDICATOR:
returnBookTableMetaData.CONTENT_ITEM_TYPE;
default:
thrownewIllegalArgumentException("UnknownURI"+uri);
}
}
@Override
publicUriinsert(Uriuri,ContentValuesvalues){
//TODOAuto-generatedmethodstub
Log.i(TAG,"insert");
//if(sUriMatcher.match(uri)!
=INCOMMING_BOOK_COLLECTION_URI_INDICATOR){
//thrownewIllegalArgumentException("UnknownURI"+uri);
//}
longnow=
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Android 移动 应用 开发