Android之——經常使用手機號碼功能

來源:互聯網
上載者:User

標籤:tar   android   布局   順序   自己   相關   instance   dir   pre   

轉載請註明出處:http://blog.csdn.net/l1028386804/article/details/47374415

有些Android手機中會帶有一些經常使用號碼的功能,比方訂餐電話、公用電話、航空機票,等等諸如此類的服務,點擊對應的號碼會自己主動彈出撥打到電話的介面。

那以下我們就一起來實現一個手機經常使用號碼功能的小範例吧。

一、原理

首先。這些經常使用號碼放置在一個SQLite資料庫中,我們將這些號碼從資料庫中讀取出來。依照對應的順序顯示到Android系統提供的ExpandableListView中(有關ExpandableListView的具體使用方法請大家參考Android官方文檔,我在這裡就不多說了),然後給每一個條目設定點擊事件。在點擊事件中啟用撥打到電話的介面。並將對應的號碼傳遞到撥打到電話的介面。

好了,原理講完了,是不是非常easy呢?以下,我們就一起來實現這些詳細的功能吧。

二、實現1、準備資料庫

我們首先將資料庫放置在Android項目下的assets檔案夾

例如以:

2、建立資料的操作類CommonNumberService

建立資料操作類CommonNumberService,這個類中主要有4個方法,一個是構造方法,在構造方法中我們將Android內容物件傳遞到這個類中。其它3個方法實現了將資料庫複寫到/data/data/應用程式套件名/files檔案夾。然後實現資料的查詢操作。

1)構造方法的實現

在構造方法中。我們將內容物件傳遞到這個類中

詳細實現代碼例如以下:

private Context context;public CommonNumberService(Context context) {super();this.context = context;}

2)擷取經常使用號碼組資料方法

詳細代碼例如以下:

/** * 得到組資料 * @return */public List<Map<String, String>> getGroupData(){List<Map<String, String>> groupData = new ArrayList<Map<String,String>>();File file = new File(context.getFilesDir(), "commonnum.db");SQLiteDatabase db = SQLiteDatabase.openDatabase(file.getAbsolutePath(), null, SQLiteDatabase.OPEN_READONLY);if(db.isOpen()){Cursor c = db.query("classlist", new String[]{"name", "idx"}, null, null, null, null, null);while(c.moveToNext()){Map<String, String> map = new HashMap<String, String>();String name = c.getString(c.getColumnIndex("name"));String idx = c.getString(c.getColumnIndex("idx"));map.put("name", name);map.put("idx", idx);groupData.add(map);}c.close();db.close();}return groupData;} 

3)擷取經常使用號碼子條目資料

詳細代碼例如以下:

/** * 得到子條目的資料 * @return */public List<List<Map<String, String>>> getChildData(){List<List<Map<String, String>>> childData = new ArrayList<List<Map<String,String>>>();List<Map<String, String>> groupData =  this.getGroupData();File file = new File(context.getFilesDir(), "commonnum.db");SQLiteDatabase db = SQLiteDatabase.openDatabase(file.getAbsolutePath(), null, SQLiteDatabase.OPEN_READONLY);if(db.isOpen()){for(int i = 0; i < groupData.size(); i++){String idx = groupData.get(i).get("idx");List<Map<String, String>> list = new ArrayList<Map<String,String>>();Cursor c = db.query("table" + idx, new String[]{"_id", "number", "name"}, null, null, null, null, null);while(c.moveToNext()){Map<String, String> map = new HashMap<String, String>();String name = c.getString(c.getColumnIndex("name"));String number = c.getString(c.getColumnIndex("number"));map.put("name", name);map.put("number", number);list.add(map);}c.close();childData.add(list);}db.close();}return childData;}

4)總體代碼

詳細實現代碼例如以下:

