Android 发送短信与接收短信
package com.example.testsms; 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.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends Activity {
private Button send;
private TextView number;
private TextView content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
send = (Button) this.findViewById(R.id.button1);
number = (TextView) this.findViewById(R.id.textView2);
content = (TextView) this.findViewById(R.id.textView1);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com");
MyReceiver myReceiver = new MyReceiver();
registerReceiver(myReceiver,intentFilter);
send.setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
SmsManager smsManager = SmsManager.getDefault();
Intent sentIntent = new Intent("com");
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, sentIntent, 0);
smsManager.sendTextMessage((String)number.getText(),null,(String)content.getText(),pendingIntent,null);
}
}); }
class MyReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
if(getResultCode()==RESULT_OK){
Toast.makeText(MainActivity.this,"success",1).show();
}
}
}
}
package com.example.mysms6; import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.app.Activity;
import android.telephony.SmsMessage;
import android.view.Menu;
import android.widget.TextView; public class MainActivity extends Activity {
private TextView textView1;
private TextView textView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1 = (TextView) this.findViewById(R.id.textView);
textView2 = (TextView) this.findViewById(R.id.textView3);
IntentFilter intentFilter = new IntentFilter();
intentFilter.setPriority(800);
intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
MyReceiver myReceiver = new MyReceiver();
registerReceiver(myReceiver,intentFilter);
}
class MyReceiver 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();
}
textView1.setText(address);
textView2.setText(fullMessage);
abortBroadcast();
}
}
}
方便自己以后查阅吧 一个用eclipse 一个用Android Studio
Android 发送短信与接收短信的更多相关文章
- Android使用BroadCastRecevier广播实现接收短信,并利用Toast弹出显示内容
在上一篇文章 Android简单实现BroadCastReceiver广播机制 中简单的实现了一个广播机制,这里利用BroadCarstRecevier实现一个接收短信并显示内容的案例,当然至于接收到 ...
- android 发送自定义广播以及接收自定义广播
发送自定义广播程序: 布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout x ...
- Android软件开发之发送短信与系统短信库解析
今天我和同学们讨论一下Android平台下如何调用系统方法发送短信.接收短信.系统的短信库相关的问题.进入正题,我们先使用Eclipse工具模拟给自己的模拟器发送一条短信.在Eclipse下打开DDM ...
- android学习十四(android的接收短信)
收发短信是每一个手机主要的操作,android手机当然也能够接收短信了. android系统提供了一系列的API,使得我们能够在自己的应用程序里接收和发送短信. 事实上接收短信主要是利用我们前面学过的 ...
- 基于Socket客户端局域网或广域网内共享同一短信猫收发短信的开发解决方案
可使同一网络(局域网或广域网)内众多客户端,共享一个短信猫设备短信服务器进行短信收发,短信服务器具备对客户端的管理功能. 下面是某市建设银行采用本短信二次开发平台时实施的系统方案图: 在该方案中,考虑 ...
- Android之发送短信和接收验证码
最近项目需求需要发送短信和接收验证码并将验证码显示在输入框中 以下是我的记录 前提---权限 <uses-permission android:name="andro ...
- Android 短信模块分析(四) MMS之短信的发送与接收
MMS之短信的发送与接收分析: 一.信息发送: com.android.mms.data.WorkingMessage.java 类 send()函数: public void send() { . ...
- android 发送短信的两种方式,以及接收报告和发送报告
android发送短信,以及接收报告和发送报告 android中发送短信其实有两种方式,这个和打电话类似,大家可以了解一下: 一.调起系统发短信功能 ...
- android基础---->发送和接收短信
收发短信应该是每个手机最基本的功能之一了,即使是许多年前的老手机也都会具备这项功能,而Android 作为出色的智能手机操作系统,自然也少不了在这方面的支持.今天我们开始自己创建一个简单的发送和接收短 ...
随机推荐
- yum change source repo centos共存安装sun jdk6和jdk7
之前一直使用的是163的源,今天从微博看到阿里云推出了自己的源.因为我的主机是阿里云,所以可以走内网,速度提升更快.过程如下:cd /etc/yum.repos.d/mv mv CentOS-Base ...
- 【转】MySQL USE NAMES 'UTF8'
先说MySQL的字符集问题.Windows下可通过修改my.ini内的 # CLIENT SECTION [mysql] default-character-set=utf8 # SERVER SEC ...
- openssh-server 安装
sudo apt-get update sudo apt-get install openssh-server 1:ssh-keygen -t rsa -f ~/.ssh/id_rsa 这里会提示输入 ...
- Windows创建文件链接
Windows平台创建文件.文件夹链接: 测试平台,windows10. D:\>mklink 创建符号链接. MKLINK [[/D] | [/H] | [/J]] Link Target / ...
- cdecl和stdcall调用约定-汇编演示
. .model flat, stdcall .stack ExitProcess PROTO, dwExitCode:DWORD .data val2 sdword result dword ? . ...
- [LeetCode]题解(python):063-Unique path II
题目来源 https://leetcode.com/problems/unique-paths-ii/ Follow up for "Unique Paths": Now cons ...
- Linux上使用SMART检测硬盘
SMART(Self-Monitoring, Analysis, and Reporting Technology)是一种普及度比较高的磁盘分析检测工具,磁盘运行过程中,该工具搜集磁盘的状态参数,如型 ...
- 通过iphone蓝牙与经过苹果MFI授权认证的硬件通讯,传输图片(转)
http://blog.csdn.net/hwj2012/article/details/7883711 相关: http://blog.csdn.net/xufeidll/article/detai ...
- 给Windows机器创建软连接
给Windows机器创建软连接 http://blog.csdn.net/w6611415/article/details/32084677
- shell 脚本文件Windows传到Linux后编码问题
shell 脚本文件Windows传到Linux后编码问题 下面这个标红的位置出现,是由于脚本从Windows机器上直接传到linux文件格式不对导致的. cat -v a.sh help^M exi ...