Android Application Development in step by step info
Part 05
Android Sending SMS
There are following two ways to send SMS using Android device:
Using SmsManager to send SMS
Using Built-in Intent to send SMS
Using SmsManager to send SMS
The SmsManager manages SMS operations such as sending data to the given mobile device. You can create this object by calling the static methodSmsManager.getDefault() as follows:
SmsManager smsManager =SmsManager.getDefault();
Once you have SmsManager object, you can usesendDataMessage() method to send SMS at the specified mobile number as below:
smsManager.sendTextMessage("phoneNo", null, "SMS text", null, null);
Apart from the above method, there are few other important functions available in SmsManager class. These methods are listed below:
S.N.
Method & Description
1
ArrayList<String> divideMessage(String text)
This method divides a message text into several fragments, none bigger than the maximum SMS message size.
2
static SmsManager getDefault()
This method is used to get the default instance of the SmsManager
3
void sendDataMessage(StringdestinationAddress, String scAddress, shortdestinationPort, byte[] data, PendingIntentsentIntent, PendingIntent deliveryIntent)
This method is used to send a data based SMS to a specific application port.
4
void sendMultipartTextMessage(StringdestinationAddress, String scAddress,ArrayList<String> parts,ArrayList<PendingIntent> sentIntents,ArrayList<PendingIntent> deliveryIntents)
Send a multi-part text based SMS.
5
void sendTextMessage(StringdestinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntentdeliveryIntent)
Send a text based SMS.
Example
Following example shows you in practical how to useSmsManager object to send an SMS to the given mobile number.
To experiment with this example, you will need actual Mobile device equipped with latest Android OS, otherwise you will have to struggle with emulator which may not work.
Step
Description
1
You will use Eclipse IDE to create an Android application and name it as SendSMSDemo under a package com.example.sendsmsdemo. While creating this project, make sure you Target SDKand Compile With at the latest version of Android SDK to use higher levels of APIs.
2
Modify src/MainActivity.java file and add required code to take care of sending email.
3
Modify layout XML file res/layout/activity_main.xml add any GUI component if required. I'm adding a simple GUI to take mobile number and SMS text to be sent and a simple button to send SMS.
4
Modify res/values/strings.xml to define required constant values
5
Modify AndroidManifest.xml as shown below
6
Run the application to launch Android emulator and verify the result of the changes done in theaplication.
Following is the content of the modified main activity file src/com.example.sendsmsdemo/MainActivity.java.
package com.example.sendsmsdemo;
import android.os.Bundle;
import android.app.Activity;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
Button sendBtn;
EditText txtphoneNo;
EditText txtMessage;
@Override
protected void onCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendBtn = (Button)findViewById(R.id.btnSendSMS);
txtphoneNo = (EditText)findViewById(R.id.editTextPhoneNo);
txtMessage = (EditText)findViewById(R.id.editTextSMS);
sendBtn.setOnClickListener(newView.OnClickListener() {
public void onClick(View view) {
sendSMSMessage();
}
});
}
protected void sendSMSMessage() {
Log.i("Send SMS", "");
String phoneNo =txtphoneNo.getText().toString();
String message =txtMessage.getText().toString();
try {
SmsManager smsManager =SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, message, null, null);
Toast.makeText(getApplicationContext(), "SMS sent.",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again.",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
@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;
}
}
Following will be the content of res/layout/activity_main.xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textViewPhoneNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/phone_label" />
<EditText
android:id="@+id/editTextPhoneNo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="phone"/>
<TextView
android:id="@+id/textViewMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sms_label" />
<EditText
android:id="@+id/editTextSMS"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"/>
<Button android:id="@+id/btnSendSMS"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/send_sms_label"/>
</LinearLayout>
Following will be the content of res/values/strings.xml to define two new constants:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">SendSMSDemo</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="phone_label">Enter PhoneNumber:</string>
<string name="sms_label">Enter SMSMessage:</string>
<string name="send_sms_label">Send SMS</string>
</resources>
Following is the default content ofAndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sendsmsdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permissionandroid:name="android.permission.SEND_SMS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.sendsmsdemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<actionandroid:name="android.intent.action.MAIN" />
<categoryandroid:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Let's try to run your SendSMSDemo application. I assume you have connected your actual Android Mobile device with your computer. To run the app from Eclipse, open one of your project's activity files and click Run icon from the toolbar. Before starting your application, Eclipse will display following window to select an option where you want to run your Android application.
Select your mobile device as an option and then check your mobile device which will display following screen:
Now you can enter a desired mobile number and a text message to be sent on that number. Finally click on SedSMS button to send your SMS. Make sure your GSM connection is working fine to deliver your SMS to its recipient.
You can take a number of SMS separated by comma and then inside your program you will have to parse them into an array string and finally you can use a loop to send message to all the given numbers. That's how you can write your own SMS client. Next section will show you how to use existing SMS client to send SMS.
Using Built-in Intent to send SMS
You can use Android Intent to send SMS by calling built-in SMS functionality of the Android. Following section explains different parts of our Intent object required to send an SMS.
Intent Object - Action to send SMS
You will use ACTION_VIEW action to launch an SMS client installed on your Android device. Following is simple syntax to create an intent with ACTION_VIEW action
Intent smsIntent = newIntent(Intent.ACTION_VIEW);
Intent Object - Data/Type to send SMS
To send an SMS you need to specify smsto: as URI using setData() method and data type will be tovnd.android-dir/mms-sms using setType() method as follows:
smsIntent.setData(Uri.parse("smsto:"));
smsIntent.setType("vnd.android-dir/mms-sms");
Intent Object - Extra to send SMS
Android has built-in support to add phone number and text message to send an SMS as follows:
smsIntent.putExtra("address" , new String("0123456789;3393993300"));
smsIntent.putExtra("sms_body" , "Test SMS to Angilla");
Here address and sms_body are case sensitive and should be specified in small characters only. You can specify more than one number in single string but separated by semi-colon (;).
Example
Following example shows you in practical how to use Intent object to launch SMS client to send an SMS to the given recipients.
To experiment with this example, you will need actual Mobile device equipped with latest Android OS, otherwise you will have to struggle with emulator which may not work.
Step
Description
1
You will use Eclipse IDE to create an Android application and name it as SendSMSDemo under a package com.example.sendsmsdemo. While creating this project, make sure you Target SDKand Compile With at the latest version of Android SDK to use higher levels of APIs.
2
Modify src/MainActivity.java file and add required code to take care of sending SMS.
3
Modify layout XML file res/layout/activity_main.xml add any GUI component if required. I'm adding a simple button to launch SMS Client.
4
Modify res/values/strings.xml to define required constant values
5
Modify AndroidManifest.xml as shown below
6
Run the application to launch Android emulator and verify the result of the changes done in theaplication.
Following is the content of the modified main activity file src/com.example.sendsmsdemo/MainActivity.java.
package com.example.sendsmsdemo;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
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(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startBtn = (Button)findViewById(R.id.sendSMS);
startBtn.setOnClickListener(newView.OnClickListener() {
public void onClick(View view) {
sendSMS();
}
});
}
protected void sendSMS() {
Log.i("Send SMS", "");
Intent smsIntent = newIntent(Intent.ACTION_VIEW);
smsIntent.setData(Uri.parse("smsto:"));
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address" , new String ("0123456789"));
smsIntent.putExtra("sms_body" , "Test SMS to Angilla");
try {
startActivity(smsIntent);
finish();
Log.i("Finished sending SMS...", "");
} catch (android.content.ActivityNotFoundExceptionex) {
Toast.makeText(MainActivity.this,
"SMS faild, please try again later.", Toast.LENGTH_SHORT).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;
}
}
Following will be the content of res/layout/activity_main.xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button android:id="@+id/sendSMS"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/compose_sms"/>
</LinearLayout>
Following will be the content of res/values/strings.xml to define two new constants:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">SendSMSDemo</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="compose_sms">Compose SMS</string>
</resources>
Following is the default content ofAndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sendsmsdemo"
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.example.sendsmsdemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<actionandroid:name="android.intent.action.MAIN" />
<categoryandroid:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Let's try to run your SendSMSDemo application. I assume you have connected your actual Android Mobile device with your computer. To run the app from Eclipse, open one of your project's activity files and click Run icon from the toolbar. Before starting your application, Eclipse will display following window to select an option where you want to run your Android application.
Select your mobile device as an option and then check your mobile device which will display following screen:
Now use Compose SMS button to launch Android built-in SMS clients which is shown below
You can modify either of the given default fields and finally use send SMS button (marked with red rectangle) to send your SMS to the mentioned recipient.
Android Phone Calls
As such every Android Device especially Mobile phone is meant to provide a functionality to make a phone call but still you may need to write an application where you want to give an option to your user to make a call using a hard coded phone number.
This chapter lists down all the simple steps to create an application which can be used to make a Phone Call. You can use Android Intent to make phone call by calling built-in Phone Call functionality of the Android. Following section explains different parts of our Intent object required to make a call.
Intent Object - Action to make Phone Call
You will use ACTION_CALL action to trigger built-in phone call functionality available in Android device. Following is simple syntax to create an intent with ACTION_CALL action
Intent phoneIntent = newIntent(Intent.ACTION_CALL);
You can use ACTION_DIAL action instead of ACTION_CALL, in that case you will have option to modify hardcoded phone number before making a call instead of making a direct call.
Intent Object - Data/Type to make Phone Call
To make a phone call at a given number 91-800-001-0101, you need to specify tel: as URI using setData() method as follows:
phoneIntent.setData(Uri.parse("tel:91-800-001-0101"));
The interesting point is that, to make a phone call, you do not need to specify any extra data or data type.
Example
Following example shows you in practical how to use Android Intent to make phone call to the given mobile number.
To experiment with this example, you will need actual Mobile device equipped with latest Android OS, otherwise you will have to struggle with emulator which may not work.
Step
Description
1
You will use Eclipse IDE to create an Android application and name it as PhoneCallDemounder a package com.example.phonecalldemo. While creating this project, make sure youTarget SDK and Compile With at the latest version of Android SDK to use higher levels of APIs.
2
Modify src/MainActivity.java file and add required code to take care of making a call.
3
Modify layout XML file res/layout/activity_main.xml add any GUI component if required. I'm adding a simple button to Call 91-800-001-0101 number
4
Modify res/values/strings.xml to define required constant values
5
Modify AndroidManifest.xml as shown below
6
Run the application to launch Android emulator and verify the result of the changes done in theaplication.
Following is the content of the modified main activity file src/com.example.phonecalldemo/MainActivity.java.
package com.example.phonecalldemo;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
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(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startBtn = (Button)findViewById(R.id.makeCall);
startBtn.setOnClickListener(newView.OnClickListener() {
public void onClick(View view) {
makeCall();
}
});
}
protected void makeCall() {
Log.i("Make call", "");
Intent phoneIntent = newIntent(Intent.ACTION_CALL);
phoneIntent.setData(Uri.parse("tel:91-800-001-0101"));
try {
startActivity(phoneIntent);
finish();
Log.i("Finished making a call...", "");
} catch (android.content.ActivityNotFoundExceptionex) {
Toast.makeText(MainActivity.this,
"Call faild, please try again later.", Toast.LENGTH_SHORT).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;
}
}
Following will be the content of res/layout/activity_main.xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button android:id="@+id/makeCall"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/make_call"/>
</LinearLayout>
Following will be the content of res/values/strings.xml to define two new constants:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">PhoneCallDemo</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="make_call">Call 91-916-571-3436</string>
</resources>
Following is the default content ofAndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.phonecalldemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permissionandroid:name="android.permission.CALL_PHONE" />
<uses-permissionandroid:name="android.permission.READ_PHONE_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.phonecalldemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<actionandroid:name="android.intent.action.MAIN" />
<categoryandroid:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Let's try to run your PhoneCallDemo application. I assume you have connected your actual Android Mobile device with your computer. To run the app from Eclipse, open one of your project's activity files and click Run icon from the toolbar. Before starting your application, Eclipse will display following window to select an option where you want to run your Android application.
Selct your mobile device as an option and then check your mobile device which will display following screen:
Now use Call 91704950-1770 button to make phone call as shown below:
Android Mobile Call Progress
Publishing Android Application
Android application publishing is a process that makes your Android applications available to users. Infact, publishing is the last phase of the Android application development process.
Once you developed and fully tested your Android Application, you can stat selling or distributing free using Google Play (A famous Android marketplace). You can also release your applications by sending them directly to users or by letting users download them from your own website.
You can check a detailed publishing process at Android official website, but this tutorial will take you through simple steps to launch your application on Google Play. Here is a simplified check list which will help you in launching your Android application:
Step
Activity
1
Regression Testing Before you publish your application, you need to make sure that itsmeeting the basic quality expectations for all Android apps, on all of the devices that you are targeting. So perform all the required testing on different devices including phone and tablets.
2
Application Rating When you will publish your application at Google Play, you will have to specify a content rating for your app, which informs Google Play users of its maturity level. Currently available ratings are (a) Everyone (b) Low maturity (c) Medium maturity (d) High maturity.
3
Targeted Regions Google Play lets you control what countries and territories where your application will be sold. Accordingly you must take care of setting up time zone, localization or any other specific requirement as per the targeted region.
4
Application Size Currently, the maximum size for an APK published on Google Play is 50 MB. If your app exceeds that size, or if you want to offer a secondary download, you can use APK Expansion Files, which Google Play will host for free on its server infrastructure and automatically handle the download to devices.
5
SDK and Screen Compatibility It is important to make sure that your app is designed to run properly on the Android platform versions and device screen sizes that you want to target.
6
Application Pricing Deciding whether you app will be free or paid is important because, on Google Play, free apps must remain free. If you want to sell your application then you will have to specify its price in different currencies.
7
Promotional Content It is a good marketing practice to supply a variety of high-quality graphic assets to showcase your app or brand. After you publish, these appear on your product details page, in store listings and search results, and elsewhere.
8
Build and Upload release-ready APK The release-ready APK is what you you will upload to the Developer Console and distribute to users. You can check complete detail on how to create a release-ready version of your app: Preparing for Release.
9
Finalize Application Detail Google Play gives you a variety of ways to promote your app and engage with users on your product details page, from colorful graphics, screenshots, and videos to localized descriptions, release details, and links to your other apps. So you can decorate your application page and provide as much as clear crisp detail you can provide.
Export Android Application
You will need to export your application as an APK (Android Package) file before you upload it Google Play marketplace.
To export an application, just open that application project in Eclipse and select File->Export from your Eclipse and follow the simple steps to export your application:
Next select, Export Android Application option as shown in the above screen shot and then click Nextand again Next so that you get following screen where you will choose Create new keystore to store your application.
Enter your password to protect your application and click on Next button once again. It will display followigscreen to let you create a key for your application:
Once you filled up all the information, click Nextbutton and finally it will ask you a location where Application will be exported:
Finally, you click on Finish button to generate your Android Application Package File which will be uploaded at Google Play marketplace.
Google Play Registration
The most important step is to register with Google Play using Google Play Marketplace. You can use your existing google ID if you have any otherwise you can create a new Google ID and then register with the marketplace. You will have following screen to accept terms and condition.
You can use Continue to payment button to proceed to make a payment of $25 as a registration fee and finally to complete your account detail.
Once you are a registered user at Google Play, you can upload release-ready APK for your application and finally you will complete application detail using application detail page as mentioned in step 9 of the above mentioned checklist.
Comments
Post a Comment