Friday, November 23, 2012

SMS application in android


File : res/layout/main.xml – A button only.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <Button
        android:id="@+id/buttonSend"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Send" />
 
</LinearLayout>
File : SendSMSActivity.java – Activity class to use build-in SMS intent to send out the SMS message.
package com.mkyong.android;
 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
 
public class SendSMSActivity extends Activity {
 
 Button buttonSend;
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
 
  buttonSend = (Button) findViewById(R.id.buttonSend);
 
  buttonSend.setOnClickListener(new OnClickListener() {
 
   @Override
   public void onClick(View v) {
 
    try {
 
         Intent sendIntent = new Intent(Intent.ACTION_VIEW);
         sendIntent.putExtra("sms_body", "default content"); 
         sendIntent.setType("vnd.android-dir/mms-sms");
         startActivity(sendIntent);
 
    } catch (Exception e) {
     Toast.makeText(getApplicationContext(),
      "SMS faild, please try again later!",
      Toast.LENGTH_LONG).show();
     e.printStackTrace();
    }
   }
  });
 }
}
See demo :
send sms via build-in sms application
send sms via build-in sms application

No comments:

Post a Comment