Android SmsManager 发送短信
SmsManager可以在后台发送短信,无需用户操作,开发者就用这个SmsManager功能在后台偷偷给SP发短信,导致用户话费被扣。必须添加android.permission.SEND_SMS权限。
<uses-permission android:name="android.permission.SEND_SMS" />
如果短信内容过长,可以使用SmsManager.divideMessage(String text)方法自动拆分成一个ArrayList数组,再根据数组长度循环发送。
用sendMultipartTextMessage(String destinationAddress, string scAddress, ArrayList<String> parts, ArrayList<PendingIntent> sentIntents, ArrayList<PendingIntent> deliveryIntents)方法发送。
参数分别为号码,短信服务中心号码(null 即可),短信内容,短信发送结果广播PendingIntent,短信到达广播。
下面写一个demo查移动话费余额,贴上代码:
package com.dimos.sendmessage; import java.util.ArrayList; import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsManager; public class SendMessageActivity extends Activity { @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); SendReceiver receiver=new SendReceiver();
IntentFilter filter=new IntentFilter();
filter.addAction(SendReceiver.ACTION);
registerReceiver(receiver,filter);
//必须先注册广播接收器,否则接收不到发送结果 SmsManager smsManager = SmsManager.getDefault();
Intent intent = new Intent();
intent.setAction(SendReceiver.ACTION);
ArrayList<String> divideMessage = smsManager.divideMessage("ye");
PendingIntent sentIntent = PendingIntent.getBroadcast(this, 1, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
sentIntents.add(sentIntent); try {
smsManager.sendMultipartTextMessage("10086", null,
divideMessage, sentIntents, null);
} catch (Exception e) {
e.printStackTrace();
}
}
}
接收器:
package com.dimos.sendmessage; import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent; public class SendReceiver extends BroadcastReceiver { public static final String ACTION = "action.send.sms"; @Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION.equals(action)) {
int resultCode = getResultCode();
if (resultCode == Activity.RESULT_OK) {
// 发送成功
System.out.println("发送成功!");
} else {
// 发送失败
System.out.println("发送失败!");
}
}
} }
Android SmsManager 发送短信的更多相关文章
- android之发送短信程序
首先改写activity_main.xml文件 代码如下: <LinearLayout xmlns:android="http://schemas.android.com/apk/re ...
- Android 学习第13课,android 实现发送短信的功能
1. 界面布局 界面代码: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ...
- Android之发送短信和接收验证码
最近项目需求需要发送短信和接收验证码并将验证码显示在输入框中 以下是我的记录 前提---权限 <uses-permission android:name="andro ...
- Android之发送短信的两种方式
SMS涉及的主要类SmsManager 实现SMS主要用到SmsManager类,该类继承自java.lang.Object类,下面我们介绍一下该类的主要成员. 公有方法: ArrayList< ...
- (转)Android之发送短信的两种方式
https://www.cnblogs.com/dongweiq/p/4866022.html if(TextUtils.isEmpty(number)||TextUtils.isEmpty(cont ...
- Android 中发送短信
import android.net.Uri; //调用Android系统API发送短信 Uri uri = Uri.parse("smsto:" + strSmsPhone_va ...
- android 60 发送短信
import android.os.Bundle; import android.app.Activity; import android.telephony.SmsManager; import a ...
- 至Android虚拟机发送短信和拨打电话
Android的emulator是已经包括了gsm 模块,能够模拟电话与短信进行调试(就不用花太多冤枉钱) 首先,肯定是打开虚拟机: emulator -avd XXXXXX -scale 0.8&a ...
- java攻城师之路(Android篇)--搭建开发环境、拨打电话、发送短信、布局例子
一.搭建开发环境 1.所需资源 JDK6以上 Eclipse3.6以上 SDK17, 2.3.3 ADT17 2.安装注意事项 不要使用中文路径 如果模拟器默认路径包含中文, 可以设置android_ ...
随机推荐
- crtmpserver系列之一:流媒体概述
阅读目录 概述 流媒体系统的组成 媒体文件封装 传输协议 回到顶部 概述 所谓流媒体按照字面意思理解就是像流一样的媒体,看起来像是废话.流媒体现在司空见惯,所以一般人大概不会有疑问.事实上在流媒体还没 ...
- 启动Activity时显示空白界面的问题
问题描述: 启动activity时,先显示一个空白的界面,带标题栏的,1秒左右的时间后才显示activity对应 layout上的内容. 解决办法: 将activity的windows设置为透明的就可 ...
- .Net中C#的DllImport的用法
大家在实际工作学习C#的时候,可能会问:为什么我们要为一些已经存在的功能(比如 Windows中的一些功能,C++中已经编写好的一些方法)要重新编写代码,C#有没有方法可以直接都用这些原本已经存在的功 ...
- Moduli number system
A number system with moduli is defined by a vector of k moduli, [m1,m2, ···,mk]. The moduli must be p ...
- Android开发UI之ViewPager及PagerAdapter
ViewPager,官网链接--http://developer.android.com/reference/android/support/v4/view/ViewPager.html ViewPa ...
- phpMyAdmin import.php 跨站脚本漏洞
漏洞名称: phpMyAdmin import.php 跨站脚本漏洞 CNNVD编号: CNNVD-201402-281 发布时间: 2014-02-21 更新时间: 2014-02-21 危害等级: ...
- C#创建https请求并使用pfx证书
常规情况下创建Web请求,并获取请求数据的代码如下: WebRequest req = WebRequest.Create(url); req.Timeout = 15000; WebResponse ...
- Android开发视频学习(1)
S01E02_搭建Android开发环境 设置ANDROID_SDK_HOME环境变量,用于指定Android模拟器的目录,官方文档没有说要设置. S01E03_Android创世纪 Android根 ...
- [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.2.5
Show that matrices with distinct eigenvalues are dense in the space of all $n\times n$ matrices. (Us ...
- WinForm触摸屏程序功能界面长时间不操作自动关闭回到主界面 z
操作者经常会在执行了某操作后,没有返还主界面就结束了操作然后离开了,程序应该关闭功能窗体自动回到主界面方便下一位操作者操作.那么对于WinForm程序怎么实现呢? 实现原理:拦截Application ...