I tried this code in my android application for the SMS message but it is not working , the application does not appear in the messaging list. Should I add something to make
I am providing you a detailed desc to do that in different case(with contacts, text shares etc).
Manifest Entry for you Message Activity
<!-- Defines also the app name in the Android menu -->
<activity
android:name="it.rainbowbreeze.smsforfree.ui.ActSendSms"
android:label="@string/common_appName"
>
<!-- Sends sms for someone -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.SENDTO" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="sms" />
<data android:scheme="smsto" />
</intent-filter>
<!-- Sends text to someone .This will enable any Text Share functionality-->
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
Now we have made a processIntentData method as shown below to be applied in Message Activity:
private void processIntentData(Intent intent)
{
if (null == intent) return;
if (Intent.ACTION_SENDTO.equals(intent.getAction())) {
//in the data i'll find the number of the destination
String destionationNumber = intent.getDataString();
destionationNumber = URLDecoder.decode(destionationNumber);
//clear the string
destionationNumber = destionationNumber.replace("-", "")
.replace("smsto:", "")
.replace("sms:", "");
//and set fields
mTxtDestination.setText(destionationNumber);
} else if (Intent.ACTION_SEND.equals(intent.getAction()) && "text/plain".equals(intent.getType())) {
//in the data i'll find the content of the message
String message = intent.getStringExtra(Intent.EXTRA_TEXT);
//clear the string
mTxtBody.setText(message);
}
}
Use as shown in Message Activity:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
mTxtDestination = (EditText) findViewById(R.id.actsendsms_txtDestination);
mTxtBody = (EditText) findViewById(R.id.actsendsms_txtMessage);
...
//executed when the application first runs
if (null == savedInstanceState) {
processIntentData(getIntent());
}
}
The attached snap for results:
Try this code to send SMS, In your activity manifiest file grand android.permission.SEND_SMS permission
.
Main.xml
<?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 "
/>
<Button
android:id="@+id/sendsms_intent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" Send SMS using Intent.ACTION_SENDTO "
/>
</LinearLayout>
Now the Activity class is,AndroidSMS.java
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
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);
Button buttonSendSms_intent = (Button)findViewById(R.id.sendsms_intent);
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);
}});
buttonSendSms_intent.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String smsNumber = edittextSmsNumber.getText().toString();
String smsText = edittextSmsText.getText().toString();
Uri uri = Uri.parse("smsto:" + smsNumber);
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra("sms_body", smsText);
startActivity(intent);
}});
}
}