在Sqlite数据库中插入调用日志 [英] Inserting Call Logs in Sqlite Databse

查看:146
本文介绍了在Sqlite数据库中插入调用日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个程序,每次调用或发送短信时都会在数据库中插入详细信息。我应该使用广播接收器或内容观察者或服务吗?什么是合适的?我是android的新手,迫切需要帮助。





我现在已经完成了以下代码。这个问题是当第一次运行代码时,例如调用日志有8条记录,因此这8条记录被插入数据库中。然后,如果调用日志有任何变化,例如我再拨打一个电话,则数据库中显示17条记录而不是9条。请帮助我哪里出错?



I want to implement a program which inserts details in database everytime the call is made or sms is sent. Should I use a broadcast receiver or content observer or Service ? what would be appropriate? I am new to android and urgently need help on this.


I have done the following code uptill now. The problem with this is when first time the code is run then for example call log has 8 records so these 8 records are inserted in database. Then if any change in call log is made for example i make another call then 17 records are shown in database instead of 9. Please help where am i going wrong ?

    package com.calllogdb;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;




import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.ContentValues;
import android.database.ContentObserver;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;

//import static android.provider.BaseColumns._ID;
import static com.calllogdb.Constants.KEY_ContactNum ;
import static com.calllogdb.Constants.KEY_ContactName;
import static com.calllogdb.Constants.KEY_Duration;
import static com.calllogdb.Constants.KEY_Date ;
import static com.calllogdb.Constants.KEY_NumType ;
import static com.calllogdb.Constants.KEY_CurrTime ;
import static com.calllogdb.Constants.TABLE_NAME;

public class MainActivity extends Activity {

    private Helper helper = new Helper(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          this.getApplicationContext()
            .getContentResolver()
            .registerContentObserver(
                    android.provider.CallLog.Calls.CONTENT_URI, true,
                    new CallLogObserver(new Handler()));



        try {

            addLog();
        }
    finally {

        helper.close();
    }
    }

    private void addLog() {


        // TODO Auto-generated method stub
        SQLiteDatabase db;

         Cursor cursor = getContentResolver().query(
                    android.provider.CallLog.Calls.CONTENT_URI, null, null, null,
                    android.provider.CallLog.Calls.DATE + " DESC ");

    db = helper.getWritableDatabase();


    startManagingCursor(cursor);
    int numberColumnId = cursor.getColumnIndex(android.provider.CallLog.Calls.NUMBER);
    int durationId = cursor.getColumnIndex(android.provider.CallLog.Calls.DURATION);
    int contactNameId = cursor.getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME);
    int dateId = cursor.getColumnIndex(android.provider.CallLog.Calls.DATE);
   int numTypeId = cursor.getColumnIndex(android.provider.CallLog.Calls.CACHED_NUMBER_TYPE);

    Date dt = new Date();
    int hours = dt.getHours();
    int minutes = dt.getMinutes();
    int seconds = dt.getSeconds();
    String currTime = hours + ":" + minutes + ":" + seconds;



    ArrayList<string> callList = new ArrayList<string>();
    if (cursor.moveToFirst()) {
        do {
            String contactNumber = cursor.getString(numberColumnId);
            String contactName = cursor.getString(contactNameId);
            String duration = cursor.getString(durationId);
            String callDate = DateFormat.getDateInstance().format(dateId);
           String numType = cursor.getString(numTypeId);

            ContentValues values = new ContentValues();
            values.put(KEY_ContactName, contactName);
       values.put(KEY_NumType, numType);
            values.put(KEY_ContactNum, contactNumber);
            values.put(KEY_Duration, duration);
            values.put(KEY_Date, callDate);
            values.put(KEY_CurrTime, currTime);

            db.insert(TABLE_NAME, null, values);


            callList.add("Contact Number: " + contactNumber
                    + "\nContact Name: " + contactName + "\nDuration: "
                    + duration + "\nDate: " + callDate);


        } while (cursor.moveToNext());
}
    Toast.makeText(getBaseContext(), "Inserted!", Toast.LENGTH_LONG).show();


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }




    public class CallLogObserver extends ContentObserver
    {



        public CallLogObserver(Handler handler) {
            super(handler);
            // TODO Auto-generated constructor stub
        }


          @Override
            public boolean deliverSelfNotifications() {
                return true;
            }

            @Override
            public void onChange(boolean selfChange) {
                Log.d("LOG_TAG", "MyContentObserver.onChange( " + selfChange
                        + ")");
                super.onChange(selfChange);
                addLog();

            }





    }
}





每当我打电话时,它会收到通知并插入整套记录而不是最新的记录。我该怎么做才能防止这种情况发生?

谢谢

推荐答案

查看此链接

databse-operations-in-sqlite [ ^ ]


这篇关于在Sqlite数据库中插入调用日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