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_ ...
随机推荐
- Android 如何在Java代码中手动设置控件的marginleft
1.定义LayoutParams LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.La ...
- vnc执行,报xauth could not run
/usr/bin/gnome-terminal [INFO-10%]生成随机密码 [INFO-20%]生成密码文件 [INFO-30%]完成初始化,准备打开连接 Error: could not ru ...
- Android:双层嵌套ViewPager并添加单击事件
仿网易新闻: 实现原理: 上面的导航栏是一个自定义的ViewGroup(类似ActionBar) 中间内容层是ViewPager套fragment实现左右切换滑动 fragment里面的布局是一个Li ...
- 使用智遥工作流,优化SAP请购流程
传统请购流程,都是用户在SAP系统中填写请购单,然后再打印出来,递交给上级领导审批.领导审批完了,再到SAP系统中更新release标识.若中途请购单内容需要变更,则需要重新打印,审批. 智遥工作流, ...
- Oracle Kill Session – FRM-40501
FRM-40501: ORACLE error: unable to reserve record for update or delete frm-40501:oracle 错误:无法保留用于更新或 ...
- Android开发UI之个性化控件之Menu
MenuDrawer 滑出式菜单,通过拖动屏幕边缘滑出菜单,支持屏幕上下左右划出,支持当前View处于上下层,支持Windows边缘.ListView边缘.ViewPager变化划出菜单等. 项目地址 ...
- Aspcms所有标签调用
错误号3002: 出现此问题一般是有些频道模板不全,或者缺少一部分模板导致,或者没有单独设置html文件夹,并将所有html页面放进去 基本语法 {aspcms:sitepath} 网站终极目录(可放 ...
- voucer
<style type="text/css"> .fieldset_s{border: 1px #dedede solid;padding: 19px; color: ...
- ES5新特性:理解 Array 中增强的 9 个 API
为了更方便的对JS中Array进行操作,ES5规范在Array的原型上新增了9个方法,分别是forEach.filter.map.reduce.reduceRight.some.every.index ...
- 如何在小方框上打对号 小方框内打对勾 word 方框打对勾
在word中做选择时,很多人遇到需要在小方框上打对勾而不知如何做,现将可行的各种方法总结如下: 1:直接找到一个做好的,保存为图片,在需要的时候插入它: 2:插入文本框,然后边框选择为实线,在文本 ...