【安卓筆記】單機版手機歸屬地查詢

來源:互聯網
上載者:User

標籤:android   style   blog   http   java   使用   

既然是單機版,那麼必然是查詢本機資料庫了,所以我們得準備一個離線資料庫檔案(:http://download.csdn.net/detail/rowandjj/7660979).步驟: 1.建立一個工具類開啟資料庫:
package cn.edu.chd.mobilesafe.db.dao;import android.database.sqlite.SQLiteDatabase;public class AddressDao{public static SQLiteDatabase getAddressDB(String path){return SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);}}

2.編寫業務方法:首先需要使用Regex判斷號碼是手機號碼還是固定電話。若是手機號碼則根據前面7位查詢資料庫。若是固定電話,則先要判斷固定電話的長度,分下面幾種情況:3位區號+7位號碼3位區號+8位號碼4位區號+7位號碼4位區號+8位號碼根據區號即可查出歸屬地
package cn.edu.chd.mobilesafe.engine;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.os.Environment;import cn.edu.chd.mobilesafe.db.dao.AddressDao;public class AddressService{public static String getAddress(String number){String city = number;SQLiteDatabase db = AddressDao.getAddressDB(Environment.getExternalStorageDirectory().getPath()+"/address.db");if(number.matches("^1[3458]\\d{9}$"))//手機號{if(db.isOpen()){Cursor cursor = db.rawQuery("select city from info where mobileprefix = ?", new String[]{number.substring(0, 7)});if(cursor.moveToNext()){city = cursor.getString(0);}}db.close();}else//固定電話{int len = number.length();switch (len){case 4:city = "模擬器";break;case 7:case 8:city = "本地號碼";break;case 10://3位區號+7位號碼if(db.isOpen()){Cursor cursor = db.rawQuery("select city from info where area = ? limit 1", new String[]{number.substring(0, 3)});if(cursor.moveToNext()){city = cursor.getString(0);}db.close();}break;case 11://3位區號+8位號碼,4位區號+7位號碼if(db.isOpen()){Cursor cursor = db.rawQuery("select city from info where area = ? limit 1", new String[]{number.substring(0, 3)});if(cursor.moveToNext()){city = cursor.getString(0);}cursor = db.rawQuery("select city from info where area = ? limit 1", new String[]{number.substring(0, 4)});if(cursor.moveToNext()){city = cursor.getString(0);}db.close();}break;case 12:if(db.isOpen()){Cursor cursor = db.rawQuery("select city from info where area = ? limit 1", new String[]{number.substring(0, 4)});if(cursor.moveToNext()){city = cursor.getString(0);}db.close();}break;}}if(db.isOpen()){db.close();}return city;}}

3.調用業務類,查詢手機歸屬地資訊: 由於是資料庫操作,所以使用了AsyncTask進行非同步查詢。
package cn.edu.chd.mobilesafe.ui;import android.app.Activity;import android.os.AsyncTask;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.animation.Animation;import android.view.animation.AnimationUtils;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;import cn.edu.chd.mobilesafe.R;import cn.edu.chd.mobilesafe.engine.AddressService;public class QueryNumberActivity extends Activity{private Button but_query = null;private EditText et_number = null;private TextView tv_show = null;@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.query_number);but_query = (Button) findViewById(R.id.but_query_p);et_number = (EditText) findViewById(R.id.et_query_p);tv_show = (TextView) findViewById(R.id.tv_show_p);but_query.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){String text = et_number.getText().toString();if(text.trim().equals("")){Toast.makeText(QueryNumberActivity.this,"號碼不可為空",0).show();}else{//非同步查詢資料庫,獲得歸屬地資訊顯示到txetview上new QueryNumberTask().execute(text);}}});}public class QueryNumberTask extends AsyncTask<String, Void, String>{@Overrideprotected String doInBackground(String... params){String number = params[0];//查詢資料庫,擷取歸屬地資訊return AddressService.getAddress(number);}@Overrideprotected void onPostExecute(String result){tv_show.setText(result);}}}

效果:





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.