package cn.lyz.mobilesafe.engine;import java.io.File;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;/** * 擷取經常使用號碼 * @author liuyazhuang * */public class CommonNumberService {private Context context;public CommonNumberService(Context context) {super();this.context = context;}/** * 將資料類複製到files檔案夾 */public void copyDbToFilesDir(){//將資料庫複寫到files檔案夾File file = new File(context.getFilesDir(), "commonnum.db");if(!file.exists()){try {InputStream in = context.getAssets().open("commonnum.db");OutputStream out = new FileOutputStream(file);byte[] buffer = new byte[1024];int len = 0;while((len = in.read(buffer)) != -1){out.write(buffer, 0, len);}out.close();in.close();} catch (Exception e) {e.printStackTrace();}}}/** * 得到組資料 * @return */public List<Map<String, String>> getGroupData(){List<Map<String, String>> groupData = new ArrayList<Map<String,String>>();File file = new File(context.getFilesDir(), "commonnum.db");SQLiteDatabase db = SQLiteDatabase.openDatabase(file.getAbsolutePath(), null, SQLiteDatabase.OPEN_READONLY);if(db.isOpen()){Cursor c = db.query("classlist", new String[]{"name", "idx"}, null, null, null, null, null);while(c.moveToNext()){Map<String, String> map = new HashMap<String, String>();String name = c.getString(c.getColumnIndex("name"));String idx = c.getString(c.getColumnIndex("idx"));map.put("name", name);map.put("idx", idx);groupData.add(map);}c.close();db.close();}return groupData;} /** * 得到子條目的資料 * @return */public List<List<Map<String, String>>> getChildData(){List<List<Map<String, String>>> childData = new ArrayList<List<Map<String,String>>>();List<Map<String, String>> groupData =  this.getGroupData();File file = new File(context.getFilesDir(), "commonnum.db");SQLiteDatabase db = SQLiteDatabase.openDatabase(file.getAbsolutePath(), null, SQLiteDatabase.OPEN_READONLY);if(db.isOpen()){for(int i = 0; i < groupData.size(); i++){String idx = groupData.get(i).get("idx");List<Map<String, String>> list = new ArrayList<Map<String,String>>();Cursor c = db.query("table" + idx, new String[]{"_id", "number", "name"}, null, null, null, null, null);while(c.moveToNext()){Map<String, String> map = new HashMap<String, String>();String name = c.getString(c.getColumnIndex("name"));String number = c.getString(c.getColumnIndex("number"));map.put("name", name);map.put("number", number);list.add(map);}c.close();childData.add(list);}db.close();}return childData;}}

2、介面顯示類CommonNumberActivity

在這個類中我們主要實現了。擷取介面控制項,然後調用CommonNumberService類中方法,給介面封裝資料,同一時候設定ExpandableListView子條目的點擊事件,啟用撥打到電話接介面,並將選擇的電話號碼傳遞到撥打到電話介面。


詳細實現代碼例如以下:

package cn.lyz.mobilesafe.activity;import java.io.File;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import java.util.List;import java.util.Map;import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.View;import android.widget.ExpandableListView;import android.widget.ExpandableListView.OnChildClickListener;import android.widget.SimpleExpandableListAdapter;import cn.lyz.mobilesafe.R;import cn.lyz.mobilesafe.engine.CommonNumberService;/** * 經常使用號碼 * @author liuyazhuang * */public class CommonNumberActivity extends Activity {private ExpandableListView elv_common_number;private CommonNumberService service;private SimpleExpandableListAdapter adapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.common_number);elv_common_number = (ExpandableListView) findViewById(R.id.elv_common_number);this.service = new CommonNumberService(this);//將資料庫複寫到files檔案夾service.copyDbToFilesDir();//擷取組資料List<Map<String, String>> groupData = service.getGroupData();//擷取子條目資料List<List<Map<String, String>>> childData = service.getChildData();adapter = new SimpleExpandableListAdapter(this, groupData, android.R.layout.simple_expandable_list_item_1, new String[]{"name"}, new int[]{android.R.id.text1}, childData, android.R.layout.simple_expandable_list_item_2, new String[]{"name", "number"}, new int[]{android.R.id.text1, android.R.id.text2});elv_common_number.setAdapter(adapter);elv_common_number.setOnChildClickListener(new MyOnChildClickListener());}/** * 點擊事件 * @author liuyazhuang * */private class MyOnChildClickListener implements OnChildClickListener{@Overridepublic boolean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition, long id) {Map<String, String> map = (Map<String, String>) adapter.getChild(groupPosition, childPosition);String number = map.get("number");Intent intent = new Intent();intent.setAction(Intent.ACTION_DIAL);intent.setData(Uri.parse("tel:"+number));startActivity(intent);return false;}}}

3、介面布局common_number.xml

這個布局非常easy,就是放置了一個ExpandableListView

詳細實現例如以下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <ExpandableListView        android:id="@+id/elv_common_number"        android:layout_width="match_parent"        android:layout_height="match_parent" /></LinearLayout>

三、執行效果


四、溫馨提示

大家能夠到連結http://download.csdn.net/detail/l1028386804/8980263下載執行個體中用到的資料庫commonnum.db檔案

本執行個體中,為了方面,我把一些文字直接寫在了布局檔案裡和相關的類中,大家在真實的項目中要把這些文字寫在string.xml檔案裡,在外部參考這些資源。切記,這是作為一個Android程式猿最主要的開發常識和規範,我在這裡僅僅是為了方便直接寫在了類和布局檔案裡。

Android之——經常使用手機號碼功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.