Get inbox messages from android device to show in custom listview

user1451125 picture user1451125 · Dec 31, 2012 · Viewed 54.9k times · Source

Possible Duplicate:
How can I read SMS messages from the inbox programmatically in Android?

I don't know how can i access the inbox of an android phone programmatically, can you please guide me or share some tutorial how can i do it(Access the inbox of the phone). by the way my application goes like this. it is an SMS Encrypter and my app replicates what the original inbox have and by that i can encrypt messages in send it and vice versa my app is the only way to decrypt that said message.

Answer

RobinHood picture RobinHood · Dec 31, 2012

With the help of content Provider you can achieve your goal, look out below Example which is copied from here so that if the blog disappears this post will remain useful, hope it will help you and also go though content provider.

AndroidManifest.xml

  <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.itcuties.android.apps"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
       android:minSdkVersion="8"
       android:targetSdkVersion="16" />

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

    <application
       android:allowBackup="true"
       android:icon="@drawable/ic_launcher"
       android:label="@string/app_name"
       android:theme="@style/AppTheme" >
       <activity
           android:name="com.itcuties.android.apps.MainActivity"
           android:label="@string/app_name" >
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />
               <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
       </activity>
    </application>

</manifest>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000">

    <TextView
       android:id="@+id/smsNumberText"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="NUMBER_GOES_HERE">
    </TextView>

</LinearLayout>

SMSData.java

package com.itcuties.android.apps.data;

/**
* This class represents SMS.
*
* @author itcuties
*
*/
public class SMSData {

   // Number from witch the sms was send
   private String number;
   // SMS text body
   private String body;

   public String setNumber() {
       return number;
   }

   public void setNumber(String number) {
       this.number = number;
   }

   public String setBody() {
       return body;
   }

   public void setBody(String body) {
       this.body = body;
   }

}

ListAdapter.java

package com.itcuties.android.apps;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.itcuties.android.apps.data.SMSData;


/**
* List adapter for storing SMS data
*
* @author itcuties
*
*/
public class ListAdapter extends ArrayAdapter<SMSData> {

   // List context
    private final Context context;
    // List values
    private final List<SMSData> smsList;

   public ListAdapter(Context context, List<SMSData> smsList) {
       super(context, R.layout.activity_main, smsList);
       this.context = context;
       this.smsList = smsList;
   }

   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
       LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.activity_main, parent, false);

       TextView senderNumber = (TextView) rowView.findViewById(R.id.smsNumberText);
       senderNumber.setText(smsList.get(position).setNumber().toString());

       return rowView;
   }

}

MainActivity.java

package com.itcuties.android.apps;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import com.itcuties.android.apps.data.SMSData;

/**
* Main Activity. Displays a list of numbers.
*
* @author itcuties
*
*/
public class MainActivity extends ListActivity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);

 ArrayList<Message> smsInbox = new ArrayList<Message>();

Uri uriSms = Uri.parse("content://sms");

Cursor cursor = this.getContentResolver()
        .query(uriSms,
                new String[] { "_id", "address", "date", "body",
                        "type", "read" }, "type=" + type, null,
                "date" + " COLLATE LOCALIZED ASC");
if (cursor != null) {
    cursor.moveToLast();
    if (cursor.getCount() > 0) {

        do {

            Message message = new Message();
            message.messageNumber = cursor.getString(cursor
                    .getColumnIndex("address"));
            message.messageContent = cursor.getString(cursor
                    .getColumnIndex("body"));
            smsInbox.add(message);
        } while (cursor.moveToPrevious());
    }

       }
       c.close();

       // Set smsList in the ListAdapter
       setListAdapter(new ListAdapter(this, smsList));

   }

   @Override
   protected void onListItemClick(ListView l, View v, int position, long id) {
       SMSData sms = (SMSData)getListAdapter().getItem(position);

       Toast.makeText(getApplicationContext(), sms.setBody(), Toast.LENGTH_LONG).show();

   }

}

also check below links related@

Android Reading Inbox Message

SMS Messaging

inbox_message_listview