-4

I want to read the conversation and put it in ListView. How can I do it?
I have two TextViews for each row. I want to set them by using BaseAdapter.

By using this, content://mms-sms/conversations/

*Update:*

I tried doing this:

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);
      }
}
dave
  • 59
  • 1
  • 5

2 Answers2

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

and

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

And of course you need to set your manifest for those services and receivers.

<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>
Barışcan Kayaoğlu
  • 1,294
  • 3
  • 14
  • 35
  • So do you have the sms data and you ask for implementation of list view? If so you can search for simple examples of custom array adapter which you just give an array to the adapter and notify the list view for data change. – Barışcan Kayaoğlu Jan 13 '14 at 13:33
0

Try with this

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

Also add this Permission :

<uses-permission name="android.permission.READ_SMS" />
Vijju
  • 3,458
  • 1
  • 22
  • 20
  • Thanks, But I dont want to read sms, I want to read conversation. See the update I am not able to see the items, Whats the problem. – dave Jan 14 '14 at 07:22