前言

Android 监听短信的方式有两种

1、监听短信数据库,数据库发生改变时回调。

2、监听短信广播

其中第二种方式由于国内各厂家的定制Android 可能导致无响应 目前测试 魅族 无法监听到短信广播

本文介绍第一种方式监听短信

一、创建Service前台服务

package com.iwhalecloud.demo.SMS;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.database.ContentObserver;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Binder;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.RequiresApi;
import com.iwhalecloud.demo.MainActivity;
import com.iwhalecloud.demo.R;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response; public class MyService extends Service {
private static final String TAG = MyService.class.getSimpleName();
private SMSContentObserver smsObserver;
public String phoneNo = "";
public String httpUrl = ""; @Override
public IBinder onBind(Intent intent) {
return new MyBinder();
}
public class MyBinder extends Binder {
/**
* 获取当前Service的实例
* @return
*/
public MyService getService(){
return MyService.this;
}
} @RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onCreate() {
super.onCreate();
//注册观察者
smsObserver = new SMSContentObserver(MyService.this,new Handler());
getContentResolver().registerContentObserver(Uri.parse("content://sms"), true, smsObserver);
} @RequiresApi(api = Build.VERSION_CODES.O)
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
phoneNo=intent.getStringExtra("phoneNum");
httpUrl=intent.getStringExtra("httpUrl");
startForeground(100,getNotification("服务运行中...","正在监听号码:"+phoneNo+",保持应用后台运行..."));
return START_STICKY;
} @Override
public void onDestroy() {
super.onDestroy();
getContentResolver().unregisterContentObserver(smsObserver);
} @RequiresApi(api = Build.VERSION_CODES.O)
private Notification getNotification(String title, String message){
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); // 唯一的通知通道的id.
String notificationChannelId = "notification_channel_id_01";
// Android8.0以上的系统,新建消息通道
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//用户可见的通道名称
String channelName = "Foreground Service Notification";
//通道的重要程度
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(notificationChannelId, channelName, importance);
notificationChannel.setDescription("Channel description");
//LED灯
notificationChannel.enableLights(false);
//震动
notificationChannel.enableVibration(false);
if (notificationManager != null) {
notificationManager.createNotificationChannel(notificationChannel);
}
} NotificationCompat.Builder builder = new NotificationCompat.Builder(this, notificationChannelId);
//通知小图标
builder.setSmallIcon(R.mipmap.ic_launcher);
//通知标题
builder.setContentTitle(title);
//通知内容
builder.setContentText(message);
//设定通知显示的时间
builder.setWhen(System.currentTimeMillis());
//设定启动的内容
Intent clickIntent = new Intent(Intent.ACTION_MAIN);
//点击回到活动主页 而不是创建新主页
clickIntent.addCategory(Intent.CATEGORY_LAUNCHER);
clickIntent.setComponent(new ComponentName(this,MainActivity.class));
clickIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent); //创建通知并返回
return builder.build();
}
@SuppressLint("Range")
private void setSmsCode() {
Toast.makeText(MyService.this,phoneNo+":"+httpUrl,Toast.LENGTH_SHORT).show();
Cursor cursor = null;
// 添加异常捕捉
try {
cursor = getContentResolver().query(
Uri.parse("content://sms"),
new String[] { "_id", "address", "body", "date" },
null, null, "date desc"); //
if (cursor != null) {
cursor.moveToFirst();
final long smsdate = Long.parseLong(cursor.getString(cursor.getColumnIndex("date")));
final long nowdate = System.currentTimeMillis();
Toast.makeText(MyService.this,nowdate+":"+smsdate+"===="+ (nowdate - smsdate),Toast.LENGTH_SHORT).show();
// 如果当前时间和短信时间间隔超过60秒,认为这条短信无效
final String strAddress = cursor.getString(cursor.getColumnIndex("address")); // 短信号码
final String strBody = cursor.getString(cursor.getColumnIndex("body")); // 在这里获取短信信息
Toast.makeText(MyService.this,"strAddress:"+strAddress,Toast.LENGTH_SHORT).show();
Toast.makeText(MyService.this,"strBody:"+strBody,Toast.LENGTH_SHORT).show();
if (nowdate - smsdate > 60 * 1000) {
Log.i(TAG, "短信过期");
Toast.makeText(MyService.this,"短信过期",Toast.LENGTH_SHORT).show();
return;
} final int smsid = cursor.getInt(cursor.getColumnIndex("_id"));
if (TextUtils.isEmpty(strAddress) || TextUtils.isEmpty(strBody)) {
return;
}
Log.i(TAG, "phoneNo: "+phoneNo);
Log.i(TAG, "httpUrl: "+httpUrl);
if (strAddress.equals(phoneNo)){
Log.i(TAG, "是我想要的号码");
Toast.makeText(MyService.this,strAddress+":"+strBody,Toast.LENGTH_SHORT).show();
Pattern continuousNumberPattern = Pattern.compile("(?<![0-9])([0-9]{6})(?![0-9])");
Matcher m = continuousNumberPattern.matcher(strBody);
String dynamicPassword = "";
while (m.find()) {
dynamicPassword = m.group();
}
//连接http服务
String finalDynamicPassword = dynamicPassword;
new Thread(new Runnable() {
@Override
public void run() {
OkHttpClient mOkHttpClient = new OkHttpClient();
try {
RequestBody requestBody = new FormBody.Builder().add("code", finalDynamicPassword).build();
Request request = new Request.Builder().url(httpUrl).post(requestBody).build();
Response response = mOkHttpClient.newCall(request).execute();//发送请求
String result = response.body().string();
Log.d(TAG, "result: " + result);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
Log.i(TAG, "onReceiveSms: "+ dynamicPassword);
}
}else {
Toast.makeText(MyService.this,"cursor 为 NULL",Toast.LENGTH_SHORT).show();
}
}catch (Exception e) {
Toast.makeText(MyService.this,"出错了::::"+e.getMessage(),Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
finally {
Toast.makeText(MyService.this, String.valueOf(cursor != null),Toast.LENGTH_SHORT).show();
if (cursor != null) {
cursor.close();
}
}
}
public class SMSContentObserver extends ContentObserver {
private static final int MSG = 1;
private int flag = 0;
private Context mContext;
private Handler mHandler;
public SMSContentObserver(Context mContext,
Handler mHandler) {
super(mHandler);
this.mContext = mContext;
this.mHandler = mHandler;
}
@Override
public void onChange(boolean selfChange) {
// TODO Auto-generated method stub
super.onChange(selfChange);
//onchange调用两次 过滤掉一次
if (flag%2 == 1){
setSmsCode();
}
flag++;
}
} }

二、主界面(参考)

package com.iwhalecloud.demo;

import android.Manifest;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton; import androidx.appcompat.app.AppCompatActivity;
import com.iwhalecloud.demo.SMS.MyService; public class MainActivity extends AppCompatActivity { private static final String TAG = "CC";
final private int REQUEST_CODE_ASK_PERMISSIONS = 1;
private boolean serviceFlag = false; @Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences read = getSharedPreferences("info", MODE_PRIVATE);
String phoneNum = "10659874";
String httpUrl= "";
if (read.getString("phoneNum",null)!=null){
phoneNum = read.getString("phoneNum",null);
}
if (read.getString("httpUrl",null)!=null){
httpUrl = read.getString("httpUrl",null);
}
TextView v1 = findViewById(R.id.editText1);
TextView v2 = findViewById(R.id.editText2);
v1.setText(phoneNum);
v2.setText(httpUrl);
ToggleButton tb = findViewById(R.id.button);
tb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
TextView pn = findViewById(R.id.editText1);
TextView httpUrl = findViewById(R.id.editText2);
if (tb.isChecked()){
Intent service = new Intent(getApplicationContext(), MyService.class);
SharedPreferences.Editor editor = getSharedPreferences("info",MODE_PRIVATE).edit();
editor.putString("phoneNum",pn.getText().toString());
editor.putString("httpUrl",httpUrl.getText().toString());
editor.apply();
service.putExtra("phoneNum",pn.getText().toString());
service.putExtra("httpUrl",httpUrl.getText().toString());
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
MainActivity.this.startService(service);
}
pn.setEnabled(false);
httpUrl.setEnabled(false);
Toast.makeText(MainActivity.this,"服务开启",Toast.LENGTH_SHORT).show();
}else {
Intent service = new Intent(getApplicationContext(), MyService.class);
MainActivity.this.stopService(service);
pn.setEnabled(true);
httpUrl.setEnabled(true);
Toast.makeText(MainActivity.this,"服务停止",Toast.LENGTH_SHORT).show();
}
}
});
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
int hasReadSmsPermission = checkSelfPermission(Manifest.permission.READ_SMS);
if (hasReadSmsPermission != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.READ_SMS}, REQUEST_CODE_ASK_PERMISSIONS);
return;
}
}
} @Override
protected void onResume() {
super.onResume();
} @Override
protected void onDestroy() {
super.onDestroy();
Intent service = new Intent(getApplicationContext(), MyService.class);
this.stopService(service);
}
}

