Android实验设计.docx

上传人:b****4 文档编号:27517971 上传时间:2023-07-02 格式:DOCX 页数:23 大小:594.37KB
下载 相关 举报
Android实验设计.docx_第1页
第1页 / 共23页
Android实验设计.docx_第2页
第2页 / 共23页
Android实验设计.docx_第3页
第3页 / 共23页
Android实验设计.docx_第4页
第4页 / 共23页
Android实验设计.docx_第5页
第5页 / 共23页
点击查看更多>>
下载资源
资源描述

Android实验设计.docx

《Android实验设计.docx》由会员分享,可在线阅读,更多相关《Android实验设计.docx(23页珍藏版)》请在冰豆网上搜索。

Android实验设计.docx

Android实验设计

Android实验设计

实验一:

系统安装与HelloWorld

【目的】

安装智能手机开发相关软件平台。

【要求】

1、完成智能手机开发平台安装、以及相关配置

2、并实现HelloWorld

3、了解项目的基本文件目录结构

【原理】

Eclipse安装原理,Android编程方法

【过程】

1、安装JAVAJDK

下载网址:

2、安装Eclipse

下载网址:

http:

//www.eclipse.org/downloads/

3、安装Android

4、安装ADT(AndroidDevelopmentTools)

5、安装手机USB驱动

如果用模拟器调试,则可暂时不装。

6、建立新项目,实现HelloWorld。

OpenEclipse.

ClickthemenuFile->New->Project.

ExpandtheAndroidfolderandselectAndroidProject.

NametheprojectHelloWorld

得到的文件结构如下:

运行:

选运行的设备,可以是模拟器,也可以是真机(如果已经连接好真实手机的话):

 

模拟器运行:

 

真实手机调试:

实验二:

界面设计:

控件与布局

【目的】

Android编程基础,UI设计。

【要求】

1、了解Android编程原理

2、掌握界面控件设计

3、掌握控件的事件处理编程

【原理】

UI设计原理

【过程】

1、了解各种控件的基本功能

各种控件:

Menu

TextView、EditText、

Button

Radiobutton

List

ProgressBar;

2、了解布局Layout的应用

多种Layout:

AbsoluteLayout

FrameLayout

GridView

LinearLayout

ListLayout

RadioGroup

TableLayout

………

3、利用布局安排各种控件,设计良好用户界面

android="

android:

orientation="vertical"

android:

layout_width="fill_parent"

android:

layout_height="fill_parent"

>

id="@+id/TextView01"

android:

layout_width="fill_parent"

android:

layout_height="wrap_content"

android:

text="@string/hello"

/>

id="@+id/EditText01"

android:

layout_width="fill_parent"

android:

layout_height="wrap_content"

/>

id="@+id/ImageView01"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

src="@drawable/adr"

/>

id="@+id/LinearLayout01"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

orientation="horizontal">

id="@+id/Button01"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

text="@string/btn_name"

/>

id="@+id/Button02"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

text="@string/stp_name"

/>

id="@+id/progressbar01"

android:

layout_width="fill_parent"

android:

layout_height="20px"

style="?

android:

attr/progressBarStyleHorizontal"

/>

id="@+id/seekbar01"

android:

layout_width="fill_parent"

android:

layout_height="20px"

style="?

android:

attr/progressBarStyleHorizontal"

/>

 

实验三:

图形绘制与OpenGL

【目的】

在屏幕绘制各种图形,了解OpenGL

【要求】

1、了解在屏幕绘图方法

2、了解OpenGL

【原理】

【过程】

1、绘制直线、园、曲线等各种图形

2、显示字符

3、利用OpenGL编程方法

