Read Sms in android.
package example.readsms;
import java.net.URI;
import java.util.ArrayList;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.telephony.SmsMessage;
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;
import android.widget.Toast;
public class MainActivity extends Activity {
public static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
ArrayList<String> list;
ArrayAdapter<String> adapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv=(ListView) findViewById(R.id.smslist);
if(inbox()!=null)
{
adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,inbox());
lv.setAdapter(adapter);
IntentFilter filter = new IntentFilter(SMS_RECEIVED);
registerReceiver(receiver_SMS, filter);
}
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String addrs=list.get(arg2).toString();
Toast.makeText(getApplicationContext(), addrs, Toast.LENGTH_LONG).show();
}
});
}
public ArrayList<String> inbox()
{
list=new ArrayList<String>();
Uri uri=Uri.parse("content://sms/inbox");
Cursor cursor=getContentResolver().query(uri, new String[]{"_id","address","date","body"}, null, null, null);
cursor.moveToFirst();
while(cursor.moveToNext())
{
String address=cursor.getString(1);
String body=cursor.getString(3);
list.add(address+""+body);
}
return list;
}
@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 boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId()) {
case R.id.item1:
Intent close = new Intent(getApplicationContext(),SendSMSActivity.class);
startActivity(close);
return true;
case R.id.item2:
Toast.makeText(getApplicationContext(),"Item 2 Selected",Toast.LENGTH_LONG).show();
return true;
case R.id.item3:
Toast.makeText(getApplicationContext(),"Item 3 Selected",Toast.LENGTH_LONG).show();
return true;
case R.id.item4:
return true;
// finish();
case R.id.item5:
// finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
BroadcastReceiver receiver_SMS = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(SMS_RECEIVED))
{
Bundle bundle=intent.getExtras();
if(bundle!=null)
{
Object[] pdus=(Object[])bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++)
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
for (SmsMessage message : messages)
{
// Toast.makeText(MainActivity.this,"Address-"+message.getOriginatingAddress()+ "Sms-"+message.getDisplayMessageBody(), Toast.LENGTH_LONG).show();
receivedMessage(message.getDisplayOriginatingAddress());
}
}
}
}
};
private void receivedMessage(String message)
{
list.add(message);
adapter.notifyDataSetChanged();
}
}
import java.net.URI;
import java.util.ArrayList;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.telephony.SmsMessage;
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;
import android.widget.Toast;
public class MainActivity extends Activity {
public static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
ArrayList<String> list;
ArrayAdapter<String> adapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv=(ListView) findViewById(R.id.smslist);
if(inbox()!=null)
{
adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,inbox());
lv.setAdapter(adapter);
IntentFilter filter = new IntentFilter(SMS_RECEIVED);
registerReceiver(receiver_SMS, filter);
}
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String addrs=list.get(arg2).toString();
Toast.makeText(getApplicationContext(), addrs, Toast.LENGTH_LONG).show();
}
});
}
public ArrayList<String> inbox()
{
list=new ArrayList<String>();
Uri uri=Uri.parse("content://sms/inbox");
Cursor cursor=getContentResolver().query(uri, new String[]{"_id","address","date","body"}, null, null, null);
cursor.moveToFirst();
while(cursor.moveToNext())
{
String address=cursor.getString(1);
String body=cursor.getString(3);
list.add(address+""+body);
}
return list;
}
@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 boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId()) {
case R.id.item1:
Intent close = new Intent(getApplicationContext(),SendSMSActivity.class);
startActivity(close);
return true;
case R.id.item2:
Toast.makeText(getApplicationContext(),"Item 2 Selected",Toast.LENGTH_LONG).show();
return true;
case R.id.item3:
Toast.makeText(getApplicationContext(),"Item 3 Selected",Toast.LENGTH_LONG).show();
return true;
case R.id.item4:
return true;
// finish();
case R.id.item5:
// finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
BroadcastReceiver receiver_SMS = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(SMS_RECEIVED))
{
Bundle bundle=intent.getExtras();
if(bundle!=null)
{
Object[] pdus=(Object[])bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++)
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
for (SmsMessage message : messages)
{
// Toast.makeText(MainActivity.this,"Address-"+message.getOriginatingAddress()+ "Sms-"+message.getDisplayMessageBody(), Toast.LENGTH_LONG).show();
receivedMessage(message.getDisplayOriginatingAddress());
}
}
}
}
};
private void receivedMessage(String message)
{
list.add(message);
adapter.notifyDataSetChanged();
}
}
activity-main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<ListView
android:id="@+id/smslist"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textViewPhoneNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Phone Number : "
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editTextPhoneNo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:phoneNumber="true" >
</EditText>
<Button
android:id="@+id/buttonSend"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Send" />
</LinearLayout>