我有20个联系人存储在数组中,我想只检索那些联系人的短信然后进行处理?
这可能吗?我的意思是我可以编写where子句来存储查询中的联系人,然后检查是否如何去做?
或者我应该使用for循环来逐个检索短信吗?
在循环内部是这样的:
Cursor cur = getContentResolver().query(uriSMSURI, null, "from=+14015963069", null,null);
如果有人可以指导我这件事会很棒吗?
谢谢!
更新
根据建议尝试以下内容:
Uri uriSMSuri = Uri.parse("content://mms-sms/conversations?simple=true");
int argcount = 2; // number of IN arguments
String[] args = new String[]{ "+14103599690", "+167890" };
StringBuilder inList = new StringBuilder(argcount*2); //for adding the ?
for(int i=0;i<argcount;i++)
{
if (i > 0)
{
inList.append(",");
}
inList.append("?");
}
cursor = getContentResolver().query(uriSMSuri, new String[] {"*"}, "address IN ("+inList.toString()+")", args, null);
但我仍然收到以下日志错误:
05-04 07:35:37.240: E/AndroidRuntime(32662): FATAL EXCEPTION: main
05-04 07:35:37.240: E/AndroidRuntime(32662): Process: com.ylg.smsapp, PID: 32662
05-04 07:35:37.240: E/AndroidRuntime(32662): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ylg.smsapp/com.ylg.smsapp.Sms}: android.database.sqlite.SQLiteException: no such column: field (code 1): , while compiling: SELECT * FROM threads WHERE field IN (?,?) ORDER BY date DESC
05-04 07:35:37.240: E/AndroidRuntime(32662): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
05-04 07:35:37.240: E/AndroidRuntime(32662): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
05-04 07:35:37.240: E/AndroidRuntime(32662): at android.app.ActivityThread.access$800(ActivityThread.java:151)
05-04 07:35:37.240: E/AndroidRuntime(32662): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
05-04 07:35:37.240: E/AndroidRuntime(32662): at android.os.Handler.dispatchMessage(Handler.java:102)
05-04 07:35:37.240: E/AndroidRuntime(32662): at android.os.Looper.loop(Looper.java:135)
05-04 07:35:37.240: E/AndroidRuntime(32662): at android.app.ActivityThread.main(ActivityThread.java:5254)
05-04 07:35:37.240: E/AndroidRuntime(32662): at java.lang.reflect.Method.invoke(Native Method)
05-04 07:35:37.240: E/AndroidRuntime(32662): at java.lang.reflect.Method.invoke(Method.java:372)
05-04 07:35:37.240: E/AndroidRuntime(32662): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
05-04 07:35:37.240: E/AndroidRuntime(32662): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
05-04 07:35:37.240: E/AndroidRuntime(32662): Caused by: android.database.sqlite.SQLiteException: no such column: field (code 1): , while compiling: SELECT * FROM threads WHERE field IN (?,?) ORDER BY date DESC
05-04 07:35:37.240: E/AndroidRuntime(32662): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:181)
05-04 07:35:37.240: E/AndroidRuntime(32662): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
05-04 07:35:37.240: E/AndroidRuntime(32662): at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
05-04 07:35:37.240: E/AndroidRuntime(32662): at android.content.ContentResolver.query(ContentResolver.java:478)
05-04 07:35:37.240: E/AndroidRuntime(32662): at android.content.ContentResolver.query(ContentResolver.java:422)
05-04 07:35:37.240: E/AndroidRuntime(32662): at com.ylg.smsapp.Sms.onCreate(Sms.java:112)
05-04 07:35:37.240: E/AndroidRuntime(32662): at android.app.Activity.performCreate(Activity.java:5990)
05-04 07:35:37.240: E/AndroidRuntime(32662): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
05-04 07:35:37.240: E/AndroidRuntime(32662): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
05-04 07:35:37.240: E/AndroidRuntime(32662): ... 10 more
答案 0 :(得分:1)
您可以尝试使用&#34; IN&#34;
String[] args = new String[]{ "+1234576", "+167890" };
cursor = contentResolver.query(uriSMSURI, PROJECTION, "address IN (?)", args, null);