1、Android实验设计Android实验设计实验一:系统安装与HelloWorld【目的】安装智能手机开发相关软件平台。【要求】1、 完成智能手机开发平台安装、以及相关配置2、 并实现Hello World3、 了解项目的基本文件目录结构【原理】Eclipse安装原理,Android编程方法【过程】1、 安装JAVA JDK下载网址:2、 安装Eclipse下载网址:http:/www.eclipse.org/downloads/3、 安装Android或4、 安装ADT(Android Development Tools)或5、 安装手机USB驱动或如果用模拟器调试,则可暂时不装。6、 建立
2、新项目,实现Hello World。Open Eclipse.Click the menu File - New - Project.Expand the Android folder and select Android Project.Name the project HelloWorld得到的文件结构如下:运行:选运行的设备,可以是模拟器,也可以是真机(如果已经连接好真实手机的话):模拟器运行:真实手机调试:实验二:界面设计:控件与布局【目的】Android编程基础,UI设计。【要求】1、 了解Android编程原理2、 掌握界面控件设计3、 掌握控件的事件处理编程【原理】UI设计原理【
3、过程】1、 了解各种控件的基本功能各种控件:MenuTextView、EditText、ButtonRadio buttonListProgressBar;2、 了解布局Layout的应用多种Layout:AbsoluteLayoutFrameLayoutGridViewLinearLayoutListLayoutRadioGroupTableLayout3、 利用布局安排各种控件,设计良好用户界面 实验三:图形绘制与OpenGL【目的】在屏幕绘制各种图形,了解OpenGL【要求】1、 了解在屏幕绘图方法2、 了解OpenGL【原理】【过程】1、 绘制直线、园、曲线等各种图形2、 显示字符3、
4、 利用OpenGL编程方法 public void onDrawFrame(GL10 gl) /一般的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)点 /设置模型位
5、置旋转及缩放信息 gl.glTranslatef(0.0f, 0.0f, -1.0f);/将模型位置设置为(0, 0, -1) float angle = 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); /mC
6、ube.draw(gl, gl.GL_LINES); 实验四:网络访问与服务【目的】掌握Android网络访问方法【要求】1、 了解手机WEB网站访问编程2、 通过网络进行数据访问3、 了解数据库使用【原理】利用Android网络访问协议【过程】1、 访问WEB网站,通过HttpResponse类,读入网络数据。 HttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet(url); HttpResponse response = client.execute(get); HttpEntity entity =
7、response.getEntity(); /尝试读取entity的长度,返回-1表示长度未知 long length = entity.getContentLength(); InputStream is = entity.getContent(); String s = null; if (is != null) ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte buf = new byte512; int ch = -1; int count = 0; while (ch = is.read(buf) != -1
8、) 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 = new String(baos.toByteArray(); return s;读入网站数据:实验五:硬件访问与传感器
9、【目的】通过底层API访问手机硬件及手机上的各种传感器【要求】1、 获取手机上电话、短信等各种功能的编程2、 了解手机上各种传感器的功能与使用方法【原理】利用手机本身的功能与相关传感器的使用【过程】1、 了解程序使用手机电话功能的方法短信收发:package com.android.TinySMS;import android.app.Activity;import android.app.PendingIntent;import android.content.BroadcastReceiver;import android.content.Context;import android.co
10、ntent.Intent;import android.os.Bundle;import android.telephony.gsm.SmsManager;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class TinySMS extends Activity public static final String SMS_ACTION = com.android.TinySMS.RESULT;/ pr
11、ivate TextView message; private Button snd; private EditText tel; private EditText txt; private SentReceiver receiver = new SentReceiver(); private class SentReceiver extends BroadcastReceiver Override public void onReceive(Context context, Intent intent) if (intent.getAction().equals(SMS_ACTION) in
12、t code = getResultCode(); /短消息发送成功 if(code = Activity.RESULT_OK) Toast.makeText(TinySMS.this, R.string.msg_sent, Toast.LENGTH_SHORT).show(); ; /* Called when the activity is first created. */ Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(
13、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(new View.OnClickListener() public void onClick(View arg0) Strin
14、g phoneNo = tel.getText().toString(); String message = txt.getText().toString(); if (phoneNo.length()0 & message.length()0) sendSMS(phoneNo, message); else Toast.makeText(TinySMS.this, 请重新输入电话号码和短信内容, Toast.LENGTH_LONG).show(); ); private void sendSMS(String address, String content) SmsManager manag
15、er = SmsManager.getDefault(); Intent i = new Intent(SMS_ACTION); /生成PendingIntent,当消息发送完成,接收到广播 PendingIntent sentIntent = PendingIntent.getBroadcast( this, 0, i, PendingIntent.FLAG_ONE_SHOT); manager.sendTextMessage( address, null, content, sentIntent, null); 如果要发短信,还需在AndroidManifest.xml中声明权限:2、 手
16、机上有多种传感器,可以对这些传感器进行编程。相机拍摄:package com.android.cameraAndroid;import android.app.Activity;import android.os.Bundle;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import android.content.Context;import android.content.Intent;import android.graphics.PixelFormat;import and
17、roid.hardware.Camera;import android.hardware.Camera.Parameters;import android.hardware.Camera.PictureCallback;import android.hardware.Camera.ShutterCallback;import android.media.AudioManager;import android.media.ToneGenerator;import .Uri;import android.os.Environment;import android.os.StatFs;import
18、android.view.Menu;import android.view.MenuItem;import android.view.SurfaceHolder;import android.view.SurfaceView;public class CameraAndroid extends Activity private CameraPreview preview; private Camera camera; private ToneGenerator tone; private static final int OPTION_SNAPSHOT = 0; Override public
19、 void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); preview = new CameraPreview(this); setContentView(preview); Override public boolean onOptionsItemSelected(MenuItem item) int itemId = item.getItemId(); switch(itemId) case OPTION_SNAPSHOT: /拍摄照片 camera.takePicture(shutterC
20、allback, null, jpegCallback); break; return true; /返回照片的JPEG格式的数据 private PictureCallback jpegCallback = new PictureCallback() public void onPictureTaken(byte data, Camera camera) Parameters ps = camera.getParameters(); if(ps.getPictureFormat() = PixelFormat.JPEG) /存储拍照获得的图片 String path = save(data)
21、; /将图片交给Image程序处理 Uri uri = Uri.fromFile(new File(path); Intent intent = new Intent(); intent.setAction(android.intent.action.VIEW); intent.setDataAndType(uri, image/jpeg); startActivity(intent); ; /快门按下的时候onShutter()被回调 private ShutterCallback shutterCallback = new ShutterCallback() public void onS
22、hutter() if(tone = null) /发出提示用户的声音 tone = new ToneGenerator(AudioManager.STREAM_MUSIC, ToneGenerator.MAX_VOLUME); tone.startTone(ToneGenerator.TONE_PROP_BEEP2); ; private String save(byte data) String path = /sdcard/+System.currentTimeMillis()+.jpg; try /判断SD卡上是否有足够的空间 String storage = Environment.
23、getExternalStorageDirectory().toString(); StatFs fs = new StatFs(storage); long available = fs.getAvailableBlocks()*fs.getBlockSize(); if(availabledata.length) /空间不足直接返回空 return null; File file = new File(path); if(!file.exists() /创建文件 file.createNewFile(); FileOutputStream fos = new FileOutputStream(file); fos.write(data); fos.close(); catch (Exception e) e.printStackTrace(); retu
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1