2014-01-13 47 views
-4

我想讀取對話並將其放入ListView中。我該怎麼做?
我有兩個TextViews每行。我想通過使用BaseAdapter來設置它們。如何在Android中讀取SMS對話

通過使用此, 內容://彩信,短信/通話/

* 更新: *

我試着這樣做:

public ArrayList<String> number = new ArrayList<String>(); 
public ArrayList<String> body = new ArrayList<String>(); 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    ListView listView = (ListView) findViewById(R.id.listView1); 

    listView.setAdapter(new BaseAdapter(){ 
     public int getCount() { 
     return number.size(); 
     } 

     public Object getItem(int position) { 
     return number.get(position); 
     } 

     public long getItemId(int position) { 
     return position; 
     } 

     public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); 
     View view = inflater.inflate(R.layout.contact_row, null); 
     TextView textView = (TextView) view.findViewById(R.id.textView1); 
     textView.setText(number.get(position)); 
     return view; 
     }}); 


} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

public void getcon() 
{ 
    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")); 

     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)); 
     } 

     number.add(contactName); 
     } 
} 
+0

在顯示列表中的任何問題..? –

+0

@BlackTiger表示 – dave

+0

顯示記錄中是否有任何錯誤。 –

回答

1
public class ServiceForBoot extends Service { 

private IntentFilter filter; 
@Override 
public void onCreate() { 
    super.onCreate(); 
    filter = new IntentFilter("IntentTag"); 
    registerReceiver(new TextMessageReceiver(), filter); 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
} 

@Override 
public void onStart(Intent intent, int startId) { 

} 

@Override 
public IBinder onBind(Intent arg0) { 
    // TODO Auto-generated method stub 
    return null; 
} 
} 

public class TextMessageReceiver extends BroadcastReceiver{ 
Context context; 
Intent intent; 
public void onReceive(Context context, Intent intent) 
{ 
    this.context = context; 
    this.intent = intent; 
    Bundle bundle=intent.getExtras(); 
    Object[] messages=(Object[])bundle.get("pdus"); 
    SmsMessage[] sms=new SmsMessage[messages.length]; 

    for(int n=0;n<messages.length;n++){ 
     sms[n]=SmsMessage.createFromPdu((byte[]) messages[n]); 
    } 

    for(SmsMessage msg:sms){ 
     String number = msg.getOriginatingAddress(); 
     String message = msg.getMessageBody(); 
     Log.v("TxtReceiver", "Number: " + number); 
     Log.v("TxtReceiver", "Number: " + message); 
    } 
} 
} 

當然,您需要爲這些服務和接收器設置清單。

<uses-permission android:name="android.permission.RECEIVE_SMS" /> 

     <receiver android:name=".TextMessageReceiver" > 
     <intent-filter> 
      <action android:name="android.provider.Telephony.SMS_RECEIVED"/> 
     </intent-filter> 
    </receiver> 
+0

不是這個..我想從收件箱中讀取短信對話 – dave

+0

那麼你有短信數據和你請求實現列表視圖?如果是這樣,您可以搜索自定義數組適配器的簡單示例,您只需將該數組提供給適配器並通知列表視圖以進行數據更改。 –

0

嘗試用這種

Uri uriSMSURI = Uri.parse("content://sms/inbox"); 
Cursor cur = getContentResolver().query(uriSMSURI, null, null, null,null); 
String sms = ""; 
while (cur.moveToNext()) { 
sms += "From :" + cur.getString(2) + " : " + cur.getString(11)+"\n";   
} 

而且添加該權限:

<uses-permission name="android.permission.READ_SMS" /> 
+0

謝謝,但我不想讀短信,我想讀對話。看到更新我無法看到項目,什麼是問題。 – dave