Android Software Engineer Question: Download Android Software Engineer PDF

Explain sending SMS messages programmatically?

Tweet Share WhatsApp

Answer:

Take a button on activity_main.xml file as follows.

<Button android:id="@+id/btnSendSMS" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:onClick="sendmySMS" android:text="sendSMS" />

According to above code when user clicks the button sendmySMS method will be called. sendmySMS is user defined method.

In the AndroidManifest.xml file, add the following statements

<uses-permissionandroid:name="android.permission.SEND_SMS"/>

Now we write the final step. Write the given below method in MainActivity,java file

publicvoidsendmySMS(View v)
{
SmsManagersms = SmsManager.getDefault();
sms.sendTextMessage("5556", null, "Hello from google", null, null);
}
In this example I have used two emulator. On the first Android emulator (5554), click the Send SMSbutton to send an SMS message to the second emulator(5556).

Download Android Software Engineer PDF Read All 62 Android Software Engineer Questions
Previous QuestionNext Question
Explain activity creator in Android?Explain how to use built-in messaging within the application?