gpt4 book ai didi

java - 在android中获取短信对话

转载 作者:行者123 更新时间:2023-11-30 03:32:10 26 4
gpt4 key购买 nike

我正在开发一个显示对话列表的 android 应用程序。

我的代码在 Samsung Galaxy S、Nexus one 和 Galaxy Nexus 中运行良好,但查询在我的 Galaxy Grand 手机中出现问题。

这是我的代码:

public  ArrayList<String> getSMS()
{

ArrayList<String> sms = new ArrayList<String>();
Uri uriSMSURI = Uri.parse("content://mms-sms/conversations/");
Cursor cursor = getContentResolver().query(uriSMSURI, null, null, null, "date desc");
//cursor.moveToFirst();
while (cursor.moveToNext())
{
String address = cursor.getString(cursor.getColumnIndex("address"));
String body = cursor.getString(cursor.getColumnIndexOrThrow("body"));
String read = cursor.getString(cursor.getColumnIndexOrThrow("read"));


//to fetch the contact name of the conversation
String contactName = address;
Uri Nameuri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address));
Cursor cs= getContentResolver().query(Nameuri, new String[]{PhoneLookup.DISPLAY_NAME},PhoneLookup.NUMBER+"='"+address+"'",null,null);

if(cs.getCount()>0)
{
cs.moveToFirst();
contactName = cs.getString(cs.getColumnIndex(PhoneLookup.DISPLAY_NAME));
}

sms.add(contactName + "\n"+body);
}
return sms;
}

以下是stackTrace:

  06-24 17:26:21.744: E/AndroidRuntime(12764): FATAL EXCEPTION: main
06-24 17:26:21.744: E/AndroidRuntime(12764): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ssn.sms/com.ssn.sms.test}: java.lang.NullPointerException
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.app.ActivityThread.access$700(ActivityThread.java:140)
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.os.Handler.dispatchMessage(Handler.java:99)
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.os.Looper.loop(Looper.java:137)
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.app.ActivityThread.main(ActivityThread.java:4935)
06-24 17:26:21.744: E/AndroidRuntime(12764): at java.lang.reflect.Method.invokeNative(Native Method)
06-24 17:26:21.744: E/AndroidRuntime(12764): at java.lang.reflect.Method.invoke(Method.java:511)
06-24 17:26:21.744: E/AndroidRuntime(12764): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
06-24 17:26:21.744: E/AndroidRuntime(12764): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
06-24 17:26:21.744: E/AndroidRuntime(12764): at dalvik.system.NativeStart.main(Native Method)
06-24 17:26:21.744: E/AndroidRuntime(12764): Caused by: java.lang.NullPointerException
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.os.Parcel.readException(Parcel.java:1431)
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:188)
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:140)
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.content.ContentProviderProxy.query(ContentProviderNative.java:366)
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.content.ContentResolver.query(ContentResolver.java:372)
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.content.ContentResolver.query(ContentResolver.java:315)
06-24 17:26:21.744: E/AndroidRuntime(12764): at com.ssn.sms.test.getSMS(test.java:157)
06-24 17:26:21.744: E/AndroidRuntime(12764): at com.ssn.sms.test.onCreate(test.java:74)
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.app.Activity.performCreate(Activity.java:5206)
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
06-24 17:26:21.744: E/AndroidRuntime(12764): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
06-24 17:26:21.744: E/AndroidRuntime(12764): ... 11 more

我也引用了下面的链接用了,还是一样

SGS-3 bug related to SMS conversations list?

最佳答案

试试这个我有使用功能所有短信返回 json 字符串

public JSONObject getsms()
{
JSONObject result = null;
JSONArray jarray = null;
String link[] = {"content://sms/inbox","content://sms/sent","content://sms/draft"};

try {

jarray = new JSONArray();

result = new JSONObject();
Uri uri = Uri.parse("content://sms/");
Cursor c= act.getContentResolver().query(uri, null, null ,null,null);
act.startManagingCursor(c);

// Read the sms data and store it in the list
if(c.moveToFirst()) {

for(int i=0; i < c.getCount(); i++) {

result.put("body",c.getString(c.getColumnIndexOrThrow("body")).toString());

result.put("date",c.getString(c.getColumnIndexOrThrow("date")).toString());
result.put("read",c.getString(c.getColumnIndexOrThrow("read")).toString());
result.put("type",c.getString(c.getColumnIndexOrThrow("type")).toString());
if((c.getString(c.getColumnIndexOrThrow("type")).toString()).equals("3"))
{
//Cursor cur= getContentResolver().query("", null, null ,null,null);
//startManagingCursor(cur);

String threadid = c.getString(c.getColumnIndexOrThrow("thread_id")).toString();
Cursor cur= act.getContentResolver().query(Uri.parse("content://mms-sms/conversations?simple=true"), null, "_id ="+threadid ,null,null);
act.startManagingCursor(cur);
if(cur.moveToFirst())
{
String recipientId = cur.getString(cur.getColumnIndexOrThrow("recipient_ids")).toString();
cur= act.getContentResolver().query(Uri.parse("content://mms-sms/canonical-addresses"), null, "_id = " + recipientId, null, null);
act.startManagingCursor(cur);
if(cur.moveToFirst())
{
String address = cur.getString(cur.getColumnIndexOrThrow("address")).toString();
result.put("address",address);
cur.close();
}
}

}else
{
result.put("address",c.getString(c.getColumnIndexOrThrow("address")).toString());
}
jarray.put(result);
result = new JSONObject();


c.moveToNext();
}
}
c.close();

result.put("smslist", jarray);
//result = new JSONObject(jarray.toString());

} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}

关于java - 在android中获取短信对话,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17275105/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com