android开发实战之做手机号和邮编查询小程序

简介:


我在前年做了个查询手机号和邮编地理位置的小程序,已经放到mm和天翼空间上卖了,应这次CU征文我就介绍一下这个项目的详细内幕。

首先最终上效果图

手机号区号归属

 

程序代码结构

手机号区号归属2

1.开发准备

1)开发过程首先从网络上找手机号数据库,我找到的是mdb(微软的access),越新越好

2)android的数据库是sqlite,因此我们必须在电脑上创建好数据库,导入到程序里,到sqlite.org上下载windows版的sqlite3,创建好数据库mobile.db:

全国的城市列表名称,使用id作为唯一标识,可以减少数据库大小,因为android程序中文件大小不能超过1MB

点击(此处)折叠或打开

  1. CREATE TABLE city(id int,name text);

手机号区号归属4

--运营商列表

 

  1. CREATE TABLE isp(id int,name text);

手机号区号归属3

---所有城市的邮政编码

 

  1. CREATE TABLE code_city(code text,city_id integer);

手机号区号归属5

--所有手机号段对应的运营商和城市列表,这个表公有44400条数据,若是把isp和city的id作为两个字段的话,会超过1MB,我把isp和city的id进行合并成一个字段(最高位为ispid(isp只有3个),后三位为city(357个)的id)能够省下200kb的大小,最终整个库为806KB

 

  1. CREATE TABLE mobile (start_num integer, end_num integer, isp_city_id integer);--所有手机号段对应的运营商和城市列表

2.开始开发

新建工程,我选择的是1.6,只是两年前的最新版本,我目前也懒得更新了

手机号区号归属6

2.1 AndroidManifest.xml 这个是android里最重要的文件,这个文件类似于ssh里的applicationContext.xml之类的定义了活动和权限等等信息,我基本上没有改这个文件,因为是单机的不需要调用网络蓝牙和文件写入之类的权限

 

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.       package="yifangyou.mobile_query"
  4.       android:versionCode="1"
  5.       android:versionName="1.0">
  6. <application android:icon="@drawable/icon" android:label="@string/app_name">
  7. <activity android:label="@string/app_name" android:name=".mobile_query">
  8. <intent-filter>
  9. <action android:name="android.intent.action.MAIN" />
  10. <category android:name="android.intent.category.LAUNCHER" />
  11. </intent-filter>
  12. </activity>
  13.  
  14. </application>
  15. <uses-sdk android:minSdkVersion="2" />
  16.  
  17. </manifest>

2.2,在res目录下建立raw目录把mobile.db放入

2.3,在res/values/strings.xml是保存程序中用到的中文字,代码里最好不要写中文,便于实现多语言版本

 

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string name="app_name">手机号区号归属</string>
  4. <string name="searching_txt">请稍后,查询中...</string>
  5. <string name="search_complete_txt">查询完毕</string>
  6. <string name="search_txt">查询</string>
  7. <string name="result_txt">查询结果</string>
  8. <string name="enter_txt">手机号或者区号</string>
  9. <string name="id_empty_txt">手机号或者区号为空!</string>
  10. <string name="id_error_txt">手机号或者区号错误</string>
  11. <string name="enter_again_txt">请输入手机号或者区号</string>
  12. <string name="mobile_error_txt">手机号必须是11位的数字串</string>
  13. <string name="telphone_error_txt">区号必须是大于等于3位的数字串</string>
  14. <string name="location_txt">所属地区:</string>
  15. <string name="unknown_txt">未知</string>
  16. <string name="about_line_txt">开发者:yifangyou\n发布时间:2010年07月\nCopyright?2010 yifangyou\n</string>
  17. <string name="help_line_txt">本程序实现查询手机号或者区号对应的地理位置信息,精确到省市和运营商,信息库2010年07月01日整理完成,共18万条数据,包含最新的152、186、188、189号码段的归属地数据。\n使用方法:在输入框内输入11位手机号或者3、4位区号,点击搜索即可</string>
  18. <string name="about_txt">关于</string>
  19. <string name="exit_txt">退出</string>
  20. <string name="help_txt">帮助</string>
  21. <string name="exit_info_txt">已经退出,谢谢使用</string>
  22. <string name="clean_txt">清空</string>
  23. <string name="ok_txt">确定</string>
  24. </resources>

2.4建立菜单,如下面的@+id/help表示菜单项的id,在程序代码中需要响应这个菜单时用到

 

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <menu xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:id="@+id/help"
  4.         android:alphabeticShortcut="h"
  5.         android:title="帮助" />
  6. <item android:id="@+id/about"
  7.         android:alphabeticShortcut="a"
  8.         android:title="关于" />
  9. <item android:id="@+id/exit"
  10.         android:alphabeticShortcut="e"
  11.         android:title="退出" />
  12. </menu>

2.4页面布局,我这个程序界面比较简单用线性竖向布局

