博主很懒,只写代码
布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="10dp"
android:text="From:"
/>
<TextView
android:id="@+id/sender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="10dp"
android:text="Content:"
/>
<TextView
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="10dp"
android:text="To:"
/>
<EditText
android:id="@+id/to"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
>
<EditText
android:id="@+id/msg_input"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
/>
<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Send"
/>
</LinearLayout>
</LinearLayout>
MainAcitivity.java
package com.example.smstest;
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.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private TextView sender;
private TextView content;
private IntentFilter receiveFilter;
private MessageReceiver messageReceiver;
private EditText to;
private EditText msgInput;
private Button send;
private IntentFilter sendFilter;
private SendStatusReceiver sendStatusReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sender=(TextView)findViewById(R.id.sender);
content=(TextView)findViewById(R.id.content);
receiveFilter=new IntentFilter();
receiveFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
messageReceiver=new MessageReceiver();
registerReceiver(messageReceiver,receiveFilter);
sendFilter=new IntentFilter();
sendFilter.addAction("SENT_SMS_ACTION");
sendStatusReceiver=new SendStatusReceiver();
registerReceiver(sendStatusReceiver,sendFilter);
to=(EditText)findViewById(R.id.to);
msgInput=(EditText)findViewById(R.id.msg_input);
send=(Button)findViewById(R.id.send);
send.setOnClickListener(new OnClickListener(){
public void onClick(View v){
SmsManager smsManager=SmsManager.getDefault();
Intent sentIntent=new Intent("SENT_SMS_ACTION");
PendingIntent pi=PendingIntent.getBroadcast(MainActivity.this, 0, sentIntent, 0);
smsManager.sendTextMessage(to.getText().toString(), null, msgInput.getText().toString(), pi, null);
}
});
}
public void onDestory()
{
super.onDestroy();
unregisterReceiver(messageReceiver);
}
class MessageReceiver extends BroadcastReceiver{
public void onReceive(Context context,Intent intent){
Bundle bundle=intent.getExtras();
Object[] pdus=(Object[])bundle.get("pdus"); //提取短信消息
SmsMessage[] messages=new SmsMessage[pdus.length];
for(int i=0;i<messages.length;i++){
messages[i]=SmsMessage.createFromPdu((byte[])pdus[i]);
}
String address=messages[0].getOriginatingAddress();//获取发送方的号码
String fullMessage="";
for(SmsMessage message:messages){
fullMessage+=message.getMessageBody();//获取短信内容
}
sender.setText(address);
content.setText(fullMessage);
}
}
class SendStatusReceiver extends BroadcastReceiver{
public void onReceive(Context context,Intent intent){
if(getResultCode()==RESULT_OK){
//短信发送成功
Toast.makeText(context,"Send succeeded",Toast.LENGTH_LONG).show();
}
else{
//短信发送失败
Toast.makeText(context, "Send failed", Toast.LENGTH_LONG).show();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}