智能手機應用開發(fā).ppt_第1頁
智能手機應用開發(fā).ppt_第2頁
智能手機應用開發(fā).ppt_第3頁
智能手機應用開發(fā).ppt_第4頁
智能手機應用開發(fā).ppt_第5頁
已閱讀5頁,還剩33頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

智能手機應用開發(fā),based on android 2012.5,內容包括:,1、Android 平臺概述 2、搭建 Android 開發(fā)環(huán)境 3、第一個 Android 應用程序 4、Android 用戶界面設計 5、Activity、Intent、Service、Broadcast Receiver 6、Android 中的數(shù)據(jù)存取 7、Android 實驗設計,課程結構,開放手機聯(lián)盟(OHA) Android 平臺綜述: 平臺介紹、開發(fā)者社區(qū) 開發(fā)環(huán)境: 構建 Android 開發(fā)環(huán)境 應用程序: 應用程序結構 典型應用: 用戶界面、圖形編程 數(shù)據(jù)存取、網(wǎng)絡連接 硬件訪問,獲取 Android 資料的途徑, 下載 sdk 等相關軟件 查看文檔 在線幫助 開發(fā)社區(qū) 國內需要代理才能訪問 / news sdk下載 開發(fā)FAQ、源碼下載 移植,6、Android 中的數(shù)據(jù)存取,on android,數(shù)據(jù)存取方式,Preference “鍵-值”方式存儲,以 xml 文件的形式保存 File 采用 java.io.* 庫所提供 I/O 接口讀寫文件 SQLite SQLite 是輕量級的嵌入式數(shù)據(jù)庫引擎 Content Provider 用于實現(xiàn)不同應用程序之間的數(shù)據(jù)共享,1、Preference,主要用于存儲數(shù)據(jù)較少的場合,比如配置信息 文件位置:/data/data/shared_prefs/*.xml 需要用到接口:SharedPreferences 及其內部接口:SharedPreferences.Editor 取得接口: Context.SharedPreferences(filename,mode) SharedPreferences.edit() 數(shù)據(jù)類型、數(shù)據(jù)操作: int、flaot、string、boolean等 putString(key,value) getString(key,defValue),實例:Test_of_Preference,定義 main.xml: 一個 EditText 用于鍵入數(shù)據(jù) 三個 Button 分別用于將數(shù)據(jù)寫入 xml 文件、從文件獲取數(shù)據(jù)并顯示、清除文件中的數(shù)據(jù) 定義 sharedpreferences.xml: 一個 TextView 用于顯示從文件中獲取的數(shù)據(jù),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)findViewById(R.id.textView1);,創(chuàng)建接口 sp1、spEditor1 以鍵值 方式加入數(shù)據(jù) 以 String Key 為索引來獲取數(shù)據(jù) 清除數(shù)據(jù),private SharedPreferences sp1; sp1 = this.getSharedPreferences(“test“, MODE_PRIVATE); SharedPreferences.Editor spEditor1 = sp1.edit();,spEditor1.putString(“TEXT“,et1.getText().toString(); spEmit();,String string1 = sp1.getString(“TEXT“, “); textView1.setText(string1);,spEditor1.clear().commit();,運行結果:,2、File,文件可用來存放大量數(shù)據(jù),如文本、圖片、音頻等 默認位置:/data/data/files/*.* java.io.* 庫提供 I/O 接口實現(xiàn)本地文件讀寫 文件輸入流的獲取 Context.openFileInput(String name) 文件輸出流的獲取 Context.openFileOutput(String name, int mode) 包內資源(res/raw/)文件的讀取 Resources.openRawResource(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.finish(); ,讀文件 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 null; ,editText2.setText(readFile();,運行結果: 注意:寫文件時用的 MODE_APPEND 模式,3、SQLite,SQLite 輕量級嵌入式數(shù)據(jù)庫引擎,面向資源有限的設備 沒有服務器進程 所有數(shù)據(jù)存放在同一文件中 跨平臺,可自由復制 SQLiteOpenHelper 幫助類,用于管理數(shù)據(jù)庫創(chuàng)建和版本更新 onCreate(SQLiteDatabase db) onUpgrade(db,int old_ver,int new_ver) sqlite3 :位于 /tools,實例:Test_of_SQLite,DatabaseHelper 繼承自 SQLiteOpenHelper 重寫了onCreate()、onUpgrade() 方法 實現(xiàn) insert、del、query、close 等方法 ListView 顯示記錄,字段對應成 TextView ListView 的 onItemClick() 實現(xiàn)刪除記錄 數(shù)據(jù)庫文件路徑: /data/data/database/*.db,private SQLiteDatabase db; public void insert(ContentValues values) SQLiteDatabase db = getWritableDatabase(); db.insert(TABLE_NAME, null, 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); Cursor cursor = dbHelper.query(); String from = “_id“,“name“,“url“,“notes“; int to = R.id.textView1,R.id.textView2,R.id.textView3, R.id.textView4; SimpleCursorAdapter scadapter = new SimpleCursorAdapter (this,R.layout.favoritelist,cursor,from,to); ListView listView = getListView(); listView.setAdapter(scadapter); AlertDialog.Builder adBuilder = new AlertDialog.Builder(this);,Query_Test.java,listView.onItemClick adBuilder.setMessage(“確認刪除?“) .setPositiveButton(“Y“, new OnClickListener() .setNegativeButton(“N“, new OnClickListener(); AlertDialog aleraDialog = adBuilder.create(); aleraDialog.show(); ,刪除記錄并重建 ListView,運行結果:,4、Content Provider,與 Activity、Service、Broadcast Receiver 同屬 Android 應用程序基本組件之一 用于保存和檢索數(shù)據(jù),實現(xiàn)了數(shù)據(jù)的跨應用共享 Android 中各應用均運行在自己的進程中,互相訪問的 Content Provider 接口統(tǒng)一定義在 vider 包內,涵蓋了常見的數(shù)據(jù)類型如音視頻、圖片、聯(lián)系人等 實現(xiàn)的方法包括: insert、delete、qurey、update 等,URI Content Provider 用以實現(xiàn)數(shù)據(jù)共享的對象 Content Resolver Content Provider 的客戶端接口 客戶端通過 getContentResolver() 獲得接口 提供與 Content Provider 對應的方法 間接地通過操作 Resolver 來操作 Provider 一個 Provider 可以對應多個 Resolver 用戶自定義系統(tǒng) :Content Provider,實例:Test_of_ContentProvider,系統(tǒng) Content Provider 添加聯(lián)系人:,String name = editText1.getText().toString(); String notes = editText2.getText().toString(); ContentResolver contentResolver1 = getContentResolver(); ContentValues contentValues1 = new ContentValues(); Uri uri1 = Contacts.People.CONTENT_URI; contentValues1.put(People.NAME,name); contentValues1.put(People.NOTES, notes); trycontentResolver1.insert(uri1, contentValues1);,運行結果:,7、Android 的多媒體應用,on android,Android 的多媒體,Android 實現(xiàn)了常見媒體格式的編解碼機制 圖片:jpeg、gif、png、bmp 音頻:3gp、mp3、wav 視頻:3gp、mp4 Android 提供相應 API 實現(xiàn)多媒體應用 音視頻播放:MediaPlayer、JetPlayer 音視頻錄制: MediaRecorder 媒體文件來源: 應用程序資源、本地文件、網(wǎng)絡文件流,實例:ImageView,實例:GridView、Gallery,詳見實例:Test_of_Widget,實例:Test_of_Mp3Player,實現(xiàn) MediaPlayer.OnCompletionListener 接口,public class Mp3Player_Test extends Activity implements MediaPlayer.OnCompletionListener / 重寫 onCompletion() 方法,實現(xiàn)循環(huán)播放 public void onCompletion(MediaPlayer mp) if(flag1=1)ib4play(); if(flag2=1)ib8play(); / 重寫 onDestroy() 方法 public void onDestroy() super.onDestroy(); if(imageButton2.isEnabled() ib2stop(); if(imageButton6.isEnabled() ib6stop(); ,定義 MediaPlayer 對象并初始化,private MediaPlayer mp1,mp2; private void mp1init() try mp1=MediaPlayer.create(this, R.raw.temp); mp1.setOnCompletionListener(this); catch (Throwable t)errorReport(t); private void mp2init() try mp2 = new MediaPlayer(); String path = “/sdcard/graduated.mp3“; mp2.setDataSource(path); mp2.prepare(); mp2.setOnCompletionListener(this); catch (Throwable t)errorReport(t); ,音樂文件路徑:,定義三個 ImageButton 對象并實例化,實現(xiàn) paly、pause 方法,private void ib4play() flag1=1; mediaPlayer1.start(); imageButton2.setEnabled(true); imageButton3.setEnabled(true); imageButton4.setEnabled(false); private void ib3pause

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論