GTU Android Program 13 Read all Message (SMS)

      Comments Off on GTU Android Program 13 Read all Message (SMS)

GTU Android Program 13 Read all Message (SMS)

GTU Android Program 13

Read Message is very easy task in Android. Here I have explained how you can read all message which is available in the message inbox. After Social messenger apps like whatsapp or hike has launched we are mostly using this apps instead of normal message, but still if you would like to do something with message you can use this code to read all message and use it.

Questions Related with Android Message API

  • Explain sending and Receiving SMS programmatically with example.
  • What XML should be placed with AndroidManifest.xml to give
    permission for send and receive SMS?
  • What is needed by the application to send an SMS?
  • Write code snippet for following… Sending an SMS


Step 1 : Crete android project.

Step 2 : Write code as i have explained here.

Step 3 : How to Send SMS to Emulator for Testing ?

  • 3.1 Go to DDMS
  • 3.2 Select Current Running Emulator from left Pane.
  • 3.3 Select Emulator option from right Pane
  • 3.4 Write down incoming number as 123
  • 3.5 Select SMS and write down your message
  • 3.6 Hit send button.

Location : res\layout

FileName: activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="gtu.android.prog13.MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Message"
        android:textColor="#0000ff"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView2"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="16dp" >

    </ListView>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="Developed By: Adarsh Patel"
        android:textColor="#0000ff"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

 

Location : src\package

FileName: ActivityMain.java

package gtu.android.prog13;

/*
 *  GTU Android Program 13 Read all Message (SMS) 
 * 
 */

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

	ListView lv1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	
		lv1 = (ListView) findViewById(R.id.listView1);
		
		
		List<String> s;
		s = readAllMessage();
		
		String messagecsv="";
		
		for(int i=0;i<s.size();i++)
		{
			messagecsv +=  s.get(i) + ",";
		}
		
		String messageArray[];
		
		messageArray = messagecsv.split(",");
		
		//Create Array Adapter and Pass ArrayOfValues to it.
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,android.R.id.text1,messageArray);
		
		//BindAdpater with our Actual ListView
		lv1.setAdapter(adapter);
		
		//Do something on click on ListView Click on Items
		lv1.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
				// Here you can add code like forward, reply, etc
			}
		});
				
	}
	
	public List<String> readAllMessage(){
	     	List<String> sms = new ArrayList<String>();
	        
	     	Uri uriSMSURI = Uri.parse("content://sms/inbox");
	        Cursor cur = getContentResolver().query(uriSMSURI, null, null, null, null);

	        while (cur.moveToNext()) 
	        {
	               //String address = cur.getString(cur.getColumnIndex("address"));
	               String body = cur.getString(cur.getColumnIndexOrThrow("body"));
	              sms.add(body);  
	        }
	        return sms;

	    }

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

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}

 

Output

GTU Android Program 13

Download Complete Code

Download