Thursday, 27 August 2015

Android Programming Tutorials 23 : Sending SMS In Android

SMS:

In this Tutorial we are discussing how SMS is send Through Android Phone.


OUT PUT Of The Tutorial:





Create Android Application:

  • File >> New >> Android Application
  • Enter Project name: SMSApp
  • Package: com.ambilpursunil.newapp
  • Keep other default selections, click Next until you reach Finish

                                   

     1.Simply Copy and Past the Code which is display below in the  MainActivity.java 


    package com.ambilpursunil.newapp;

    import android.app.Activity;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;

    public class MainActivity extends Activity {

                          @Override
                 protected void onCreate(Bundle savedInstanceState) {
                            super.onCreate(savedInstanceState);
                            setContentView(R.layout.activity_main);
       Button sendButton = (Button) findViewById(R.id.bsendSMS);
       sendButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                // TODO Auto-generated method stub
                sendMobileSMS();
                }
                });
                }
                private void sendMobileSMS() {
                // TODO Auto-generated method stub
                Log.i("send SMS.","");
                Intent smsIntent = new Intent(Intent.ACTION_VIEW);
                smsIntent.setData(Uri.parse("smsto."));
                smsIntent.setType("vnd.android-dir/mms-sms");
                smsIntent.putExtra("address",new String("0000 503 000"));
                smsIntent.putExtra("sms_body", "Testing SMS");
                try{
                startActivity(smsIntent);
                finish();
                Log.i("Finish Sending SMS...", "");
                }catch(android.content.ActivityNotFoundException ex){
    Toast.makeText(MainActivity.this, "SMS Failed Please Try Again",                                                 Toast.LENGTH_LONG).show();
                }
                }
                @Override
                public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.main, menu);
                return true;
                }
      }

    2.Simple Copy and Past the below code:  actvitiy_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"
        android:background="@drawable/india"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >

        <Button
            android:id="@+id/bsendSMS"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="126dp"
            android:text="@string/send_sms"
            android:textColor="#3B0B0B"
            android:textSize="20dp"/>
       
    </RelativeLayout>

    Note: Place any Image in drawable folde, Here i placed india.jpg image.

    3.Copy and past the code for manifest.xml

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

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

        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.ambilpursunil.newapp.MainActivity"
                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>

    4.Right click on the project and Run As-->Android Application


    OUT PUT Of The Tutorial:



    OutPut:

    When our application launch click on the  Send SMS Button Then it will redirected to our predefined Messaging option of our mobile edit the phone no and text clicl on send. 

    Please Send Your Comments To ambilpura.sunil@gmail.com

    Stay Tune For Next Tutorial... Camera In Android:

    No comments:

    Post a Comment