玩下软工项目,第一轮--全局Context的获取,SQLite的建立与增删改查,读取用户通话记录信息
项目的Github地址:https://github.com/ggrcwxh/LastTime
采用基于git的多人协作开发模式
软件采用mvc设计模式,前端这么艺术的事我不太懂,交给斌豪同学去头疼了。第一轮先实现查询通话记录返回对应号码上一次的通话时间。
真机测试下的效果图:显示称呼和上一次通话距离现在的时间,单位小时
API接口:
首先需要创建一个LastTimeDatabaseHelper的对象来创建数据库,建议创建在mainActivity下,创建建议格式如下,传入参数为全局Context;数据库名;允许返回自定义的Cursor,一般填null,当前数据库版本号
public LastTimeDatabaseHelper dbHelper= new LastTimeDatabaseHelper(MyApplication.getContext(),"lasttime.db",null,1);
关于设置亲情号码:建议格式如下:传入参数分别为称呼,电话号码,以及dbHelper
KithAndKinService test =new KithAndKinService("mother","111111",dbHelper); test.insertToDatabase();
关于删除亲情号码:建议格式如下:传入参数为称呼以及dbHelper
KithAndKinService test2 =new KithAndKinService("mother",dbHelper);
test2.deleteInDatabase()
关于更新数据库通话记录时间,建议格式如下:
CallInfoService call = new CallInfoService(dbHelper); call.getCallInfos(); call.dataProcessing();
关于查看上一次通话记录时间,建议格式如下,l1为上次通话时间-1970年1月1日 的时长的毫秒数,请在activity上获取当前时间再减去l1,建议获取方法System.currentTimeMillis()
KithAndKinService test3 =new KithAndKinService("mother",dbHelper); Map<String,String> map1 = test3.seleteInDatabase(); long l1 = Long.valueOf(map1.get("date"));
实现方法:
因为我不写activity,那么我写的功能就要脱离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 @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 }
增删改查:主要是采用SQLiteDatabase提供的insert()等方法
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 KithAndKinService(LastTimeDatabaseHelper dbHelper)
{
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 Map<String,String> seleteInDatabase(){
SQLiteDatabase db = dbHelper.getWritableDatabase();
String temp = "call=?";
String[] temp2 ={call};
String[] temp3 ={"call","num","date"};
Cursor cursor = db.query("KITH_AND_KIN",temp3,temp,temp2,null,null,null);
Map<String,String> map = new HashMap<String,String>();
if (cursor != null && cursor.moveToFirst())
{
map.put("call",cursor.getString(0));
map.put("num",cursor.getString(1));
map.put("date", String.valueOf(cursor.getLong(2)));
}
return map;
}
public List<Map<String,String>> seleteInDatabaseAll(){
SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor cursor = db.query("KITH_AND_KIN",null,null,null,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" />
然后就是读取通话记录了:主要是读取用resolver.query读取通话记录的数据库,然后更新数据库
public class CallInfoService {
private List<CallInfo> callinfos = new ArrayList<CallInfo>();
private LastTimeDatabaseHelper dbHelper;
public List<CallInfo> getCallinfos() {
return callinfos;
}
public CallInfoService(LastTimeDatabaseHelper dbHelper){
this.dbHelper=dbHelper;
}
public void getCallInfos() {
ContentResolver resolver = MyApplication.getContext().getContentResolver();
Uri uri = CallLog.Calls.CONTENT_URI;
String[] projection = new String[]{
CallLog.Calls.NUMBER,
CallLog.Calls.DATE,
CallLog.Calls.TYPE
};
if (ActivityCompat.checkSelfPermission(MyApplication.getContext(), Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {
return ;
}
Cursor cursor = resolver.query(uri, projection, null, null, null);
while (cursor.moveToNext()){
if(cursor.getInt(2)==2)//OUTGOING_TYPE=2
{
String number=cursor.getString(0);
long date = cursor.getLong(1);
boolean flag=false;
for(CallInfo attribute:callinfos)
{
if(attribute.getNumber().equals(number)) {
if(attribute.getDate()<date)
{
attribute.setDate(date);
flag=true;
break;
}
}
}
if(flag==false)
{
CallInfo temp = new CallInfo(number,date);
callinfos.add(temp);
}
}
}
cursor.close();
}
public void dataProcessing(){
KithAndKinService kaks = new KithAndKinService(dbHelper);
List<Map<String,String>> all= kaks.seleteInDatabaseAll();
for(CallInfo attribute : callinfos)
{
for(int i =0 ;i<all.size();i++)
{
if(attribute.getNumber().equals(all.get(i).get("num")))
{
KithAndKinService temp = new KithAndKinService(all.get(i).get("call"),all.get(i).get("num"),attribute.getDate(),dbHelper);
temp.updateToDatabase();
}
}
}
}
}
下一节预告:读取图片exif信息,获取拍摄日期经纬度,确定大致位置