智能手机应用开发.ppt
《智能手机应用开发.ppt》由会员分享,可在线阅读,更多相关《智能手机应用开发.ppt(38页珍藏版)》请在三一文库上搜索。
1、智能手机应用开发,based on android 2012.5,内容包括:,1、Android 平台概述 2、搭建 Android 开发环境 3、第一个 Android 应用程序 4、Android 用户界面设计 5、Activity、Intent、Service、Broadcast Receiver 6、Android 中的数据存取 7、Android 实验设计,课程结构,开放手机联盟(OHA) Android 平台综述: 平台介绍、开发者社区 开发环境: 构建 Android 开发环境 应用程序: 应用程序结构 典型应用: 用户界面、图形编程 数据存取、网络连接 硬件访问,获取 Andr
2、oid 资料的途径,http:/ http:/ 下载 sdk 等相关软件 查看文档 在线帮助 开发社区 国内需要代理才能访问 http:/ news sdk下载 开发FAQ、源码下载 移植,6、Android 中的数据存取,on android,数据存取方式,Preference “键-值”方式存储,以 xml 文件的形式保存 File 采用 java.io.* 库所提供 I/O 接口读写文件 SQLite SQLite 是轻量级的嵌入式数据库引擎 Content Provider 用于实现不同应用程序之间的数据共享,1、Preference,主要用于存储数据较少的场合,比如配置信息 文件位置
3、:/data/data/shared_prefs/*.xml 需要用到接口:SharedPreferences 及其内部接口:SharedPreferences.Editor 取得接口: Context.SharedPreferences(filename,mode) SharedPreferences.edit() 数据类型、数据操作: int、flaot、string、boolean等 putString(key,value) getString(key,defValue),实例:Test_of_Preference,定义 main.xml: 一个 EditText 用于键入数据 三个 B
4、utton 分别用于将数据写入 xml 文件、从文件获取数据并显示、清除文件中的数据 定义 sharedpreferences.xml: 一个 TextView 用于显示从文件中获取的数据,et1=(EditText)findViewById(R.id.editText1); button1=(Button)findViewById(R.id.button1); button2=(Button)findViewById(R.id.button2); button3=(Button)findViewById(R.id.button3);,textView1=(TextView)findViewB
5、yId(R.id.textView1);,创建接口 sp1、spEditor1 以键值 方式加入数据 以 String Key 为索引来获取数据 清除数据,private SharedPreferences sp1; sp1 = this.getSharedPreferences(“test“, MODE_PRIVATE); SharedPreferences.Editor spEditor1 = sp1.edit();,spEditor1.putString(“TEXT“,et1.getText().toString(); spEmit();,String string1 = sp1.get
6、String(“TEXT“, “); textView1.setText(string1);,spEditor1.clear().commit();,运行结果:,2、File,文件可用来存放大量数据,如文本、图片、音频等 默认位置:/data/data/files/*.* java.io.* 库提供 I/O 接口实现本地文件读写 文件输入流的获取 Context.openFileInput(String name) 文件输出流的获取 Context.openFileOutput(String name, int mode) 包内资源(res/raw/)文件的读取 Resources.openR
7、awResource(R.raw.file),实例:Test_of_File,定义 main.xml: 两个 EditText、两个 Button 写文件 writeFile(str):,private void writeFile(String str) try FileOutputStream output= openFileOutput(FILE_NAME,MODE_APPEND); output.write(str.getBytes(); output.close(); catch(Exception e) Log.e(“File_IO“, e.toString(); this.fin
8、ish(); ,读文件 readFile():,private String readFile() try FileInputStream input = openFileInput(FILE_NAME); byte buffer = new byteinput.available(); input.read(buffer); input.close(); String str = new String(buffer); return str; catch(Exception e) Log.e(“File_IO“, e.toString(); this.finish(); return nul
9、l; ,editText2.setText(readFile();,运行结果: 注意:写文件时用的 MODE_APPEND 模式,3、SQLite,SQLite 轻量级嵌入式数据库引擎,面向资源有限的设备 没有服务器进程 所有数据存放在同一文件中 跨平台,可自由复制 SQLiteOpenHelper 帮助类,用于管理数据库创建和版本更新 onCreate(SQLiteDatabase db) onUpgrade(db,int old_ver,int new_ver) sqlite3 :位于 /tools,实例:Test_of_SQLite,DatabaseHelper 继承自 SQLiteOp
10、enHelper 重写了onCreate()、onUpgrade() 方法 实现 insert、del、query、close 等方法 ListView 显示记录,字段对应成 TextView ListView 的 onItemClick() 实现删除记录 数据库文件路径: /data/data/database/*.db,private SQLiteDatabase db; public void insert(ContentValues values) SQLiteDatabase db = getWritableDatabase(); db.insert(TABLE_NAME, null
11、, values); db.close(); public void del(int id) if(db = null) db = getWritableDatabase(); db.delete(TABLE_NAME, “_id=?“, new StringString.valueOf(id); public void onCreate(SQLiteDatabase db) this.db = db; db.execSQL(CREATE_TABLE); ,DatabaseHelper.java,DatabaseHelper dbHelper = new DatabaseHelper(this
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 智能手机 应用 开发
链接地址:https://www.31doc.com/p-2800527.html