Srikanth Technologies

Send Message To Selected Contacts

This application first displays person name and mobile number from contacts of the device. User can enter message text and select contacts to whom the message is to be sent.

This application uses the following features of Android.

Steps

Here are the steps and code related to this application.
  1. First create a new Android project that supports API 8 or above. I named it as SendMessage
  2. Create the following classes:

    Contact.java

    
    package com.st.messages;
    
    public class Contact {
    	private String name;
    	private String mobile;
    
    	public Contact(String name, String mobile) {
    		super();
    		this.name = name;
    		this.mobile = mobile;
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public String getMobile() {
    		return mobile;
    	}
    
    	public void setMobile(String mobile) {
    		this.mobile = mobile;
    	}
    
    }
    
    

    ContactsAdapter.java

    package com.st.messages;
    
    import java.util.ArrayList;
    
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.TextView;
    
    public class ContactsAdapter extends BaseAdapter {
    		private Context context;
    		private ArrayList<Contact> contacts;
    	 
    		public ContactsAdapter(Context context, ArrayList<Contact> contacts) {
    			this.context = context;
    			this.contacts = contacts;
    		}
    	 
    		public View getView(int position, View convertView, ViewGroup parent) {
    	 
    			LayoutInflater inflater = (LayoutInflater) context
    				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    	 
    			View gridView;
    	 
    			if (convertView == null) {
    	 
    				gridView = new View(context);
    	 
    				// get layout from contact.xml
    				gridView = inflater.inflate(R.layout.contact, null);
    	 
    				TextView textName = (TextView) gridView.findViewById(R.id.name);
    				textName.setText( contacts.get(position).getName());
    				
    				TextView textMobile = (TextView) gridView.findViewById(R.id.mobile);
    				textMobile.setText( contacts.get(position).getMobile());
    	 
    			} else {
    				gridView = (View) convertView;
    			}
    	 
    			return gridView;
    		}
    	 
    		@Override
    		public int getCount() {
    			return contacts.size();
    		}
    	 
    		@Override
    		public Object getItem(int position) {
    			return null;
    		}
    	 
    		@Override
    		public long getItemId(int position) {
    			return 0;
    		}
    }
    
    

    SendMessagesActivity.java

    package com.st.messages;
    import java.util.ArrayList;
    import android.app.Activity;
    import android.app.ProgressDialog;
    import android.database.Cursor;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.provider.ContactsContract;
    import android.provider.ContactsContract.CommonDataKinds.Phone;
    import android.provider.ContactsContract.Contacts;
    import android.provider.ContactsContract.Data;
    import android.telephony.SmsManager;
    import android.util.Log;
    import android.view.View;
    import android.widget.CheckBox;
    import android.widget.EditText;
    import android.widget.ListView;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class SendMessagesActivity extends Activity {
    	ListView listView;
    	EditText editMessage;
    	ProgressDialog progressDialog;
    	Handler progresshandler;
    
    	@Override
    	public void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.listcontacts);
    		listView = (ListView) findViewById(R.id.contactsView);
    		editMessage = (EditText) findViewById(R.id.editMessage);
    
    		Cursor c = this.managedQuery(ContactsContract.Data.CONTENT_URI, null,
    			Data.MIMETYPE + "=?", // condition
    	        new String[] { ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE }, // value
    			null);
    
    		ArrayList<Contact> contacts = new ArrayList<Contact>();
    
    		while (c.moveToNext()) {
    			int type = c.getInt(c.getColumnIndex(Phone.TYPE));
    			if (type == Phone.TYPE_MOBILE) {
    				Contact con = new Contact(c.getString(c
    						.getColumnIndex(Contacts.DISPLAY_NAME)), c.getString(c
    						.getColumnIndex(Phone.NUMBER)));
    				contacts.add(con);
    			}
    		}
    		listView.setAdapter(new ContactsAdapter(this, contacts));
    
    		progressDialog = new ProgressDialog(this);
    		progressDialog.setMessage("Sending Messages.. Please wait!");
    
    		progresshandler = new Handler() {
    			public void handleMessage(Message msg) {
    				progressDialog.dismiss();
    				Toast.makeText(SendMessagesActivity.this, "Messages Sent",
    						Toast.LENGTH_LONG).show();
    			}
    		};
    	}
    
    	class SendMessagesThread extends Thread {
    		Handler handler;
    
    		public SendMessagesThread(Handler handler) {
    			this.handler = handler;
    		}
    
    		public void run() {
    			SmsManager smsManager = SmsManager.getDefault();
    			// Find out which contacts are selected
    			for (int i = 0; i < listView.getCount(); i++) {
    				View item = (View) listView.getChildAt(i);
    				boolean selected = ((CheckBox) item.findViewById(R.id.selected)).isChecked();
    				if (selected) {
    					String mobile = ((TextView) item.findViewById(R.id.mobile)).getText().toString();
    					try {
    						smsManager.sendTextMessage(mobile, null, editMessage.getText().toString(), null, null);
    					} catch (Exception ex) {
    						Log.d("Mobile", "Could not send message to " + mobile);
    					}
    				}
    			}
    			Message m = handler.obtainMessage();
    			handler.sendMessage(m);
    		} // run
    	} // Thread
    
    	public void sendMessages(View v) {
    		if (editMessage.getText().toString().length() > 0) {
    			SendMessagesThread thread = new SendMessagesThread(progresshandler);
    			thread.start();
    			progressDialog.show();
    		} else {
    			Toast.makeText(this, "Please enter message!", Toast.LENGTH_LONG)
    					.show();
    		}
    	}
    }
    

    res/layout/contact.xml

    <?xml version="1.0" encoding="utf-8"?>
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:stretchColumns="1">
        
        <TableRow>
          <CheckBox android:id="@+id/selected" 
               android:layout_width="wrap_content"
               android:layout_height="wrap_content" />  
          <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </TextView>    
        
        <TextView
            android:id="@+id/mobile"
            android:layout_width="100dp"
            android:layout_height="wrap_content" >
        </TextView>
        
        </TableRow>
     
    </TableLayout>
    

    res/layout/listcontacts.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="vertical" >
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Enter Message Text:" />
        
    
        <EditText
            android:id="@+id/editMessage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:singleLine="false" />
    
        <Button android:id="@+id/btnSend"
            android:onClick="sendMessages"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Send Message" />
        <ListView
            android:id="@+id/contactsView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </ListView>
    </LinearLayout>
    

    Maninfest file is as follows : AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.st.messages"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk android:minSdkVersion="8" />
        <uses-permission android:name="android.permission.READ_CONTACTS" />
         <uses-permission android:name="android.permission.SEND_SMS" />
        
        <application
            android:icon="@drawable/icon"
            android:label="@string/app_name" >
            <activity
                android:name=".SendMessagesActivity"
                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>
    

    res/values/strings.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="app_name">Send Message</string>
    </resources>
    
  3. Place icon.jpg in res/drawable folder
  4. Build and run the project using Emulator that supports API 8 or highter
  5. Enter text to be sent and select contacts to whom you want to send message. Click on Send Message button to send message to selected mobile numbers.

Related Resources

Use the following links to download .APK file of this application.

Download SendMessage.apk and install in your device.


Post Your Comment

Image

Enter the code given in the above image :
Enter Your Name :
Enter Your Email Address :
Enter Your Comment :

Comments


Posted By narendra On 18-Aug-12 09:53:28 PM

awesome cool app sir y dont you place it android market



Posted By Bhagyashree Barmukh On 05-Feb-13 10:58:42 AM

I like ur solution... nd very helpful 2 me



Posted By wAAS On 14-Jun-13 10:45:16 AM

here i am getting repeated names n numbers of contacts and i am able to get all contacts..please give me the solution