试试这个方法:
要发送短信,请使用 SMS Manager 中的 sendTextMessage。
sendTextMessage(String destinationAddress, String scAddress, String text,PendingIntent sentIntent, PendingIntent deliveryIntent)
第一个 Pending Intent 参数 (sentIntent) 在消息发送成功或失败时触发。接收此 Intent 的广播接收器的结果代码将是以下之一。
Activity.RESULT_OK - To indicate a successful transmission.
Sms.Manager.RESULT_ERROR_GENERIC_FAILURE - To indicate a nonspecific failure
Sms.Manager.RESULT_ERROR_RADIO_OFF - To indicate the phone radio is turned off
Sms.Manager.RESULT_ERROR_NULL_PDU - To indicate a PDU failure.
SmsManager.RESULT_ERROR_NO_SERVICE - To indicate that no cellular service is currently available.
第二个 Pending 参数(deliveryIntent)只有在收件人收到您的 SMS 消息后才会触发。
发送后和传递事件通过 Toast 显示适当的消息。
activity_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"
android:background="@drawable/bg"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/pn"
android:textSize="15sp"
android:textColor="@android:color/white"
android:background="@android:color/black"
/>
<EditText
android:id="@+id/phno"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray"
android:ems="10" >
</EditText>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/msg"
android:textSize="15sp"
android:textColor="@android:color/white"
android:background="@android:color/black"
/>
<EditText
android:id="@+id/smstxt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:background="@android:color/darker_gray"
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>
MainActivity.java
import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
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 MainActivity extends Activity {
Button btnSend;
EditText etPhoneNo;
EditText etMsg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_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 {
String SENT = "sent";
String DELIVERED = "delivered";
Intent sentIntent = new Intent(SENT);
/*Create Pending Intents*/
PendingIntent sentPI = PendingIntent.getBroadcast(
getApplicationContext(), 0, sentIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Intent deliveryIntent = new Intent(DELIVERED);
PendingIntent deliverPI = PendingIntent.getBroadcast(
getApplicationContext(), 0, deliveryIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
/* Register for SMS send action */
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String result = "";
switch (getResultCode()) {
case Activity.RESULT_OK:
result = "Transmission successful";
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
result = "Transmission failed";
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
result = "Radio off";
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
result = "No PDU defined";
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
result = "No service";
break;
}
Toast.makeText(getApplicationContext(), result,
Toast.LENGTH_LONG).show();
}
}, new IntentFilter(SENT));
/* Register for Delivery event */
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(getApplicationContext(), "Deliverd",
Toast.LENGTH_LONG).show();
}
}, new IntentFilter(DELIVERED));
/*Send SMS*/
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, msg, sentPI,
deliverPI);
} catch (Exception ex) {
Toast.makeText(getApplicationContext(),
ex.getMessage().toString(), Toast.LENGTH_LONG)
.show();
ex.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.activity_main, menu);
return true;
}
}
希望这有帮助!