Users Online
· Guests Online: 97
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Newest Threads
No Threads created
Hottest Threads
No Threads created
Latest Articles
Articles Hierarchy
125 Java Android Program to Show SMS in Your Phone
Here is source code of the Program to Show SMS in Your Phone in Android. The program is successfully compiled and run on a Windows system using Eclipse Ide. The program output is also shown below.
Main Activity
package com.example.read_sms;
//insipired from www.stack overflow.com and www.vogella.com/android/
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends ListActivity {
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List List = new ArrayList();
Uri uri = Uri.parse("content://sms/inbox");
Cursor cur = getContentResolver().query(uri, null, null, null, null);
startManagingCursor(cur);
if (cur.moveToFirst()) {
for (int i = 0; i < cur.getCount(); i++) {
SmsFormat sms = new SmsFormat();
if (cur.getColumnIndexOrThrow("body") > -1) {
sms.setBody(cur
.getString(cur.getColumnIndexOrThrow("body"))
.toString());
}
if (cur.getColumnIndexOrThrow("address") > -1) {
sms.setNumber(cur.getString(
cur.getColumnIndexOrThrow("address")).toString());
}
String name = null;
name = Search(cur.getString(
cur.getColumnIndexOrThrow("address")).toString());
if (name != " ") {
sms.setName(name);
List.add(sms);
}
cur.moveToNext();
}
}
cur.close();
setListAdapter(new ListAdapter(this, List));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
SmsFormat sms = (SmsFormat) getListAdapter().getItem(position);
Log.i("MSG", sms.getBody());
}
public String Search(String number) {
Uri uri = Uri.withAppendedPath(
ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(number));
String name = " ";
ContentResolver contentResolver = getContentResolver();
Cursor contactLookup = contentResolver.query(uri, new String[] {
BaseColumns._ID, ContactsContract.PhoneLookup.DISPLAY_NAME },
null, null, null);
try {
if (contactLookup != null && contactLookup.getCount() > 0) {
contactLookup.moveToNext();
name = contactLookup.getString(contactLookup
.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
}
} finally {
if (contactLookup != null) {
contactLookup.close();
}
}
return name;
}
}
List Adapter
package com.example.read_sms;
import java.util.List;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class ListAdapter extends ArrayAdapter {
private final Context context;
private final List smslist;
public ListAdapter(Context context, List 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.textView1);
senderNumber.setText(smslist.get(position).getName() + "\n"
+ smslist.get(position).getNumber() + "\n" + "\n"
+ smslist.get(position).getBody());
return rowView;
}
}
Sms Format
package com.example.read_sms;
public class SmsFormat {
private String number;
private String body;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
}
Activity_Main
<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=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
Main Activity
package com.example.read_sms;
//insipired from www.stack overflow.com and www.vogella.com/android/
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends ListActivity {
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List
Uri uri = Uri.parse("content://sms/inbox");
Cursor cur = getContentResolver().query(uri, null, null, null, null);
startManagingCursor(cur);
if (cur.moveToFirst()) {
for (int i = 0; i < cur.getCount(); i++) {
SmsFormat sms = new SmsFormat();
if (cur.getColumnIndexOrThrow("body") > -1) {
sms.setBody(cur
.getString(cur.getColumnIndexOrThrow("body"))
.toString());
}
if (cur.getColumnIndexOrThrow("address") > -1) {
sms.setNumber(cur.getString(
cur.getColumnIndexOrThrow("address")).toString());
}
String name = null;
name = Search(cur.getString(
cur.getColumnIndexOrThrow("address")).toString());
if (name != " ") {
sms.setName(name);
List.add(sms);
}
cur.moveToNext();
}
}
cur.close();
setListAdapter(new ListAdapter(this, List));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
SmsFormat sms = (SmsFormat) getListAdapter().getItem(position);
Log.i("MSG", sms.getBody());
}
public String Search(String number) {
Uri uri = Uri.withAppendedPath(
ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(number));
String name = " ";
ContentResolver contentResolver = getContentResolver();
Cursor contactLookup = contentResolver.query(uri, new String[] {
BaseColumns._ID, ContactsContract.PhoneLookup.DISPLAY_NAME },
null, null, null);
try {
if (contactLookup != null && contactLookup.getCount() > 0) {
contactLookup.moveToNext();
name = contactLookup.getString(contactLookup
.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
}
} finally {
if (contactLookup != null) {
contactLookup.close();
}
}
return name;
}
}
List Adapter
package com.example.read_sms;
import java.util.List;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class ListAdapter extends ArrayAdapter
private final Context context;
private final List
public ListAdapter(Context context, List
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.textView1);
senderNumber.setText(smslist.get(position).getName() + "\n"
+ smslist.get(position).getNumber() + "\n" + "\n"
+ smslist.get(position).getBody());
return rowView;
}
}
Sms Format
package com.example.read_sms;
public class SmsFormat {
private String number;
private String body;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
}
Activity_Main
<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=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
Comments
No Comments have been Posted.
Post Comment
Please Login to Post a Comment.