Friday, December 27, 2013
How to Fetch Incoming SMS in Android
Manchu Bhargavi
10:04 PM
android
,
android example in SMS
,
Android SMS from Inbox messages
,
fetch sms in android
,
imcoming SMS in andrroid example
5 comments
:
Hi,
This is an Example for Fetch the Inbox messages in phone and displayed in Listview using Android.
Uri to parse the particular location and Using the content resolver take the path from the content provider in phone.These two lines to fetch the messages from inbox
This is an Example for Fetch the Inbox messages in phone and displayed in Listview using Android.
Uri to parse the particular location and Using the content resolver take the path from the content provider in phone.These two lines to fetch the messages from inbox
Uri
sentURI = Uri.parse("content://sms/sent");
String[] reqCols = new String[] {
"_id", "address", "body" };
Source code
MainActivity.java:
package com.example.incomingsms;
import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
/*
* Activity is created
*
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
public class MainActivity extends Activity {
ListView mlistView;
ArrayList<Message> sms = new ArrayList<Message>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mlistView = (ListView) findViewById(R.id.listView1);
populateMessageList();
}
public void populateMessageList() {
fetchInboxMessages();
if (fetchInboxMessages() != null) {
mlistView.setAdapter(new datalist(MainActivity.this));
} else {
Toast.makeText(getApplicationContext(), "No SMS", 4).show();
}
}
/*
* Using this method to fetch the inbox messages
*/
public ArrayList<Message> fetchInboxMessages() {
Uri muriSms = Uri.parse("content://sms/inbox");
Cursor mcursor = getContentResolver().query(muriSms,
new String[] { "_id", "address", "date", "body" }, null, null,
null);
mcursor.moveToFirst();
while (mcursor.moveToNext()) {
Message mMessage = new Message();
mMessage.setmAddress(mcursor.getString(mcursor
.getColumnIndex("address")));
mMessage.setmBody(mcursor.getString(mcursor.getColumnIndex("body")));
mMessage.setmDate(mcursor.getString(mcursor.getColumnIndex("date")));
sms.add(mMessage);
}
return sms;
}
/*
* Set the Adapter for Listview
*
*/
class datalist extends BaseAdapter {
public datalist(MainActivity mainActivity) {
// TODO Auto-generated constructor stub
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return sms.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflator = getLayoutInflater();
View row;
row = inflator.inflate(R.layout.list_adapter, parent, false);
ImageView img1 = (ImageView) row.findViewById(R.id.imageView1);
TextView txt1 = (TextView) row.findViewById(R.id.textView1);
TextView txt2 = (TextView) row.findViewById(R.id.textView2);
Long timestamp = Long.parseLong(sms.get(position).getmDate());
Calendar mcalendar = Calendar.getInstance();
mcalendar.setTimeInMillis(timestamp);
DateFormat mformatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
txt1.setText(sms.get(position).getmBody());
txt2.setText("Sent by"+sms.get(position).getmAddress()+"\n"+mformatter.format(mcalendar.getTime()));
return row;
}
}
}
Message.java :
package com.example.incomingsms;
public class Message {
private String mAddress;
private String mBody;
private String mDate;
/*
* set the address
*
*/
public void setmAddress(String mAddress) {
this.mAddress = mAddress;
}
/*
* set the content in SMS
*
*/
public void setmBody(String mBody) {
this.mBody = mBody;
}
/*
* set the date
*
*/
public void setmDate(String mDate) {
this.mDate = mDate;
}
/*
* get the content
*
*/
public String getmBody() {
return mBody;
}
/*
* get the Address
*
*/
public String getmAddress() {
return mAddress;
}
/*
* get the Date
*
*/
public String getmDate() {
return mDate;
}
}
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"
tools:context=".MainActivity"
android:background="#000000">
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#FFFFFF">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="Inbox Messages"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold"
android:textColor="#000000"/>
</RelativeLayout>
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/relativeLayout1" >
</ListView>
</RelativeLayout>
ListAdapter.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="3dp"
android:background="@drawable/listselector" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="false"
android:layout_centerInParent="true"
android:contentDescription="@string/app_name"
android:padding="3dp"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="17dp"
android:layout_toRightOf="@+id/imageView1"
android:lines="1"
android:text="largetext"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_centerVertical="true"
android:text="TextView"
android:textColor="#FFFFFF"/>
</RelativeLayout>
</LinearLayout>
NOTE:
Using two Emulator we can sent the messages and retrive it .we can sent messages using internal messaging app to both Emulator
Here the output is shown below
Subscribe to:
Post Comments
(
Atom
)
Hiii madam, good evening
ReplyDeleteI am new to android ,,,,I am njyd this code very much but i got a problem how to delete particular row in this code and how to write code for contact listener to this same code....
Plz help me
This program is wrong. App crashes
ReplyDeletehow to delete selected multiple row in call log history and sms plz send me code
ReplyDeletei want all type of sms using this code
ReplyDeleteMsgclub allow you to send 1, 100, 1000000 or unlimited number of SMS with our bulk text SMS plugins.
ReplyDeleteSMS plugins