Mobile phone attribution to search

Source: Internet
Author: User

First, the Knowledge reserve

1, the attribution of the search steps

1) Get user input phone number

2) Filter the user's error input

--Using regular expressions

3) Query database, feedback results

2, the characteristics of telephone numbers

Phone number features: 1 + (3, 4, 5, 6, 7, 8) + (9 digits)

3. Regular expressions

^: Start of string

$: End of string

[3-8]:3 to 8 of any

{9}: There are nine previous types

\d: Indicates a number

\d+: Number has one or more

1) ^1[3-8]\d{9}$:

The expression is a string, the first is 1, the second is any of 3 to 8, the string ends with nine digits

2)^\d+$

A string with 1 or more digits

3) Examples:

String str= "123456789"

Str.matches ("^\\d+$")

Note: when matching "\" Note escape is "\ \"

Second, start our cell phone attribution to inquire about it

1. Prepare the database.

I'm using Android Studio: Database Assets folder (new-->folder-->assets folders)

Note: 1. The database path must be a file in the Data/data directory or the database cannot be accessed
2, according to the number of the first seven-digit query

3, before querying the database needs to first call Copydb ();

Put the database in the specified directory: path= "data/data/com.example.mobile/files/address.db

Getfilesdir () ==data/data/com.example.mobile/files

Getassets () = = The path of the assets folder we created

    private void Copydb (String dbname) {        file DestFile = new File (Getfilesdir (), dbname);        If the database exists, no longer copies the        if (destfile.exists ()) {            return;        }        for the second time. FileOutputStream Out=null;        try {            InputStream in=getassets (). open (dbname);            Out=new FileOutputStream (destfile);            byte b[]=new byte[1024];            int len=0;            while ((Len=in.read (b))!=-1) {                out.write (B,0,len);            }            In.close ();            Out.close ();        } catch (IOException e) {            e.printstacktrace ();        }    }

  

2. code example

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:orientation=" vertical "android:layout_width=" match_parent "    android:layout_height=" Match_parent ">    <textview        style=" @style/titlestyle "        android:text=" Attribution query "/>    < EditText        android:id= "@+id/et_phone"        android:inputtype= "phone"        android:layout_width= "Match_parent"        android:layout_height= "wrap_content"/>    <button        android:onclick= "Query"        android: Layout_width= "Wrap_content"        android:layout_height= "wrap_content"        android:text= "Query"/>    < TextView        android:id= "@+id/tv_result"        android:layout_marginleft= "5DP"        android:layout_width= "wrap _content "        android:layout_height=" wrap_content "        android:text=" query result "        android:textsize=" 18sp "/> </LinearLayout>

  

Package Com.example.mobile.activity;import Android.app.activity;import Android.database.cursor;import Android.database.sqlite.sqlitedatabase;import Android.os.bundle;import Android.text.textutils;import Android.view.view;import Android.widget.edittext;import Android.widget.textview;import Com.example.mobile.R; public class Addressactivity extends activity{private static final String path= "Data/data/com.example.mobile/files/add    Ress.db ";    Private EditText Etnumber;    Private TextView Tvresult;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_address);        Etnumber= (EditText) Findviewbyid (R.id.et_phone);    tvresult= (TextView) Findviewbyid (R.id.tv_result); }/** * Type: Button listener * function: Get phone attribution */public void query (view view) {String number=etnumber.gettext (). To        String (); if (! Textutils.isempty (number)) {String address= getaddress (number);        Tvresult.settext (address);        }}//Query The phone's attribution to public static string GetAddress (string number) {Sqlitedatabase database=null;        String address= "Unknown number"; Filter out queries that are not phone numbers (11-bit) if (number.matches ("^1[3-8]\\d{9}$")) {//Query the phone's attribution database=sqlitedatabase.            OpenDatabase (path,null,sqlitedatabase.open_readonly);                    Cursor cursor=database.rawquery ("Select location from Data2 where id= (select Outkey from Data1 where id=?)",            New String[]{number.substring (0,7)});            if (Cursor.movetonext ()) {address=cursor.getstring (0);        } cursor.close ();                    }else if (number.matches ("^\\d+$")) {//Match Special phone number switch (Number.length ()) {Case 3:                    address= "Alarm Telephone";                Break                    Case 4:address= "simulator";                Break                   Case 5:address= "Customer service Phone"; Break                    Case 7:case 8:address= "local phone";                Break            Default:break;        }} if (database! = null) database.close ();    return address; }}

  

  

Mobile phone attribution to search

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.