SendMessageActivity.java
package com.app.msg;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class SendMessageActivity extends Activity {
Button btnSend;
EditText etPhoneNo;
EditText etMsg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
etPhoneNo = (EditText) findViewById(R.id.phno);
etMsg = (EditText) findViewById(R.id.smstxt);
btnSend = (Button) findViewById(R.id.send);
btnSend.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String phoneNo = etPhoneNo.getText().toString();
String msg = etMsg.getText().toString();
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, msg, null,
null);
Toast.makeText(getApplicationContext(), "Message
Sent",
Toast.LENGTH_LONG).show();
} catch (Exception ex) {
Toast.makeText(getApplicationContext(),
ex.getMessage().toString(),
Toast.LENGTH_LONG).show();
ex.printStackTrace();
}
}
});
}
}
Main.xml
<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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/pn"
android:textSize="15sp"
/>
<EditText
android:id="@+id/phno"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
</EditText>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/msg"
android:textSize="15sp"
/>
<EditText
android:id="@+id/smstxt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:lines="5"
android:gravity="top" />
<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/b1" />
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.msg"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.SEND_SMS" />
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".SendMessageActivity"
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>
Output
package com.app.msg;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class SendMessageActivity extends Activity {
Button btnSend;
EditText etPhoneNo;
EditText etMsg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
etPhoneNo = (EditText) findViewById(R.id.phno);
etMsg = (EditText) findViewById(R.id.smstxt);
btnSend = (Button) findViewById(R.id.send);
btnSend.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String phoneNo = etPhoneNo.getText().toString();
String msg = etMsg.getText().toString();
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, msg, null,
null);
Toast.makeText(getApplicationContext(), "Message
Sent",
Toast.LENGTH_LONG).show();
} catch (Exception ex) {
Toast.makeText(getApplicationContext(),
ex.getMessage().toString(),
Toast.LENGTH_LONG).show();
ex.printStackTrace();
}
}
});
}
}
Main.xml
<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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/pn"
android:textSize="15sp"
/>
<EditText
android:id="@+id/phno"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
</EditText>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/msg"
android:textSize="15sp"
/>
<EditText
android:id="@+id/smstxt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:lines="5"
android:gravity="top" />
<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/b1" />
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.msg"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.SEND_SMS" />
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".SendMessageActivity"
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>
Output
To download follow this Link : SendMessage.zip
No comments:
Post a Comment