Android to implement attribution query function

Source: Internet
Author: User

There are generally two ways to implement a attribution query, one is to query through the attribution API, and the other is to query the local database. Both methods have advantages and disadvantages, the best combination of use, I used this time is to query the database method. First, you need to download the attribution database callhomedb.db from the Internet. CSDN is available for download.

Once the database has been downloaded, a small software SQLite Expert Professional can be used to view the database, and the software supports operation of the SQL statement as well.

Once you have the database, you are ready to write code:

1. Copy the database files to the Android Studio project catalog assets.

2. Copy the database and copy the database under the assets directory to the project directory:

/** * Copy database */private void Copydb (final String dbname) {new T                     Hread () {public void run () {try {File file = new file (Getfilesdir (), dbname); if (File.exists () &&file.length () >0) {log.i ("virusscanactivity", "Database is                        Existence ");                    Return                    } InputStream is = Getassets (). open (dbname);                    FileOutputStream fos = openfileoutput (dbname,mode_private);                    byte[] buffer = new byte[1024];                    int len = 0;                    while ((Len=is.read (buffer))!=-1) {fos.write (Buffer,0,len);                    } is.close ();                Fos.close ();                }catch (Exception e) {e.printstacktrace ();    }}}.start (); }

3. Execute query statements against the database to get query results

    public static string Checkcallhome (String phonenumber) {        string desc = null;        Sqlitedatabase db = Sqlitedatabase.opendatabase ("Data/data/com.example.user.callhomedemo/files/callhomedb.db", null,sqlitedatabase.open_readonly);        cursor cursor = db.rawquery ("select * from mob_location where _id =?", new String[]{phonenumber.substring (0,7)});        if (Cursor.movetonext ()) {            desc = cursor.getstring (1);        }        Cursor.close ();        Db.close ();        return desc;    }

The demo image is as follows:

  

Android to implement attribution query function

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.