publicvoidonDrawFrame(GL10gl){

//一般的opengl程序,首先要做的就是清屏

gl.glClear(GL10.GL_COLOR_BUFFER_BIT|GL10.GL_DEPTH_BUFFER_BIT);

//紧接着设置模型视图矩阵

gl.glMatrixMode(GL10.GL_MODELVIEW);

gl.glLoadIdentity();//清空矩阵

GLU.gluLookAt(gl,0,0,3,0,0,0,0,1,0);//视点变换,将相机位置设置为(0,0,3),同时指向(0,0,0)点

//设置模型位置旋转及缩放信息

gl.glTranslatef(0.0f,0.0f,-1.0f);//将模型位置设置为(0,0,-1)

floatangle=30.0f;

gl.glRotatef(angle,0,1,0);//绕模型自身Y轴旋转30度

gl.glRotatef(angle,1,0,0);//绕模型自身X轴旋转30度

gl.glScalef(1.2f,1.2f,1.2f);//设置三方向的缩放系数

//设置颜色

gl.glColor4f(0.0f,0.0f,0.0f,1.0f);

//渲染立方体

mCube.draw(gl,gl.GL_TRIANGLES);

//mCube.draw(gl,gl.GL_LINES);

}

   

实验四:

网络访问与服务

【目的】

掌握Android网络访问方法

【要求】

1、了解手机WEB网站访问编程

2、通过网络进行数据访问

3、了解数据库使用

【原理】

利用Android网络访问协议

【过程】

1、访问WEB网站,通过HttpResponse类,读入网络数据。

HttpClientclient=newDefaultHttpClient();

HttpGetget=newHttpGet(url);

HttpResponseresponse=client.execute(get);

HttpEntityentity=response.getEntity();

//尝试读取entity的长度,返回-1表示长度未知

longlength=entity.getContentLength();

InputStreamis=entity.getContent();

Strings=null;

if(is!

=null){

ByteArrayOutputStreambaos=newByteArrayOutputStream();

byte[]buf=newbyte[512];

intch=-1;

intcount=0;

while((ch=is.read(buf))!

=-1){

baos.write(buf,0,ch);

count+=ch;

//如果长度已知,可以通过taskProgress()通知监听者任务执行的比例

if(length>0){

listener.taskProgress(this,count,length);

}

//为了更好的演示进度,让线程休眠100ms

Thread.sleep(100);

}

Log.e("HttpTask","length="+baos.toByteArray().length);

//返回内容

s=newString(baos.toByteArray());

}

returns;

读入网站数据:

实验五:

硬件访问与传感器

【目的】

通过底层API访问手机硬件及手机上的各种传感器

【要求】

1、获取手机上电话、短信等各种功能的编程

2、了解手机上各种传感器的功能与使用方法

【原理】

利用手机本身的功能与相关传感器的使用

【过程】

1、了解程序使用手机电话功能的方法

短信收发:

packagecom.android.TinySMS;

importandroid.app.Activity;

importandroid.app.PendingIntent;

importandroid.content.BroadcastReceiver;

importandroid.content.Context;

importandroid.content.Intent;

importandroid.os.Bundle;

importandroid.telephony.gsm.SmsManager;

importandroid.view.View;

importandroid.widget.Button;

importandroid.widget.EditText;

importandroid.widget.Toast;

publicclassTinySMSextendsActivity{

publicstaticfinalStringSMS_ACTION="com.android.TinySMS.RESULT";

//privateTextViewmessage;

privateButtonsnd;

privateEditTexttel;

privateEditTexttxt;

privateSentReceiverreceiver=newSentReceiver();

privateclassSentReceiverextendsBroadcastReceiver{

@Override

publicvoidonReceive(Contextcontext,Intentintent){

if(intent.getAction().equals(SMS_ACTION)){

intcode=getResultCode();

//短消息发送成功

if(code==Activity.RESULT_OK)

Toast.makeText(TinySMS.this,R.string.msg_sent,

Toast.LENGTH_SHORT).show();

}

}

};

/**Calledwhentheactivityisfirstcreated.*/

@Override

publicvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

tel=(EditText)findViewById(R.id.EditText01);

tel.setText("5554");//模拟器之间互发短信

txt=(EditText)findViewById(R.id.EditText02);

txt.setText("我用自己的程序试试发短信。

");

snd=(Button)findViewById(R.id.Button01);

snd.setOnClickListener(newView.OnClickListener(){

publicvoidonClick(Viewarg0){

StringphoneNo=tel.getText().toString();

Stringmessage=txt.getText().toString();

if(phoneNo.length()>0&&message.length()>0){

sendSMS(phoneNo,message);

}else{

Toast.makeText(TinySMS.this,

"请重新输入电话号码和短信内容",

Toast.LENGTH_LONG).show();

}

}

});

}

privatevoidsendSMS(Stringaddress,Stringcontent)

{

SmsManagermanager=SmsManager.getDefault();

Intenti=newIntent(SMS_ACTION);

//生成PendingIntent,当消息发送完成,接收到广播

PendingIntentsentIntent=PendingIntent.getBroadcast(

this,

0,

i,

PendingIntent.FLAG_ONE_SHOT);

manager.sendTextMessage(

address,

null,

content,

sentIntent,

null);

}

}

