Monday, September 10, 2012

SMS, Messaging in Android

Hello Friends!

Today I am going to share very simple code for message sending in android.


1-Print screen

2-manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.manish.sms"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />

<uses-permission android:name="android.permission.SEND_SMS" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".SendSmsDemoActivity"
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>

3-SmsSendDemoActivity.java

package com.manish.sms;

import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SendSmsDemoActivity extends Activity {
EditText editTextNumber;
EditText editTextMessage;
Button buttonSend;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editTextNumber = (EditText) findViewById(R.id.editTextNumber);
editTextMessage = (EditText) findViewById(R.id.editTextMessage);
buttonSend = (Button) findViewById(R.id.buttonSend);
buttonSend.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String phoneNumber = editTextNumber.getText().toString();
String message = editTextMessage.getText().toString();

try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, message, null, null);
Toast.makeText(getApplicationContext(), "SMS Sent!",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again later!",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
});
}
}

4-main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<EditText
android:id="@+id/editTextNumber"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Phone-number" />

<EditText
android:id="@+id/editTextMessage"
android:layout_width="fill_parent"
android:layout_height="100sp"
android:hint="Type your Message here!" />

<Button
android:id="@+id/buttonSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send" />

</LinearLayout>

4 comments :

  1. i would like to send to a predefined phone number. Where do i set it? layout= hint? or in my activity(where/how)

    Tx for help

    ReplyDelete
    Replies
    1. no no you can't set it into layout hint. you should use it in your activity.
      else you want always use same number then fix it into textview..

      Thanks,

      Delete
  2. Does sending SMS through app requires any sms pack ?

    ReplyDelete
    Replies
    1. Yes Sajal, it needed sms pack if you don't have it will deduct from your main balance.

      Delete

Note: Only a member of this blog may post a comment.