Modify main.xml to have two EditText for user to enter phone number and text.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter Phone Number:"
/>
<EditText
android:id="@+id/smsnumber"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="phone"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter Phone SMS Text:"
/>
<EditText
android:id="@+id/smstext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/sendsms"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" Send SMS "
/>
</LinearLayout>
main code, AndroidSMS.java
package com.exercise.AndroidSMS;
import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class AndroidSMS extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText edittextSmsNumber = (EditText)findViewById(R.id.smsnumber);
final EditText edittextSmsText = (EditText)findViewById(R.id.smstext);
Button buttonSendSms = (Button)findViewById(R.id.sendsms);
buttonSendSms.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
SmsManager smsManager = SmsManager.getDefault();
String smsNumber = edittextSmsNumber.getText().toString();
String smsText = edittextSmsText.getText().toString();
smsManager.sendTextMessage(smsNumber, null, smsText, null, null);
}});
}
}
Modify AndroidManifest.xml to grant permission of "android.permission.SEND_SMS".
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exercise.AndroidSMS"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AndroidSMS"
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>
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
</manifest>
Download the files.
Related Articles:
- Send SMS using Intent.ACTION_SENDTO
- Send SMS in Alarm Service at a pre-defined time
2 comments:
keep doing, whatever u r doing its helpful; thanks.
Great post sir :)
But I have a problem. When i tried your code, it is fully working with no error. When I try to click send and wait for a couple second, I can't received any message. Why is like that?
Thanks :D
Post a Comment