在android中获取Sms对话

时间:2013-06-24 12:07:52

标签: java android uri

我正在开发一个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

我也提到了下面的Link并使用了它,但它仍然是相同的

SGS-3 bug related to SMS conversations list?

1 个答案:

答案 0 :(得分:1)

尝试这个我使用函数所有sms返回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;
 }