玩下軟工項目第一輪--全域Context的擷取,SQLite的建立與增刪改查,讀取使用者通話記錄資訊(隨筆沒寫完)

來源:互聯網
上載者:User

標籤:tap   color   軟體   api   ring   sel   一個   .exe   move   

項目的Github地址:

採用基於git的多人協作開發模式

軟體採用mvc設計模式,前端這麼藝術的事我不太懂,交給斌豪同學去頭疼了。第一輪先實現查詢通話記錄返回對應號碼(親情帳號?pm的需求分析還沒出來,還不是很清楚具體操作)上一次的通話時間。

API介面:

 

 

 

那麼我寫的功能就要脫離activity了。那麼首先要做的就是擷取全域Context。

實現的話挺簡單的,就是要先理解Context的含義,我是通過看http://www.jianshu.com/p/94e0f9ab3f1d(Context都沒弄明白,還怎麼做Android開發?)這篇簡書理解的。不是主要玩安卓開發的,碰到安卓這些特性還是挺頭疼的。實現方法就是寫一個Application的子類,然後修改AndroidMainifest.xml。公式化的方法了。實現後就可以通過調用,MyApplication.getContext()擷取全域Context了,具體代碼如下:

 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"//在<application></application>中插入

然後就是建立資料庫了,用安卓內建的SQLite。講道理,一般用Litepal來操縱資料庫會簡單很多,但話又說回來了,我又不是主玩安卓開發的,學習這東西還是從底層開始比較容易懂,所以這裡用SQLiteDatabase來操控資料庫。

建立資料庫:公式化的方法,兩個方法是重寫SQLiteOpenHelper裡的方法,一個用在建立整個資料庫的時候,一個用在給資料庫升級的時候(比如插入新表)。

 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     @Override13     public void onCreate(SQLiteDatabase sqLiteDatabase) {14         sqLiteDatabase.execSQL(CREATE_KITH_AND_KIN);15     }16 17     @Override18     public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {19 20     }21 }

 增刪改查:

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});    }}

 然後是讀取使用者的通話記錄

首先要許可權

<uses-permission android:name="android.permission.READ_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.TYPE11         };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 }

 資料處理:

玩下軟工項目第一輪--全域Context的擷取,SQLite的建立與增刪改查,讀取使用者通話記錄資訊(隨筆沒寫完)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.