如果要发短信,还需在AndroidManifest.xml中声明权限:

name="android.permission.READ_SMS">

name="android.permission.SEND_SMS">

2、手机上有多种传感器,可以对这些传感器进行编程。

相机拍摄:

packagecom.android.cameraAndroid;

importandroid.app.Activity;

importandroid.os.Bundle;

importjava.io.File;

importjava.io.FileOutputStream;

importjava.io.IOException;

importandroid.content.Context;

importandroid.content.Intent;

importandroid.graphics.PixelFormat;

importandroid.hardware.Camera;

importandroid.hardware.Camera.Parameters;

importandroid.hardware.Camera.PictureCallback;

importandroid.hardware.Camera.ShutterCallback;

importandroid.media.AudioManager;

importandroid.media.ToneGenerator;

import.Uri;

importandroid.os.Environment;

importandroid.os.StatFs;

importandroid.view.Menu;

importandroid.view.MenuItem;

importandroid.view.SurfaceHolder;

importandroid.view.SurfaceView;

publicclassCameraAndroidextendsActivity{

privateCameraPreviewpreview;

privateCameracamera;

privateToneGeneratortone;

privatestaticfinalintOPTION_SNAPSHOT=0;

@Override

publicvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

preview=newCameraPreview(this);

setContentView(preview);

}

@Override

publicbooleanonOptionsItemSelected(MenuItemitem){

intitemId=item.getItemId();

switch(itemId){

caseOPTION_SNAPSHOT:

//拍摄照片

camera.takePicture(shutterCallback,null,jpegCallback);

break;

}

returntrue;

}

//返回照片的JPEG格式的数据

privatePictureCallbackjpegCallback=newPictureCallback(){

publicvoidonPictureTaken(byte[]data,Cameracamera){

Parametersps=camera.getParameters();

if(ps.getPictureFormat()==PixelFormat.JPEG){

//存储拍照获得的图片

Stringpath=save(data);

//将图片交给Image程序处理

Uriuri=Uri.fromFile(newFile(path));

Intentintent=newIntent();

intent.setAction("android.intent.action.VIEW");

intent.setDataAndType(uri,"image/jpeg");

startActivity(intent);

}

}

};

//快门按下的时候onShutter()被回调

privateShutterCallbackshutterCallback=newShutterCallback(){

publicvoidonShutter(){

if(tone==null)

//发出提示用户的声音

tone=newToneGenerator(AudioManager.STREAM_MUSIC,

ToneGenerator.MAX_VOLUME);

tone.startTone(ToneGenerator.TONE_PROP_BEEP2);

}

};

privateStringsave(byte[]data){

Stringpath="/sdcard/"+System.currentTimeMillis()+".jpg";

try{

//判断SD卡上是否有足够的空间

Stringstorage=Environment.getExternalStorageDirectory().toString();

StatFsfs=newStatFs(storage);

longavailable=fs.getAvailableBlocks()*fs.getBlockSize();

if(available

//空间不足直接返回空

returnnull;

}

Filefile=newFile(path);

if(!

file.exists())

//创建文件

file.createNewFile();

FileOutputStreamfos=newFileOutputStream(file);

fos.write(data);

fos.close();

}catch(Exceptione){

e.printStackTrace();

retu

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 初中教育 > 学科竞赛

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1