版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】怎么在Android中获取短信验证码
本篇文章为大家展示了怎么在Android中获取短信验证码,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。MainActivitypublic
class
MainActivity
extends
AppCompatActivity
{
public
static
TextView
mText;
private
SmsContent
content;
@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if
(this.checkSelfPermission(Manifest.permission.READ_SMS)
!=
PackageManager.PERMISSION_GRANTED)
{
//申请READ_SMS权限
ActivityCompat.requestPermissions(this,new
String[]{Manifest.permission.READ_SMS},
1);
}
mText
=
(TextView)
findViewById(R.id.text);
content
=
new
SmsContent(new
Handler(),this);
//注册短信变化监听
this.getContentResolver().registerContentObserver(Uri.parse("content://sms/"),
true,
content);
}
@Override
protected
void
onDestroy()
{
super.onDestroy();
this.getContentResolver().unregisterContentObserver(content);
}
@Override
public
void
onRequestPermissionsResult(int
requestCode,
@NonNull
String[]
permissions,
@NonNull
int[]
grantResults)
{
super.onRequestPermissionsResult(requestCode,
permissions,
grantResults);
doNext(requestCode,grantResults);
}
private
void
doNext(int
requestCode,
int[]
grantResults)
{
if
(requestCode
==
1)
{
if
(grantResults[0]
==
PackageManager.PERMISSION_GRANTED)
{
}
else
{
//
Permission
Denied
}
}
}
}SmsContentclass
SmsContent
extends
ContentObserver
{
private
Cursor
cursor
=
null;
private
Context
context;
public
SmsContent(Handler
handler,Context
context)
{
super(handler);
this.context
=
context;
}
@Override
public
void
onChange(boolean
selfChange)
{
super.onChange(selfChange);
Log.i("SMSTest","Begin");
//读取收件箱中指定号码的短信
//
cursor
=
context.getContentResolver().query(Uri.parse("content://sms/inbox"),
new
String[]{"_id",
"address",
"read",
"body"},
//
"
address=?
and
read=?",
new
String[]{"10086",
"0"},
"_id
desc");//按id排序,如果按date排序的话,修改手机时间后,读取的短信就不准了
cursor
=
context.getContentResolver().query(Uri.parse("content://sms/inbox"),
new
String[]{"_id",
"address",
"read",
"body"},
null,
null,
"_id
desc");
Log.i("SMSTest","cursor.isBeforeFirst():
"
+
cursor.isBeforeFirst()
+
"
cursor.getCount():
"
+
cursor.getCount());
if
(cursor
!=
null
&&
cursor.getCount()
>
0)
{
cursor.moveToFirst();
int
smsbodyColumn
=
cursor.getColumnIndex("body");
String
smsBody
=
cursor.getString(smsbodyColumn);
Log.i("SMSTest","smsBody
=
"
+
smsBody);
MainActivity.mText.setText(getDynamicPassword(smsBody));
}
//在用managedQuery的时候,不能主动调用close()方法,
否则在Android
4.0+的系统上,
会发生崩溃
if(Build.VERSION.SDK_INT
<
14)
{
cursor.close();
}
}
public
static
String
getDynamicPassword(String
str)
{
Pattern
continuousNumberPattern
=
Ppile("[0-9\\.]+");
Matcher
m
=
continuousNumberPattern.matcher(str);
String
dynamicPassword
=
"";
while(m.find()){
if(m.group().length()
==
6)
{
System.out.print(m.group());
dynamicPassword
=
m.group();
}
}
return
dynamicPassword;
}
}上述方法未读短信多了之后会同时上传2条验证码信息,可以根据cursor.getCount()来控制下。。。下面第二种方法在oppor9s上不见效果。。各位使用的小伙伴注意哦public
class
SmsReceiver
extends
BroadcastReceiver
{
public
static
final
String
SMS_RECEIVED
=
"vider.Telephony.SMS_RECEIVED";
private
boolean
flag
=
false;
private
String
msgBody;
private
String
msgAddress;
public
SmsReceiver()
{
Log.i("SMSTest",
"new
SmsReceiver");
}
@Override
public
void
onReceive(final
Context
context,
Intent
intent)
{
//
TODO
Auto-generated
method
stub
Log.i("SMSTest",
"jie
shou
dao");
Cursor
cursor
=
null;
try
{
if
(SMS_RECEIVED.equals(intent.getAction()))
{
Log.d("SMSTest",
"sms
received!");
Bundle
bundle
=
intent.getExtras();
if
(bundle
!=
null)
{
Object[]
pdus
=
(Object[])
bundle.get("pdus");
final
SmsMessage[]
messages
=
new
SmsMessage[pdus.length];
for
(int
i
=
0;
i
<
pdus.length;
i++)
{
messages[i]
=
SmsMessage.createFromPdu((byte[])
pdus[i]);
}
if
(messages.length
>
0)
{
msgBody
=
messages[0].getMessageBody();
msgAddress
=
messages[0].getOriginatingAddress();
long
msgDate
=
messages[0].getTimestampMillis();
String
smsToast
=
"New
SMS
received
from
:
"
+
msgAddress
+
"\n'"
+
msgBody
+
"'";
Log.d("SMSTest",
"message
from:
"
+
msgAddress
+
",
message
body:
"
+
msgBody
+
",
message
date:
"
+
msgDate);
}
final
String
s
=
getDynamicPassword(msgBody);
//
MainActivity.mText.setText(s);
if
(s.length()
!=
0)
{
new
AsyncTask<String,
Void,
Void>()
{
@Override
protected
Void
doInBackground(String...
strings)
{
try
{
URL
url
=
new
URL(strings[0]);
HttpURLConnection
connect
=
(HttpURLConnection)
url.openConnection();
InputStream
is
=
connect.getInputStream();
InputStreamReader
isr
=
new
InputStreamReader(is,
"utf-8");
BufferedReader
br
=
new
BufferedReader(isr);
String
line;
while
((line
=
br.readLine())
!=
null)
{
Log.i("SMSTest",
"line
=
"
+
line);
JSONObject
obj
=
new
JSONObject(line);
flag
=
obj.getBoolean("result");
}
}
catch
(IOException
|
JSONException
e)
{
e.printStackTrace();
}
return
null;
}
@Override
protected
void
onPostExecute(Void
aVoid)
{
super.onPostExecute(aVoid);
if
(flag)
{
Toast.makeText(context,
"fin
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年04月江苏宿迁泗洪县部分事业单位招考聘用118人笔试历年高频考点试摘选答案解析附后
- 2024年04月广西科学院空间智能团队招考聘用笔试历年高频考点试题附带答案解析
- 2023年气泡水企业商业风险管理
- 2023年汽车罐车企业战略商业风险
- 2024年04月广东佛山市三水区人民法院招考聘用劳动合同制审判辅助人员笔试历年高频考点试摘选答案解析附后
- 2024年04月山东菏泽鄄城县县直单位选调14人笔试历年高频考点试题附带答案解析
- 2024年04月山东畜牧兽医职业学院招考聘用博士研究生15人笔试历年高频考点试题附带答案解析
- 2024年04月安徽蚌埠市蚌山区中小学校(事业编制)2024届春季“校园招考聘用”笔试历年高频考点试摘选答案解析附后
- 2024年04月国家医疗保障局大数据中心招考聘用应届生笔试历年高频考点试题附带答案解析
- 2024年04月四川省广元市中级人民法院为辖区基层法院2024年公开招考4名合同制工作人员笔试历年高频考点试摘选答案解析附后
- 二年级有余数的除法时导学案
- 年种植10万亩优质油莎豆及年产万吨油莎豆食用油项目可行性方案研究报告
- 外企面试自我介绍7篇
- 建筑工程建筑面积计算规范(国标)
- 学校组织架构图
- 工程索赔范本
- 中考数学试题双向细目表整理版
- 英语国际音标(48个)
- 2018小学六年级数学毕业试卷及答案
- 广西现代物流发展的现状分析
- 医用气体工程施工方案及技术措施
评论
0/150
提交评论