main.xml:

 

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical"
  4.     android:layout_;fill_parent"
  5.     android:layout_height="fill_parent"
  6. >
  7. <LinearLayout
  8.     android:orientation="horizontal"
  9.     android:layout_;fill_parent"
  10.     android:layout_height="wrap_content"
  11. >
  12. <TextView
  13.          android:layout_;wrap_content"
  14.          android:layout_height="wrap_content"
  15.          android:text="@string/enter_txt"
  16. />
  17. <EditText android:id="@+id/search"
  18.          android:layout_;wrap_content"
  19.          android:layout_height="wrap_content"
  20.          android:text=""
  21.          android:layout_weight="1.0"
  22. />
  23. <Button android:id="@+id/submit"
  24.         android:layout_;wrap_content"
  25.         android:layout_height="wrap_content"
  26.         android:layout_alignParentRight="true"
  27.         android:text="@string/search_txt">
  28. </Button>
  29. </LinearLayout>
  30. <TextView
  31.          android:layout_;fill_parent"
  32.          android:layout_height="wrap_content"
  33.          android:textColor="#99FF0000"
  34.          android:text="@string/result_txt"
  35. />
  36. <TextView android:id="@+id/result"
  37.          android:layout_;fill_parent"
  38.          android:layout_height="wrap_content"
  39.          android:textColor="#9900FF00"
  40.          android:text=""
  41. />
  42. <Button android:id="@+id/clean"
  43.         android:layout_;wrap_content"
  44.         android:layout_height="wrap_content"
  45.         android:layout_alignParentRight="true"
  46.         android:text="@string/clean_txt"/>
  47. </LinearLayout>

2.5主程序

1)首先对各个按钮进行设置监听事件,里面的R.id.xxx是对应main.xml

 

  1. public void onCreate(Bundle savedInstanceState) {
  2. super.onCreate(savedInstanceState);
  3.         setContentView(R.layout.main);//加载布局文件main.xml
  4.         dbm = new DBManager(this);//这是初始化数据库操作类
  5.         search_et = (EditText) findViewById(R.id.search);
  6.         result_et = (TextView) findViewById(R.id.result);
  7. Button btn = (Button) findViewById(R.id.submit);//定义查询按钮的事件
  8.         btn.setOnClickListener(new OnClickListener() {
  9. public void onClick(View v) {
  10.                 result_et.setText(getString(R.string.searching_txt));    //读取输入框的数据库
  11. search();
  12. }
  13. });
  14. Button clean_btn=(Button)findViewById(R.id.clean);
  15.         clean_btn.setOnClickListener(new OnClickListener(){
  16. public void onClick(View v){
  17.                 search_et.setText("");
  18. }
  19. });
  20.         isp_names=dbm.get_isp_name();
  21.         city_names=dbm.get_city_name();
  22. }

菜单显示菜单和设置菜单事件

  1. public boolean onCreateOptionsMenu(Menu menu) {
  2. super.onCreateOptionsMenu(menu);
  3. try {
  4.             MenuInflater inflater = getMenuInflater();
  5. inflater.inflate(R.menu.index, menu);
  6. } catch (Exception e) {
  7.  
  8. }
  9. return true;
  10. }
  11.  
  12. public boolean onOptionsItemSelected(MenuItem item) {
  13. super.onOptionsItemSelected(item);
  14. int id = item.getItemId();
  15. switch (id) {
  16. case R.id.help:
  17.             showHelp();
  18. break;
  19. case R.id.about:
  20.             showAbout();
  21. break;
  22. default:
  23.             alert(getString(R.string.app_name)
  24. + getString(R.string.exit_info_txt));
  25. finish();
  26. break;
  27. }
  28. return true;
  29. }

剩下的就是查询数据库了

注意由于我使用的库是放在raw下,android无法把这个文件作为数据库(不知道为什么),我的解决是把这个文件写到android的数据库库能够读到的地方(一般是程序的安装目录下的files下即可)去

 

  1. public DBManager(Context context) {
  2. this.context = context;
  3. try {
  4.             dbpath = context.getPackageManager().getApplicationInfo(
  5. context.getPackageName(), 0).dataDir
  6. + "/files/" + DB_NAME;
  7.             initDB();
  8.             db = SQLiteDatabase.openOrCreateDatabase(dbpath, null);
  9. } catch (NameNotFoundException e) {
  10.             e.printStackTrace();
  11. }
  12. }
  13.  
  14. public boolean initDB() {
  15. java.io.File f = new java.io.File(dbpath);
  16. if (f.exists()) {
  17. return true;
  18. }
  19. FileOutputStream stream;
  20. try {
  21. stream = context.openFileOutput(DB_NAME, Context.MODE_PRIVATE);
  22.             fp = context.getResources().openRawResource(R.raw.mobile);
  23. try {
  24. int size = fp.available();
  25. byte[] buffer = new byte[size];
  26.                 fp.read(buffer);
  27. stream.write(buffer);
  28.                 fp.close();
  29. stream.close();
  30. } catch (IOException e1) {
  31.                 e1.printStackTrace();
  32. return false;
  33. }
  34. } catch (FileNotFoundException e) {
  35. // TODO Auto-generated catch block
  36.             e.printStackTrace();
  37. }
  38. return false;
  39. }


     本文转自yifangyou 51CTO博客,原文链接:http://blog.51cto.com/yifangyou/1032046 ,如需转载请自行联系原作者

