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

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

问题描述

我想知道是否有一种方法可以知道,如果召唤的内容提供商已经改变。我的意思是,如果我打个电话,或者我接听电话,它会返回一个新的日志已经被添加到通话记录,还是地方的标志,其中约召唤Android商店的信息。

由于,当我拨打一个电话,Android的门店数量,联系人姓名(如果存在),小时的通话中,持续的时间,......,所有的内容提供商。那么,有没有办法捕捉到了这个标志,说的召唤的内容提供商比较大,我的意思是,一个新的数据已经被插入到内容提供商CallLog.Calls。

所以,我还有很多与此相关的问题的疑虑。我不知道在哪里注册的内容观察者。我的本意是,当有新的变化,在CallLog内容提供商,将使用的code中的插入方法。

我的意思是,code什么都不会做,除非新的数据已经被添加到CallLog内容提供商。如果一些数据已被添加到CallLog内容提供商,则code将查询新的数据,然后将插入。我想这样做,因为没有内容观察者的应用程序是在一个已经插入我每次运行应用程序时数据库中插入数据,明白了吗?

因此​​,这里是我的code。如果有人能告诉我在哪里把 registerContentObserver()和其他一切是需要的。

 公共类RatedCalls扩展ListActivity {

    私人SQLiteDatabase分贝;
    私人CallDataHelper DH = NULL;
    StringBuilder的SB =新的StringBuilder();
    OpenHelper openHelper =新OpenHelper(RatedCalls.this);

    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);

        光标光标= getContentResolver()查询(
                android.provider.CallLog.Calls.CONTENT_URI,NULL,NULL,NULL,
                android.provider.CallLog.Calls.DATE +降序);

        DH =新CallDataHelper(本);
        DB = openHelper.getWritableDatabase();

        startManagingCursor(光标);
        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);

        日期DT =新的日期();
        INT小时= dt.getHours();
        INT分钟= dt.getMinutes();
        INT秒= dt.getSeconds();
        串currTime =小时+:+分钟+:+秒;

        ArrayList的<字符串>呼叫清单=新的ArrayList<字符串>();
        如果(cursor.moveToFirst()){
            做 {
                字符串contactNumber = cursor.getString(numberColumnId);
                字符串CONTACTNAME = cursor.getString(contactNameId);
                字符串长度= cursor.getString(durationId);
                。字符串callDate = DateFormat.getDateInstance()格式(dateId);
                字符串numType = cursor.getString(numTypeId);

                ContentValues​​值=新ContentValues​​();
                values​​.put(CONTACT_ID,1);
                values​​.put(CONTACT_NAME,联系人姓名);
                values​​.put(NUMBER_TYPE,numType);
                values​​.put(contact_number,contactNumber);
                values​​.put(持续时间,持续时间);
                values​​.put(日期,callDate);
                values​​.put(CURRENT_TIME,currTime);
                values​​.put(CONT,1);

                this.db.insert(CallDataHelper.TABLE_NAME,空,价值观);

                Toast.makeText(getBaseContext(),插入!,Toast.LENGTH_LONG);
                callList.add(联络号码:+ contactNumber
                        +\ n请联系姓名:+联系人姓名+\ nDuration:
                        持续时间+ +\ nDate:+ callDate);

            }而(cursor.moveToNext());
        }
        setListAdapter(新ArrayAdapter<字符串>(这一点,R.layout.listitem,呼叫清单));

        ListView的LV = getListView();
        lv.setTextFilterEnabled(真正的);
        lv.setOnItemClickListener(新android.widget.AdapterView.OnItemClickListener(){
            @覆盖
            公共无效onItemClick(适配器视图<>母公司视图中查看,INT位置,长的id){
                Toast.makeText(getApplicationContext(),((TextView的)视图).getText(),Toast.LENGTH_SHORT).show();
            }
        });
    }
}
 

解决方案

下面就是答案。不要忘了用此方法注册内容观察员:

  registerContentObserver(URI URI,布尔notifyForDescendents,ContentObserver观察员)
 

然后就可以像这样创建它。

 公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    this.getApplicationContext()
    .getContentResolver()
    .registerContentObserver(
            android.provider.CallLog.Calls.CONTENT_URI,真实,
            新MyContentObserver(处理));
}

类MyContentObserver扩展ContentObserver {
    公共MyContentObserver(处理器高){
        超(H);
    }

    @覆盖
    公共布尔deliverSelfNotifications(){
        返回true;
    }

    @覆盖
    公共无效的onChange(布尔selfChange){
        Log.d(LOG_TAGMyContentObserver.onChange(+ selfChange +));
        super.onChange(selfChange);

        //在这里调用方法来填充列表
    }
}
 

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.

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.

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.

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?

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
                        + "\nContact Name: " + contactName + "\nDuration: "
                        + duration + "\nDate: " + 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天全站免登陆