我有这段代码:
public static long addContactAddresses(Context context, String number) {
long add_id = 0;
final ContentResolver resolver = context.getContentResolver();
final Cursor c = resolver.query(ConvAddresses.CONTENT_URI,
new String[] { ConvAddressesColumns._ID,
ConvAddressesColumns.NUMBER },
ConvAddressesColumns.NUMBER + "=?",
new String[] { number }, null);
try {
if (c != null && c.moveToFirst()) {
if ((c.getString(0)) != null) {
add_id = Long.valueOf(c.getString(0));
}
} else {
ContentValues values = new ContentValues();
values.put(ConvAddressesColumns.NUMBER, number);
Uri result = resolver.insert(ConvAddresses.CONTENT_URI, values);
add_id = ContentUris.parseId(result);
}
} finally {
if (c != null) {
c.close();
}
}
return add_id;
}
很少resolver.insert(ConvAddresses.CONTENT_URI, values);
返回null,这对我来说是个问题......
查看插入实现:
public final Uri insert(Uri url, ContentValues values)
{
IContentProvider provider = acquireProvider(url);
if (provider == null) {
throw new IllegalArgumentException("Unknown URL " + url);
}
try {
long startTime = SystemClock.uptimeMillis();
Uri createdRow = provider.insert(mPackageName, url, values);
long durationMillis = SystemClock.uptimeMillis() - startTime;
maybeLogUpdateToEventLog(durationMillis, url, "insert", null /* where */);
return createdRow;
} catch (RemoteException e) {
// Arbitrary and not worth documenting, as Activity
// Manager will kill this process shortly anyway.
return null;
} finally {
releaseProvider(provider);
}
}
RemoteException
时可以返回null我想将我的代码编辑为:
Uri result = resolver.insert(ConvAddresses.CONTENT_URI, values);
if (result != null) {
add_id = ContentUris.parseId(result);
} else {
Log.w(TAG, "RemoteException in resolver.insert, let's try it again!");
add_id = addContactAddresses(context, number);
}
这会导致无限循环吗?这不好吗?这个好吗?
在这种情况下如何导致RemoteException
?