本文介绍了Android的ContentObserver从未停止过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了一个ContentObserver它工作得很好。但现在,每次的ContentObserver通知在CallLog.Calls内容提供商一定的变化,它运行不停止的onChange()方法。

我想,我的观察员运行一次,在内容提供商已经改变了每个项目。所以,如果5个新的项目已经被加入到CallLog.Calls内容提供商,观察者必须通知5次,并为每个通知给观察者,一个新的呼叫到的onChange()方法必须发生。

下面是我的code。

 公共类RatedCalls扩展ListActivity {

私有静态最后弦乐LOG_TAG =RATEDCALLSOBSERVER;
私人处理程序处理程序=新的处理程序();
私人RatedCallsContentObserver callsObserver = NULL;
私人SQLiteDatabase分贝;
私人光标指针;
StringBuilder的SB =新的StringBuilder();
OpenHelper openHelper =新OpenHelper(RatedCalls.this);

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    this.getApplicationContext()
            .getContentResolver()
            .registerContentObserver(
                    android.provider.CallLog.Calls.CONTENT_URI,真实,
                    新RatedCallsContentObserver(处理));
    Log.d(FILLLIST,来自的onCreate()调用);
}

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

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

    @覆盖
    公共无效的onChange(布尔selfChange){
        Log.d(LOG_TAG,RatedCallsContentObserver.onChange(+ selfChange
                +));
        super.onChange(selfChange);
        fillList();
    }
}

私人无效fillList(){

    光标= getContentResolver()查询(
            android.provider.CallLog.Calls.CONTENT_URI,NULL,NULL,NULL,
            android.provider.CallLog.Calls.DATE +降序);
    Log.d(FILLLIST,从filllist调用);

    DB = openHelper.getWritableDatabase();

    startManagingCursor(光标);
    INT numberColumnId =光标
            .getColumnIndex(android.provider.CallLog.Calls.NUMBER);
    INT durationId =光标
            .getColumnIndex(android.provider.CallLog.Calls.DURATION);
    INT contactNameId =光标
            .getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME);
    INT dateId = cursor.getColumnIndex(android.provider.CallLog.Calls.DATE);
    INT numTypeId =光标
            .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);
    getBaseContext()。getContentResolver()。有NotifyChange(
            android.provider.CallLog.Calls.CONTENT_URI,
            新RatedCallsContentObserver(处理));
    db.insert(CallDataHelper.TABLE_NAME,空,价值观);
    Toast.makeText(getBaseContext(),插入!,Toast.LENGTH_LONG);
    callList.add(联络号码:+ contactNumber +\ n请联系名字:
            +联系人姓名+\ nDuration:+时间+\ nDate:
            + callDate);

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

        }
    });
}
}
 

解决方案

您更改数据库的内容在 fillList(),所以 RatedCallsContentObserver。的onChange()连连叫...

I implemented a ContentObserver and it worked fine. But now everytime the ContentObserver is notified for some changes in the CallLog.Calls content provider, it runs the onChange() method without stopping.

I'd like that my observer runs once for each item that has changed in the content provider. So, if 5 new items have been added to the CallLog.Calls content provider, the observer must be notified 5 times, and for each notify to the observer, a new call to the onChange() method must happen.

Here is my code.

public class RatedCalls extends ListActivity {

private static final String LOG_TAG = "RATEDCALLSOBSERVER";
private Handler handler = new Handler();
private RatedCallsContentObserver callsObserver = null;
private SQLiteDatabase db;
private Cursor cursor;
StringBuilder sb = new StringBuilder();
OpenHelper openHelper = new OpenHelper(RatedCalls.this);

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.getApplicationContext()
            .getContentResolver()
            .registerContentObserver(
                    android.provider.CallLog.Calls.CONTENT_URI, true,
                    new RatedCallsContentObserver(handler));
    Log.d("FILLLIST", "calling from onCreate()");
}

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

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

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

private void fillList() {

    cursor = getContentResolver().query(
            android.provider.CallLog.Calls.CONTENT_URI, null, null, null,
            android.provider.CallLog.Calls.DATE + " DESC ");
    Log.d("FILLLIST", "Calling from filllist");

    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>();
    cursor.moveToFirst();

    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);
    getBaseContext().getContentResolver().notifyChange(
            android.provider.CallLog.Calls.CONTENT_URI,
            new RatedCallsContentObserver(handler));
    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);

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

        }
    });
}   
}
解决方案

You change DB content inside of the fillList(), so RatedCallsContentObserver.onChange() is called again and again...

这篇关于Android的ContentObserver从未停止过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 11:35