我正在使用CursorLoader
和ContentProvider
将数据提取到游标中。
据我所知,游标加载器注册了一个内容观察器,因此每当游标的基础数据发生变化时,都应该调用加载器的onLoadFinished
。
在我的代码中,只要呼叫状态发生变化,就会在content://com.example.phonehistory/call
uri上插入一个新行(例如,当一个呼叫处于INCOMING状态然后被切断时(BroadCastReciever中为EXTRA_STATE_IDLE
))。每当我改变状态时,我都会插入数据。
问题是插入新行时未调用加载程序onLoadFinished
。
请注意,当我遇到此问题时,应用程序正在运行。
Loader Callbacks
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
Toast.makeText(this, "onCreate for loader", Toast.LENGTH_SHORT).show();
Uri uri1 = Uri.parse("content://com.example.phonehistory/call");
CursorLoader loader = new CursorLoader(
this,
uri1,
null,
null,
null,
null);
return loader;
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
Toast.makeText(this, "onload finished", Toast.LENGTH_SHORT).show();
if(loader.getId()==1){
showDataChanges(cursor);
}
}
Content Provider的插入和查询方法:
@Override
public Uri insert(Uri uri, ContentValues values) {
Uri insertUri;
int match = myUriMatcher.match(uri);
System.out.println("Insert");
SQLiteDatabase db = mDatabaseHelpwe.getWritableDatabase();
switch(match){
case CALL :db.insert("call_history", null, values);
}
getContext().getContentResolver().notifyChange(uri, null);
return null;
}
@Override
public boolean onCreate() {
// TODO Auto-generated method stub
mDatabaseHelpwe = new DBHelper(getContext());
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
System.out.println("Query");
SQLiteDatabase db = mDatabaseHelpwe.getWritableDatabase();
Cursor c=null;
int match = myUriMatcher.match(uri);
switch(match){
case CALL :c = db.query("call_history", projection, selection, selectionArgs, null, null, null);
}
c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
}
BroadCastReciever插入数据:
@Override
public void onReceive(Context c, Intent i) {
// TODO Auto-generated method stub
Bundle bundle=i.getExtras();
if(bundle==null)
return;
SharedPreferences sp=c.getSharedPreferences("ZnSoftech", Activity.MODE_PRIVATE);
String s=bundle.getString(TelephonyManager.EXTRA_STATE);
if(i.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL))
{
String number=i.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
sp.edit().putString("number", number).commit();
sp.edit().putString("state", s).commit();
}
else if(s.equals(TelephonyManager.EXTRA_STATE_RINGING))
{
String number=bundle.getString("incoming_number");
sp.edit().putString("number", number).commit();
sp.edit().putString("state", s).commit();
}
else if(s.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
{
sp.edit().putString("state", s).commit();
}
else if(s.equals(TelephonyManager.EXTRA_STATE_IDLE))
{
String state=sp.getString("state", null);
if(!state.equals(TelephonyManager.EXTRA_STATE_IDLE))
{
sp.edit().putString("state", null).commit();
}
sp.edit().putString("state", s).commit();
getCalldetailsNow(c);
}
}
private void getCalldetailsNow(Context context) {
// TODO Auto-generated method stub
Cursor managedCursor = context.getContentResolver().query(
CallLog.Calls.CONTENT_URI, null, null, null,
android.provider.CallLog.Calls.DATE + " DESC");
int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
int duration1 = managedCursor
.getColumnIndex(CallLog.Calls.DURATION);
int type1 = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
int date1 = managedCursor.getColumnIndex(CallLog.Calls.DATE);
if (managedCursor.moveToFirst() == true) {
String phNumber = managedCursor.getString(number);
String callDuration = managedCursor.getString(duration1);
String type = managedCursor.getString(type1);
String date = managedCursor.getString(date1);
String dir = null;
int dircode = Integer.parseInt(type);
switch (dircode) {
case CallLog.Calls.OUTGOING_TYPE:
dir = "OUTGOING";
break;
case CallLog.Calls.INCOMING_TYPE:
dir = "INCOMING";
break;
case CallLog.Calls.MISSED_TYPE:
dir = "MISSED";
break;
default:
dir = "MISSED";
break;
}
SimpleDateFormat sdf_date = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat sdf_time = new SimpleDateFormat("h:mm a");
// SimpleDateFormat sdf_dur = new SimpleDateFormat("KK:mm:ss");
String dateString = sdf_date.format(new Date(Long
.parseLong(date)));
String timeString = sdf_time.format(new Date(Long
.parseLong(date)));
// String duration_new=sdf_dur.format(new
// Date(Long.parseLong(callDuration)));
ContentValues values = new ContentValues();
values.put("number", phNumber);
values.put("date", dateString);
values.put("time", timeString);
values.put("duration", callDuration);
values.put("type", dir);
Uri uri1 = Uri.parse("content://com.example.phonehistory/call");
context.getContentResolver().insert(uri1, values);
}
managedCursor.close();
}
我花了几个小时但却无法弄清楚是什么问题。