如何为通话记录实现 ContentObserver [英] How to implement a ContentObserver for call logs

查看:31
本文介绍了如何为通话记录实现 ContentObserver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有办法知道呼叫的内容提供者是否已更改.我的意思是,如果我拨打电话或接听电话,它会返回一个标志",表明已将新日志添加到通话记录或 Android 存储通话信息的位置.

I'd like to know if there is a way to know if the content provider of callings has changed. I mean, if I make a call, or I answer a call, it returns a "flag" that a new log has been added to the call log, or the place where Android store informations about callings.

因为,当我拨打电话时,Android 会将号码、联系人姓名(如果存在)、通话时间、时长等全部存储在内容提供程序中.那么有没有办法捕获这个标志",表明呼叫的内容提供者更大,我的意思是,新数据已插入内容提供者 CallLog.Calls.

Because, when I make a call, Android stores the number, the contact name (if exists), the hour of the calling, the duration, ..., all in the content provider. So is there a way to capture this "flag" that says the content provider of callings is bigger, I mean, that a new data has been inserted on the content provider CallLog.Calls.

所以,我对这个问题还有很多疑问.我不知道在哪里注册内容观察者.我的意图是,当 CallLog 内容提供者发生变化时,将使用代码的插入方法.

So, I still have a lot of doubts related to this issue. I don't know where to register the content observer. My intention is, when something changes in the CallLog content provider, the insert method of the code will be used.

我的意思是,除非新数据已添加到 CallLog 内容提供程序,否则代码不会执行任何操作.如果一些数据已经添加到 CallLog 内容提供器中,那么代码将查询新数据,然后插入.我想这样做,因为如果没有内容观察者,应用程序会在每次运行应用程序时将数据插入数据库中,明白了吗?

I mean, the code won't do anything unless new data has been added to the CallLog content provider. If some data has been added to the CallLog content provider, then the code will query the new data, and then will insert. I wanna do this, because without a content observer the application was inserting data in the database that was already inserted every time I run the application, got it?

这是我的代码.如果有人能告诉我把 registerContentObserver() 和其他所有需要的东西放在哪里.

So here is my code. If someone could tell me where to put registerContentObserver() and everything else that is needed.

public class RatedCalls extends ListActivity {

    private SQLiteDatabase db;
    private CallDataHelper dh = null;
    StringBuilder sb = new StringBuilder();
    OpenHelper openHelper = new OpenHelper(RatedCalls.this);

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

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

        dh = new CallDataHelper(this);
        db = openHelper.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("contact_id", 1);
                values.put("contact_name", contactName);
                values.put("number_type", numType);
                values.put("contact_number", contactNumber);
                values.put("duration", duration);
                values.put("date", callDate);
                values.put("current_time", currTime);
                values.put("cont", 1);

                this.db.insert(CallDataHelper.TABLE_NAME, null, values);

                Toast.makeText(getBaseContext(), "Inserted!", Toast.LENGTH_LONG);
                callList.add("Contact Number: " + contactNumber
                        + "
Contact Name: " + contactName + "
Duration: "
                        + duration + "
Date: " + callDate);

            } while (cursor.moveToNext());
        }
        setListAdapter(new ArrayAdapter<String>(this, R.layout.listitem, callList));

        ListView lv = getListView();
        lv.setTextFilterEnabled(true);
        lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}

推荐答案

这就是答案.不要忘记用这个方法注册内容观察者:

Here is the answer. Dont forget to register the content observer with this method:

registerContentObserver (Uri uri, boolean notifyForDescendents, ContentObserver observer)

然后你可以像这样创建它.

And then you can create it like this.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.getApplicationContext()
    .getContentResolver()
    .registerContentObserver(
            android.provider.CallLog.Calls.CONTENT_URI, true,
            new MyContentObserver(handler)); 
}

class MyContentObserver extends ContentObserver {
    public MyContentObserver(Handler h) {
        super(h);
    }

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

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

        // here you call the method to fill the list
    }
}

这篇关于如何为通话记录实现 ContentObserver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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