Android Sms
In Android, you can use SmsManager API or devices Built-in SMS application to
send SMS's. In this tutorial, we shows you two basic examples to send SMS message
application or outside the application.
Let us start actual programming with Android Framework
Before you start writing your first example using Android SDK,
you have to make sure that you have set-up your Android development
environment properly as explained in Android
Create Android Application
1. The Application First Step Create a New Project Start project
2.Below Rename Project Name Ex:Send Sms
Click Next
3. After Spacific Minium sdk API 23 Select Lolipop 5.0
Click Next
4. Final Select Blank Activity And Click Finish
Click Next
5. Then Select MainActivity Click Finish
6. Then Open a Android Studio
MainActivity
package com.example.sendsms;
import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText mobileno,message;
Button sendsms;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mobileno=(EditText)findViewById(R.id.editText1);
message=(EditText)findViewById(R.id.editText2);
sendsms=(Button)findViewById(R.id.button1);
//Performing action on button click
sendsms.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String no=mobileno.getText().toString();
String msg=message.getText().toString();
//Getting intent and PendingIntent instance
Intent intent=new Intent(getApplicationContext(),MainActivity.class);
PendingIntent pi=PendingIntent.getActivity(getApplicationContext(), 0, intent,0);
//Get the SmsManager instance and call the sendTextMessage method to send message
SmsManager sms=SmsManager.getDefault();
sms.sendTextMessage(no, null, msg, pi,null);
Toast.makeText(getApplicationContext(), "Message Sent successfully!",
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.activity_main, menu);
return true;
}
}
package com.example.sendsms; import android.os.Bundle; import android.app.Activity; import android.app.PendingIntent; import android.content.Intent; import android.telephony.SmsManager; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { EditText mobileno,message; Button sendsms; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mobileno=(EditText)findViewById(R.id.editText1); message=(EditText)findViewById(R.id.editText2); sendsms=(Button)findViewById(R.id.button1); //Performing action on button click sendsms.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { String no=mobileno.getText().toString(); String msg=message.getText().toString(); //Getting intent and PendingIntent instance Intent intent=new Intent(getApplicationContext(),MainActivity.class); PendingIntent pi=PendingIntent.getActivity(getApplicationContext(), 0, intent,0); //Get the SmsManager instance and call the sendTextMessage method to send message SmsManager sms=SmsManager.getDefault(); sms.sendTextMessage(no, null, msg, pi,null); Toast.makeText(getApplicationContext(), "Message Sent successfully!", 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.activity_main, menu); return true; } }
Activity Main
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?
RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="20dp"
android:ems="10"/>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="26dp"
android:ems="10"
android:inputType="textMultiLine"/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText1"
android:layout_alignBottom="@+id/editText1"
android:layout_toLeftOf="@+id/editText1"
android:text="Mobile No:" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText2"
android:layout_alignBottom="@+id/editText2"
android:layout_alignLeft="@+id/textView1"
android:text="Message:" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_below="@+id/editText2"
android:layout_marginLeft="34dp"
android:layout_marginTop="48dp"
android:text="Send SMS" />
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"? RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginRight="20dp" android:ems="10"/> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText1" android:layout_below="@+id/editText1" android:layout_marginTop="26dp" android:ems="10" android:inputType="textMultiLine"/> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/editText1" android:layout_alignBottom="@+id/editText1" android:layout_toLeftOf="@+id/editText1" android:text="Mobile No:" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/editText2" android:layout_alignBottom="@+id/editText2" android:layout_alignLeft="@+id/textView1" android:text="Message:" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText2" android:layout_below="@+id/editText2" android:layout_marginLeft="34dp" android:layout_marginTop="48dp" android:text="Send SMS" />
Activity Mainifests.xml
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sms">
< uses-permission android:name="android.permission.SEND_SMS"/ >
< uses-permission android:name="android.permission.SEND_SMS"/ >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<< PREVIEW >>
<< NEXT >>
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"? <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.sms"> < uses-permission android:name="android.permission.SEND_SMS"/ > < uses-permission android:name="android.permission.SEND_SMS"/ > <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
<< PREVIEW >>
<< NEXT >>
No comments:
Post a Comment