Android短信的发送与接收
来源:互联网 发布:神武2手游天宫加点数据 编辑:程序博客网 时间:2024/06/12 01:22
今天学习了android短信的发送与接收。
首先是MainActivity.java
public class MainActivity extends Activity { private TextView sender; private TextView content; private EditText msgInput; private Button send; private EditText to; private IntentFilter receiveFilter; private MessageReceiver messageReceiver; 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); to = (EditText) findViewById(R.id.to); send = (Button) findViewById(R.id.send); msgInput = (EditText) findViewById(R.id.msg_input); 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); send.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SmsManager smsManager = SmsManager.getDefault(); Intent sendIntent = new Intent("SENT_SMS_ACTION"); PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this,0,sendIntent,0); smsManager.sendTextMessage(to.getText().toString(),null,msgInput.getText().toString(),pi,null); } }); } @Override protected void onDestroy(){ super.onDestroy(); unregisterReceiver(messageReceiver); unregisterReceiver(sendStatusReceiver); }// 接收短信内部类 class MessageReceiver extends BroadcastReceiver { public MessageReceiver() { } @Override public void onReceive(Context context, Intent intent) { Object[] pdus = (Object[])intent.getSerializableExtra("pdus"); //提取短信消息 String format = intent.getStringExtra("format"); SmsMessage[] messages = new SmsMessage[pdus.length]; for (int i = 0; i<messages.length;i++){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i],format); } } String address = messages[0].getOriginatingAddress(); //获取发送方号码 String fullMessage = ""; for (SmsMessage message : messages){ fullMessage +=message.getMessageBody();//获取短信内容 } sender.setText(address); content.setText(fullMessage); } }// 内部类监听短信发送是否成功 class SendStatusReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context,Intent intent){ if(getResultCode() == RESULT_OK){ //若发送成功 Toast.makeText(context, "发送成功", Toast.LENGTH_SHORT).show(); to.setText(""); msgInput.setText(""); }else { Toast.makeText(context, "发送失败", Toast.LENGTH_SHORT).show(); } } }}
发送和接收需要声明权限
<uses-permission android:name="android.permission.RECEIVE_SMS"/> <uses-permission android:name="android.permission.SEND_SMS"/>
布局文件:activity_main.xml
<?xml version="1.0" encoding="utf-8"?><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" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:padding="10dp" android:text="发送人:"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:id="@+id/sender"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:padding="10dp" android:text="内容:"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:id="@+id/content"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="接受人:"/> <EditText android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="center_vertical" android:id="@+id/to"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal"> <EditText android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="center_vertical" android:id="@+id/msg_input"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:text="发送" android:id="@+id/send"/> </LinearLayout></LinearLayout>
接收短信重写内部类:
class MessageReceiver extends BroadcastReceiver { public MessageReceiver() { } @Override public void onReceive(Context context, Intent intent) { Object[] pdus = (Object[])intent.getSerializableExtra("pdus"); //提取短信消息 String format = intent.getStringExtra("format"); SmsMessage[] messages = new SmsMessage[pdus.length]; for (int i = 0; i<messages.length;i++){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i],format); } } String address = messages[0].getOriginatingAddress(); //获取发送方号码 String fullMessage = ""; for (SmsMessage message : messages){ fullMessage +=message.getMessageBody();//获取短信内容 } sender.setText(address); content.setText(fullMessage); } }
0 0
- Android短信的发送与接收
- android中短信的发送与接收
- android发送与接收超长短信
- android 接收与发送超长短信说明
- android短信接收/发送
- 短信猫的发送与接收
- Android发送接收短信的代码示例
- Android发送接收短信的代码示例
- android 短信的接收和发送处理
- android 短信的接收和发送处理
- Android-短信的接收和发送
- android短信的接收和发送处理
- android 短信的接收和发送处理
- Android 短信模块分析(四) MMS之短信的发送与接收
- Android中短信的收发机制 发送短信 接收短信 拦截短信 监听短信
- android接收和发送短信
- Android 接收和发送短信
- Android接收和发送短信
- 51nod 1103 N的倍数 (抽屉原理)
- JQuery动画效果
- 例4.2 求顺序串中出现的第一个最长连续字符
- 在一个Activity中切换多个layout
- Nginx在Linux下的安装与配置
- Android短信的发送与接收
- WPF--Style 类
- Silverlight开源项目与第三方控件收集
- iOS巅峰之背景添加毛玻璃
- 刚体变量及代码实现
- 黑马程序员_JavaSE基础知识总结九:多态、包、import和内部类
- Silverlight4中的部分新特性
- 黑马程序员——Java集合框架复习总结(二)
- Python 学习中遇到的各种问题