<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/sms_from"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:text="From" />
<TextView
android:id="@+id/sms_from_txt"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_marginLeft="15dp"
android:layout_toRightOf="@id/sms_from"/>
<TextView
android:id="@+id/sms_content"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_below="@id/sms_from"
android:layout_marginTop="20dp"
android:text="Content" />
<TextView
android:id="@+id/sms_content_txt"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="20dp"
android:layout_below="@id/sms_from_txt"
android:layout_toRightOf="@id/sms_content"/>
</RelativeLayout>
public class MainActivity extends AppCompatActivity {
private TextView fromTv;
private TextView contentTv;
private IntentFilter intentFilter;
private MessageReceiver messageReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
getSms();
}
private void getSms() {
intentFilter = new IntentFilter(); intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
messageReceiver = new MessageReceiver();
//设置较高的优先级
intentFilter.setPriority(100);
registerReceiver(messageReceiver, intentFilter);
}
private void initView() {
fromTv = (TextView) findViewById(R.id.sms_from_txt);
contentTv = (TextView) findViewById(R.id.sms_content_txt);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(messageReceiver);
}
class MessageReceiver extends BroadcastReceiver {
@Override
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();
}
//截断广播,阻止其继续被Android自带的短信程序接收到
abortBroadcast();
fromTv.setText(address);
contentTv.setText(fullMessage);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/to_ed"
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="to"/>
<EditText
android:id="@+id/to_content"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@id/to_ed"
android:hint="content"/>
<Button
android:id="@+id/send_msg"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@id/to_content"
android:text="@string/send_message"/>
</RelativeLayout>
public class SendMsgActivity extends AppCompatActivity implements View.OnClickListener {
private Context context;
private EditText toEdit;
private EditText toContent;
private IntentFilter sendFilter;
private SendStatusReceiver sendStatusReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send_msg);
context = this;
initView();
}
private void initView() {
toEdit = (EditText) findViewById(R.id.to_ed);
toContent = (EditText) findViewById(R.id.to_content);
//添加发送按钮的点击监听事件
Button sendMsg = (Button) findViewById(R.id.send_msg);
sendMsg.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.send_msg:
sendMessage();
break;
default:
break;
}
}
private void sendMessage() {
//获取短信接收者号码
String to = toEdit.getText().toString();
//获取发送短信内容
String content = toContent.getText().toString();
//获得广播接收器实例和IntentFilter实例
sendStatusReceiver = new SendStatusReceiver();
sendFilter = new IntentFilter();
sendFilter.addAction("SENT_SMS_ACTION");
//注册广播监听
registerReceiver(sendStatusReceiver, sendFilter);
//构造PendingIntent启动短信发送状态监控广播
Intent sendIntent = new Intent("SENT_SMS_ACTION");
PendingIntent pi = PendingIntent.getBroadcast(context, 0, sendIntent, 0);
//获得短信管理实例
SmsManager smsManager = SmsManager.getDefault();
//调用发送短信函数,传入参数发送短信(第一、三、四参数依次为接收者号码,短信内容,短信发送状态监控的PendingIntent)
smsManager.sendTextMessage(to, null, content, pi, null);
}
/**
* 构造广播接收器内部类监控短信是否发送成功
*/
class SendStatusReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
if (getResultCode() == RESULT_OK){
Toast.makeText(context, "successful", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context, "failed", Toast.LENGTH_SHORT).show();
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
//取消注册的广播
unregisterReceiver(sendStatusReceiver);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有