三、Main.XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"> <EditText
android:id="@+id/editText1"
android:layout_width="295dp"
android:layout_height="58dp"
android:hint="监听电话号码"
android:textColorHint="#95A1AA"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.465"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.469" /> <EditText
android:id="@+id/editText2"
android:layout_width="295dp"
android:layout_height="58dp"
android:text="http://10.0.2.2:8888/api/test/t1"
android:hint="验证码请求接口"
android:textColorHint="#95A1AA"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.45"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.595" /> <ToggleButton
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="停止服务"
android:textOff="启动服务"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText1"
app:layout_constraintVertical_bias="0.495" /> </androidx.constraintlayout.widget.ConstraintLayout>

四、清单文件

    <uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
....
....>
<service
android:name=".SMS.MyService"
android:enabled="true"
android:exported="false" />
</application>

Android 监听短信数据库过滤获取短信内容上传至服务器的更多相关文章

  1. 使用 html5 FileReader 获取图片, 并异步上传到服务器 (不使用 iframe)

    为什么80%的码农都做不了架构师?>>>   原理: 1.使用FileReader 读取图片的base64编码 2.使用ajax,把图片的base64编码post到服务器. 3.根据 ...

  2. Android监听系统短信数据库变化-提取短信内容

    由于监听系统短信广播受到权限的限制,所以很多手机可能使用这种方式没法监听广播,从而没办法获取到系统短信,所以又重新开辟一条路. Android监听系统短信数据库内容变化使用场景: 1.监听短信数据库的 ...

  3. Android 监听短信(同时监听广播和数据库)

    暗扣,强烈谴责这种侵害用户利益的行为... 下面给大家介绍Android暗扣原理.......  Android4.4以下的系统玩游戏就要小心了哈 暗扣方式之一:短信订购,即监听--------拦截- ...

  4. android 监听短信并发送到服务器

    1. 接受系统的短信广播,操作短信内容. 优点:操作方便,适合简单的短信应用. 缺点:来信会在状态栏显示通知信息. 2. 应用观察者模式,监听短信数据库,操作短信内容.   实例如下: SystemE ...

  5. 【Android】Android 监听apk安装替换卸载广播

    [Android]Android 监听apk安装替换卸载广播 首先是要获取应用的安装状态,通过广播的形式 以下是和应用程序相关的Broadcast Action ACTION_PACKAGE_ADDE ...

  6. Android 监听双卡信号强度(附完整代码)

    Android 监听双卡信号强度 监听单卡信号强度 监听单卡的信号强度非常简单直接用TelephonyManager.listen()去监听sim卡的信号强度. TelephonyManager = ...

  7. Android监听返回键、Home键+再按一次返回键退出应用

    Android监听返回键需重写onKeyDown()方法 Home键keyCode==KeyEvent.KEYCODE_HOME @Override public boolean onKeyDown( ...

  8. Android监听来电和去电

    要监听android打电话和接电话,只需下面2步骤1.第一步,写一个Receiver继承自BroadcastReceiver import android.app.Service; import an ...

  9. Android监听应用程序安装和卸载

    Android监听应用程序安装和卸载 第一. 新建监听类:BootReceiver继承BroadcastReceiver package com.rongfzh.yc; import android. ...

  10. Android监听ScrollView滑动到顶端和底部

    Android监听ScrollView滑动到顶端和底部     package cn.testscrollview; import android.os.Bundle; import android. ...

随机推荐

  1. 零基础学习人工智能—Python—Pytorch学习(十一)

    前言 本文主要介绍tensorboard的使用. tensorboard是一个可视化的,支持人工智能学习的一个工具. tensorboard的官方地址:https://www.tensorflow.o ...

  2. ECShop开源商城与COS互通:降低本地存储负载、提升访问体验

    ECShop简介 ECShop是一款开源电子商务平台,具有简单易用.安全稳定.模块化设计等特点.它提供了完整的电子商务解决方案,包括商品管理.订单管理.支付管理.配送管理.会员管理.促销管理.数据统计 ...

  3. Flutter 滑动组件互相嵌套问题

    滑动组件互相嵌套问题 如果listview/singlechildscrollview 嵌套gridview,将两个组件的shrinkwrap设置为true,并且gridview无法滚动 physic ...

  4. 中电金信:产教联合共育人才 AFAC2024金融智能创新大赛启动

    当前,人工智能技术正在蓬勃发展,引领着各行各业迈向智能化的新纪元,特别是在金融科技领域,伴随人工智能技术的不断迭代与突破,金融服务的边界也在不断拓展,传统的金融业态正经历着深刻的变革与重塑. 与此同时 ...

  5. 大咖论道|金融AI下一阶段的发展思考

    回顾过去十年,人工智能(AI)技术的发展速度让人惊叹,金融行业是现今AI应用最具潜力和最为活跃的领域之一.通过多年渗透,AI不间断从技术驱动迈向场景驱动,已广泛与金融业务深度融合,衍生出众多新业态.新 ...

  6. remove a git submodule

    参考: http://stackoverflow.com/questions/1260748/how-do-i-remove-a-submodule 1. Delete the relevant se ...

  7. Qt/C++原创项目作品精选(祖传原创/性能凶残)

    00 前言说明 从事Qt开发十年有余,一开始是做C#.NET开发的,因为项目需要,转行做嵌入式linux开发,在嵌入式linux上做可视化界面开发一般首选Qt,当然现在可选的方案很多比如安卓,但是十多 ...

  8. Qt编写地图综合应用60-覆盖物坐标和搜索

    一.前言 地图应用中有时候需要开启悬浮工具栏,用户可以直接在地图上绘制矩形.多边形.圆形.线条等,于是需要提供一个函数接口,能够获取到用户绘制的这些图形形状对应的信息.比如坐标点.圆形的中心点和半径. ...

  9. 推荐一个windows系统的下载和安装的网址:win7之家

    win7之家:http://www.windows7en.com/ 精校 完整 极致 Windows系统下载仓储站HelloWindows :https://hellowindows.cn/

  10. Helm适配华为云OBS实践分享,更方便地部署、管理复杂应用

    沃土云创开源开发者专项计划是华为给开源开发者提供专属激励资源,鼓励开发者积极参与开源 for Huawei适配,践行"让优秀开发者支持更优秀开发者"的理念. 此前我们介绍了Beam ...