Play the first round of the soft project-the global context of the acquisition, the establishment and deletion of SQLite, read the user call record information (essay not finished)

Source: Internet
Author: User
Tags sqlite using git



GitHub address of the project:



Using Git-based multi-person collaborative development model



The software uses MVC design pattern, the front-end so the art thing I do not quite understand, gives the bin Hao schoolmate to have a headache. First round to realize the query call record return the corresponding number (affection account? PM Demand analysis has not come out, not very clear specific operation) Last call time.



API interface:












Then I'm going to write the function out of the activity. So the first thing to do is to get the global context.



Implementation is very simple, is to first understand the meaning of the context, I am by looking at http://www.jianshu.com/p/94e0f9ab3f1d (context are not understand, how to do Android development?) This simple book is understandable. Not primarily for Android, but it's a headache to bump into the features of Android. The implementation method is to write a application subclass, and then modify the Androidmainifest.xml. Formulated method. After implementation can be called, myapplication.getcontext () to get the global context, the specific code is as follows:


 
 
 1 public class MyApplication extends Application {
 2     private static Context context;
 3     @Override
 4     public void onCreate(){
 5         context=getApplicationContext();
 6     }
 7     public static Context getContext() {
 8         return context;
 9     }
10 }
1 android:name= ". MyApplication "//Insert in <application></application>


Then the database is built, using the built-in SQLite of Android. Reasonable, generally use Litepal to manipulate the database will be a lot simpler, but then again, I am not the main play Android development, learning this thing or from the bottom start easier to understand, so here with Sqlitedatabase to manipulate the database.



Set up a database: A formulaic approach, two ways to rewrite the Sqliteopenhelper method, one used when creating an entire database, a time to upgrade the database (such as inserting a new table).


 
 1 public class LastTimeDatabaseHelper extends SQLiteOpenHelper{
 2     public static final String CREATE_KITH_AND_KIN = "create table KITH_AND_KIN ("
 3             + "call text primary key, "
 4             +"num text,"
 5             +"date integer)";
 6 
 7     public LastTimeDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
 8         super(context,name,factory,version);
 9 
10     }
11 
12     @Override
13     public void onCreate(SQLiteDatabase sqLiteDatabase) {
14         sqLiteDatabase.execSQL(CREATE_KITH_AND_KIN);
15     }
16 
17     @Override
18     public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
19 
20     }
21 }


Additions and deletions to check:


 
public class KithAndKinService {
    private String call;
    private String num;
    private long date;
    private LastTimeDatabaseHelper dbHelper;
    public KithAndKinService(String call,String num,long date,LastTimeDatabaseHelper dbHelper) {
        this.call=call;
        this.num=num;
        this.date=date;
        this.dbHelper=dbHelper;
    }
    public KithAndKinService(String call, String num, LastTimeDatabaseHelper dbHelper) {
        this.call=call;
        this.num=num;
        this.dbHelper=dbHelper;
    }
    public KithAndKinService(String call,LastTimeDatabaseHelper dbHelper)
    {
        this.call=call;
        this.dbHelper=dbHelper;
    }
    public KithAndKinService(String call,long date,LastTimeDatabaseHelper dbHelper){
        this.call=call;
        this.date=date;
        this.dbHelper=dbHelper;
    }
    public void insertToDatabase(){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("call",call);
        values.put("num",num);
        if(date!=0)
        {
            values.put("date",date);
        }

        db.insert("KITH_AND_KIN",null,values);
    }
    public void deleteInDatabase(){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.delete("KITH_AND_KIN","call=?",new String[] {call});
    }
    public List<Map<String,String>> seleteInDatabase(){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        String temp = "call=?";
        String[] temp2 ={call};
        Cursor cursor = db.query("KITH_AND_KIN",null,temp,temp2,null,null,null);
        List<Map<String,String>> list= new ArrayList<Map<String,String>>();

        if (cursor != null && cursor.moveToFirst()) {
            do {
                Map<String,String> map = new HashMap<String,String>();
                map.put("call",cursor.getString(cursor.getColumnIndex("call")));
                map.put("num",cursor.getString(cursor.getColumnIndex("num")));
                map.put("date",cursor.getString(cursor.getColumnIndex("date")));
                list.add(map);


            } while (cursor.moveToNext());

        }

        return  list;
    }
    public void updateToDatabase(){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("date",date);
        db.update("KITH_AND_KIN",values,"call=?",new String[] {call});
    }

}


Then it reads the user's call log.



First, you need permission


<uses-permission android:name= "Android.permission.READ_CALL_LOG"/>


Then read the call log:


 
 1 public class CallInfoService {
 2     private List<CallInfo> callInfos = new ArrayList<CallInfo>();
 3 
 4     public List<CallInfo> getCallInfos() {
 5         ContentResolver resolver = MyApplication.getContext().getContentResolver();
 6         Uri uri = CallLog.Calls.CONTENT_URI;
 7         String[] projection = new String[]{
 8                 CallLog.Calls.NUMBER,
 9                 CallLog.Calls.DATE,
10                 CallLog.Calls.TYPE
11         };
12         if (ActivityCompat.checkSelfPermission(MyApplication.getContext(), Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {
13              List<CallInfo> temp =new ArrayList<CallInfo>();
14              temp.add(new CallInfo("0",0,0));
15              return temp;
16         }
17         Cursor cursor = resolver.query(uri, projection, null, null, null);
18         while (cursor.moveToNext()){
19             String number = cursor.getString(0);
20             long date = cursor.getLong(1);
21             int type = cursor.getInt(2);
22             callInfos.add(new CallInfo(number, date, type));
23         }
24             cursor.close();
25             return callInfos ;
26 
27     }
28 }


Data processing:



Play the first round of the soft project-the global context of the acquisition, the establishment and deletion of SQLite, read the user call record information (essay not finished)


Related Article

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.