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