【问题标题】:How to implement a ContentObserver for call logs如何为通话记录实现 ContentObserver
【发布时间】:2010-12-12 15:39:31
【问题描述】:

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

因为,当我拨打电话时,Android 会将号码、联系人姓名(如果存在)、通话时间、持续时间……都存储在内容提供程序中。那么有没有办法捕获这个“标志”,它表明调用的内容提供者更大,我的意思是,已经在内容提供者 CallLog.Calls 上插入了一个新数据。

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

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

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

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

【问题讨论】:

    标签: android android-contentprovider calllog


    【解决方案1】:

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

    registerContentObserver (Uri uri, boolean notifyForDescendents, ContentObserver observer)
    

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

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

    【讨论】:

    • 不要忘记在 onDestroy() 方法中取消注册提供程序。
    • 在新的 RatedCallsContentObserver(handler)) 行中;处理程序来自哪里/我为什么需要这个?谢谢!
    • 只是传入 new Handler() 而不是 handler 工作。我只是不确定这是否是最好的方法,必须阅读它。
    • 这个处理程序参数是在 onChange(boolean) 上运行。 onChange() 将发生在提供程序处理程序上。请参阅文档developer.android.com/reference/android/database/…
    • @BenH 如果您不取消注册以前注册的观察者,它留在内存中。您将获得内存泄漏,因为您注册的对象保留了对您的活动的引用。
    【解决方案2】:

    只需创建 ContentObserver 类的新子类并覆盖它的 onChange() 方法。 onChange() 方法将包含将在内容更改时执行的所有代码。

    public class MyObserver extends ContentObserver {
        public MyObserver(Handler handler) {
                super(handler);
            }
    
        @Override
        public void onChange(boolean selfChange) {
            this.onChange(selfChange,null);
        }
    
        @Override
        public void onChange(boolean selfChange, Uri uri) {
            //Write your code here      }
    }
    

    那么您所要做的就是将您的内容提供者注册到 URI。

    getContentResolver().registerContentObserver(YourURI,true,myObserver);
    

    记得注销你的内容观察者对象,否则会导致内存泄漏。

    来源:Use Android's ContentObserver To React On Content Changes

    【讨论】:

      猜你喜欢
      • 2014-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多