Thursday, 21 March 2013

How to send a sms

Sending a sms is a process of containing the methods  that mean if one can send a sms is not easy. Here send a sms is the process of explaing the present situation and we may call it as the neccessity of the situation .But we are gaving the process made easy in android application devlopers for devloping an app with the sms requirement of the users. So, the following code will need you.

     SENDING A SMS IN ANDROID:

Write the following code in your application:
   use this code for send sms:
 package com.sending;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
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 SendingSmsActivity extends Activity implements OnClickListener {
EditText phnu,msg;
Button send;
String phone,msgg;
PendingIntent pi_sent,pi_deliver;
int vijju=511;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        phnu=(EditText)findViewById(R.id.editText1);
        msg=(EditText)findViewById(R.id.editText2);
        send=(Button)findViewById(R.id.button1);
        send.setOnClickListener(this);
    }
public void onClick(View v) {
// TODO Auto-generated method stub
phone=phnu.getText().toString();
msgg=msg.getText().toString();
SmsManager sms=SmsManager.getDefault();
sms.sendTextMessage(phone, null, msgg, pi_sent, pi_deliver);
String SENT="SMS_SENT";
String Deliver="Deliver_sms";
pi_sent=PendingIntent.getBroadcast(this, sri, new Intent(SENT), 0);
pi_deliver=PendingIntent.getBroadcast(this, sri, new Intent(Deliver), 0);
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
switch (sri) {
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(context, "sms not sent", 30).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(context, "no service", 30).show();
break;

case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(context, "stop radio", 30).show();
break;
case Activity.RESULT_OK:
Toast.makeText(context, "sms sent successfully", 30).show();
break;


default:
break;
}
}
}, new IntentFilter(SENT));
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
switch (vijju) {
case Activity.RESULT_OK:
Toast.makeText(context, "sms delivered", 30).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(context, " delivery faill", 30).show();

default:
break;
}
}
}, new IntentFilter(Deliver));
}
}




in your .xml is shuld be following:


<?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:background="#d5ff00"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText1"
        android:hint="phone number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:hint="type your text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.31" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        
        android:layout_height="wrap_content"
        android:text="send" />

</LinearLayout>

No comments:

Post a Comment