相关文章
|
3天前
|
Linux 编译器 Android开发
FFmpeg开发笔记(九)Linux交叉编译Android的x265库
在Linux环境下,本文指导如何交叉编译x265的so库以适应Android。首先,需安装cmake和下载android-ndk-r21e。接着,下载x265源码,修改crosscompile.cmake的编译器设置。配置x265源码,使用指定的NDK路径,并在配置界面修改相关选项。随后,修改编译规则,编译并安装x265,调整pc描述文件并更新PKG_CONFIG_PATH。最后,修改FFmpeg配置脚本启用x265支持,编译安装FFmpeg,将生成的so文件导入Android工程,调整gradle配置以确保顺利运行。
24 1
FFmpeg开发笔记(九)Linux交叉编译Android的x265库
|
26天前
|
Java Android开发
Android 开发获取通知栏权限时会出现两个应用图标
Android 开发获取通知栏权限时会出现两个应用图标
12 0
|
17天前
|
XML 开发工具 Android开发
构建高效的安卓应用:使用Jetpack Compose优化UI开发
【4月更文挑战第7天】 随着Android开发不断进化,开发者面临着提高应用性能与简化UI构建流程的双重挑战。本文将探讨如何使用Jetpack Compose这一现代UI工具包来优化安卓应用的开发流程,并提升用户界面的流畅性与一致性。通过介绍Jetpack Compose的核心概念、与传统方法的区别以及实际集成步骤,我们旨在提供一种高效且可靠的解决方案,以帮助开发者构建响应迅速且用户体验优良的安卓应用。
|
20天前
|
监控 算法 Android开发
安卓应用开发:打造高效启动流程
【4月更文挑战第5天】 在移动应用的世界中,用户的第一印象至关重要。特别是对于安卓应用而言,启动时间是用户体验的关键指标之一。本文将深入探讨如何优化安卓应用的启动流程,从而减少启动时间,提升用户满意度。我们将从分析应用启动流程的各个阶段入手,提出一系列实用的技术策略,包括代码层面的优化、资源加载的管理以及异步初始化等,帮助开发者构建快速响应的安卓应用。
|
20天前
|
Java Android开发
Android开发之使用OpenGL实现翻书动画
本文讲述了如何使用OpenGL实现更平滑、逼真的电子书翻页动画,以解决传统贝塞尔曲线方法存在的卡顿和阴影问题。作者分享了一个改造后的外国代码示例,提供了从前往后和从后往前的翻页效果动图。文章附带了`GlTurnActivity`的Java代码片段,展示如何加载和显示书籍图片。完整工程代码可在作者的GitHub找到:https://github.com/aqi00/note/tree/master/ExmOpenGL。
21 1
Android开发之使用OpenGL实现翻书动画
|
20天前
|
Android开发 开发者
Android开发之OpenGL的画笔工具GL10
这篇文章简述了OpenGL通过GL10进行三维图形绘制,强调颜色取值范围为0.0到1.0,背景和画笔颜色设置方法;介绍了三维坐标系及与之相关的旋转、平移和缩放操作;最后探讨了坐标矩阵变换,包括设置绘图区域、调整镜头参数和改变观测方位。示例代码展示了如何使用这些方法创建简单的三维立方体。
17 1
Android开发之OpenGL的画笔工具GL10
|
26天前
|
Android开发
Android开发小技巧:怎样在 textview 前面加上一个小图标。
Android开发小技巧:怎样在 textview 前面加上一个小图标。
12 0
|
26天前
|
Android开发
Android 开发 pickerview 自定义选择器
Android 开发 pickerview 自定义选择器
12 0
|
28天前
|
缓存 Java Android开发
安卓应用开发中的内存优化策略
在移动应用开发领域,性能一直是衡量应用质量的重要指标之一。特别是对于安卓平台,由于设备的硬件配置多样化,内存管理成为开发者面临的重大挑战。本文将深入探讨针对安卓平台的内存优化技巧,包括内存泄漏的预防、合理使用数据结构和算法、以及高效的资源释放机制。通过这些方法,开发者可以显著提升应用的性能和用户体验。
|
1月前
|
缓存 移动开发 Java
构建高效Android应用:内存优化实战指南
在移动开发领域,性能优化是提升用户体验的关键因素之一。特别是对于Android应用而言,由于设备和版本的多样性,内存管理成为开发者面临的一大挑战。本文将深入探讨Android内存优化的策略和技术,包括内存泄漏的诊断与解决、合理的数据结构选择、以及有效的资源释放机制。通过实际案例分析,我们旨在为开发者提供一套实用的内存优化工具和方法,以构建更加流畅和高效的Android应用。

热门文章

最新文章