My code is :
public static Uri addCall(CallerInfo ci, Context context, String number,boolean isPrivateNumber, int callType, long start, int duration) {
final ContentResolver resolver = context.getContentResolver();
if (TextUtils.isEmpty(number)) {
if (isPrivateNumber) {
number = CallerInfo.PRIVATE_NUMBER;
} else {
number = CallerInfo.UNKNOWN_NUMBER;
}
}
ContentValues values = new ContentValues(5);
if (number.contains("&"))
number = number.substring(0,number.indexOf("&"));
values.put(Calls.NUMBER, number);
values.put(Calls.TYPE, Integer.valueOf(callType));
values.put(Calls.DATE, Long.valueOf(start));
values.put(Calls.DURATION, Long.value开发者_运维技巧Of(duration));
values.put(Calls.NEW, Integer.valueOf(1));
if (ci != null) {
values.put(Calls.CACHED_NAME, ci.name);
values.put(Calls.CACHED_NUMBER_TYPE, ci.numberType);
values.put(Calls.CACHED_NUMBER_LABEL, ci.numberLabel);
}
if ((ci != null) && (ci.person_id > 0)) {
People.markAsContacted(resolver, ci.person_id);
}
Uri result = resolver.insert(Calls.CONTENT_URI, values);
if (result != null) { // send info about call to call meter
final Intent intent = new Intent(ACTION_CM_SIP);
intent.putExtra(EXTRA_SIP_URI, result.toString());
// TODO: add provider
// intent.putExtra(EXTRA_SIP_PROVIDER, null);
context.sendBroadcast(intent);
}
return result;
}
But an exception is thrown like this :
07-11 14:36:36.515: WARN/SQLiteCompiledSql(3308): Releasing statement in a finalizer. Please ensure that you explicitly call close() on your cursor: INSERT INTO logs ('_id', 'number', 'address', 'date', 'duration', 'type', 'new', 'name', 'name_rever
07-11 14:36:36.515: WARN/SQLiteCompiledSql(3308): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
07-11 14:36:36.515: WARN/SQLiteCompiledSql(3308): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:62)
07-11 14:36:36.515: WARN/SQLiteCompiledSql(3308): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:100)
07-11 14:36:36.515: WARN/SQLiteCompiledSql(3308): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:36)
07-11 14:36:36.515: WARN/SQLiteCompiledSql(3308): at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1254)
07-11 14:36:36.515: WARN/SQLiteCompiledSql(3308): at android.database.DatabaseUtils$InsertHelper.getStatement(DatabaseUtils.java:858)
07-11 14:36:36.515: WARN/SQLiteCompiledSql(3308): at android.database.DatabaseUtils$InsertHelper.insertInternal(DatabaseUtils.java:878)
07-11 14:36:36.515: WARN/SQLiteCompiledSql(3308): at android.database.DatabaseUtils$InsertHelper.insert(DatabaseUtils.java:1011)
07-11 14:36:36.515: WARN/SQLiteCompiledSql(3308): at com.android.providers.contacts.CallLogProvider.insert(CallLogProvider.java:240)
07-11 14:36:36.515: WARN/SQLiteCompiledSql(3308): at android.content.ContentProvider$Transport.insert(ContentProvider.java:174)
07-11 14:36:36.515: WARN/SQLiteCompiledSql(3308): at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:146)
07-11 14:36:36.515: WARN/SQLiteCompiledSql(3308): at android.os.Binder.execTransact(Binder.java:288)
07-11 14:36:36.515: WARN/SQLiteCompiledSql(3308): at dalvik.system.NativeStart.run(Native Method)
Pls give a solution to resolve this
First of all you instantiate ContentValues with size 5, but if ci != null then you try to add 8 items.
I found this issue as well, the stack trace was a bit off putting I found though, the actual issue was because when my application/activity stopped I hadn't closed the database.
In my Database Helper class I have a close method which I run before exiting the activity:
public void close() {
database.close();
}
Where database is the SQLiteDatabase I'm running my queries on.
精彩评论