android 实现监听短信接收并将当前位置发送出去

时间:2022-02-13 08:58:08

1、获取定位地址,这里使用高德定位,从官网下载定位的jar包AMap_Location_V3.50_20170731.jar包然后在build.gradle

compile files('libs/AMap_Location_V3.5.0_20170731.jar')
然后在权限文件中添加需要的权限和申请的key

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.a550211.patentproject">

<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_CONTACTS"/>

<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>


<!--用于进行网络定位-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<!--用于访问GPS定位-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<!--用于获取运营商信息,用于支持提供运营商信息相关的接口-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<!--用于访问wifi网络信息,wifi信息会用于进行网络定位-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<!--用于获取wifi的获取权限,wifi信息会用来进行网络定位-->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<!--用于访问网络,网络定位需要上网-->
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<!--用于读取手机当前的状态-->
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<!--用于写入缓存数据到扩展存储卡-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<!--用于申请调用A-GPS模块-->
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"></uses-permission>
<!--用于申请获取蓝牙信息进行室内定位-->
<uses-permission android:name="android.permission.BLUETOOTH"></uses-permission>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"></uses-permission>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:name=".MyApplication"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<receiver android:name=".SMSBroadcastReceiver">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>

<meta-data android:name="com.amap.api.v2.apikey" android:value="12026fe07b5e1fd21ea299e7474edd65">//开发者申请的key
</meta-data>


<service android:name="com.amap.api.location.APSService"></service>
</application>

</manifest>
定位地址获取代码

 private void getLocation(Context context) {
mLocationClient = new AMapLocationClient(context);
mLocationOption = new AMapLocationClientOption();
mLocationClient.setLocationListener((AMapLocationListener) context);
mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
mLocationOption.setInterval(2000);

mLocationClient.setLocationOption(mLocationOption);
mLocationClient.startLocation();

mLocationOption.setNeedAddress(true);
mLocationOption.setWifiActiveScan(false);
}

@Override
public void onLocationChanged(AMapLocation amapLocation) {
if (amapLocation != null) {
if (amapLocation.getErrorCode() == 0) {

SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss ");
Date curDate = new Date(System.currentTimeMillis());//获取当前时间
String str = formatter.format(curDate);

position = amapLocation.getCountry() + amapLocation.getProvince() + amapLocation.getCity() + amapLocation.getDistrict() + amapLocation.getStreet()
+ amapLocation.getStreetNum() + amapLocation.getAoiName() + " buildId=" + amapLocation.getAddress() + " floor=" + amapLocation.getFloor();
Log.e(TAG, "position=" + position + " data = " + str);
} else {
Log.e("AmapError", "location Error, ErrCode:"
+ amapLocation.getErrorCode() + ", errInfo:"
+ amapLocation.getErrorInfo());
}
}
}


/**
* //定位成功回调信息,设置相关消息
amapLocation.getLocationType();//获取当前定位结果来源,如网络定位结果,详见定位类型表
amapLocation.getLatitude();//获取纬度
amapLocation.getLongitude();//获取经度
amapLocation.getAccuracy();//获取精度信息
amapLocation.getAddress();//地址,如果option中设置isNeedAddress为false,则没有此结果,网络定位结果中会有地址信息,GPS定位不返回地址信息。
amapLocation.getCountry();//国家信息
amapLocation.getProvince();//省信息
amapLocation.getCity();//城市信息
amapLocation.getDistrict();//城区信息
amapLocation.getStreet();//街道信息
amapLocation.getStreetNum();//街道门牌号信息
amapLocation.getCityCode();//城市编码
amapLocation.getAdCode();//地区编码
amapLocation.getAoiName();//获取当前定位点的AOI信息
amapLocation.getBuildingId();//获取当前室内定位的建筑物Id
amapLocation.getFloor();//获取当前室内定位的楼层
*/
2、短信发送方法

 private void sendSms(String phone, String message, Context context) {

SmsManager smsManager = SmsManager.getDefault();
if (message.length() <= 70) {
smsManager.sendTextMessage(phone, null, message, null, null);
} else {
List<String> smsDivs = smsManager.divideMessage(message);
for (String ss : smsDivs) {
smsManager.sendTextMessage(phone, null, ss, null, null);
}
}
}
3、监听短信进来方法

if (intent.getAction().equals(ACTION)) {
StringBuffer SMSAddress = new StringBuffer();
StringBuffer SMSContent = new StringBuffer();
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdusObjects = (Object[]) bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[pdusObjects.length];
for (int i = 0; i < pdusObjects.length; i++) {
messages[i] = SmsMessage
.createFromPdu((byte[]) pdusObjects[i]);
}
for (SmsMessage message : messages) {
SMSAddress.append(message.getDisplayOriginatingAddress());
SMSContent.append(message.getDisplayMessageBody());

Log.e(TAG, "发送号码:" + SMSAddress + "\n" + "短信内容:" + SMSContent + " number=" + apkDao.query().get(0));
String phone = SMSAddress.toString();
phone = phone.replace("+86", "");
sendSms(phone, position, context);
}
}
}
4、完整代码

package com.example.a550211.patentproject;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class SMSBroadcastReceiver extends BroadcastReceiver implements AMapLocationListener {
private final String TAG = "SMSBroadcastReceiver";
private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";

//声明mLocationOption对象
public AMapLocationClientOption mLocationOption = null;
private AMapLocationClient mLocationClient;
private String position = null;

@Override
public void onReceive(Context context, Intent intent) {
ApkDao apkDao = new ApkDao(context);
if (intent.getAction().equals(ACTION)) {
StringBuffer SMSAddress = new StringBuffer();
StringBuffer SMSContent = new StringBuffer();
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdusObjects = (Object[]) bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[pdusObjects.length];
for (int i = 0; i < pdusObjects.length; i++) {
messages[i] = SmsMessage
.createFromPdu((byte[]) pdusObjects[i]);
}
for (SmsMessage message : messages) {
SMSAddress.append(message.getDisplayOriginatingAddress());
SMSContent.append(message.getDisplayMessageBody());

Log.e(TAG, "发送号码:" + SMSAddress + "\n" + "短信内容:" + SMSContent + " number=" + apkDao.query().get(0));
String phone = SMSAddress.toString();
phone = phone.replace("+86", "");
sendSms(phone, position, context);
}
}
}
}

/**
* 发送短信
* @param phone
* @param message
* @param context
*/
private void sendSms(String phone, String message, Context context) {

SmsManager smsManager = SmsManager.getDefault();
if (message.length() <= 70) {
smsManager.sendTextMessage(phone, null, message, null, null);
} else {
List<String> smsDivs = smsManager.divideMessage(message);
for (String ss : smsDivs) {
smsManager.sendTextMessage(phone, null, ss, null, null);
}
}
}

private void getLocation(Context context) {
mLocationClient = new AMapLocationClient(context);
mLocationOption = new AMapLocationClientOption();
mLocationClient.setLocationListener((AMapLocationListener) context);
mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
mLocationOption.setInterval(2000);

mLocationClient.setLocationOption(mLocationOption);
mLocationClient.startLocation();

mLocationOption.setNeedAddress(true);
mLocationOption.setWifiActiveScan(false);
}

@Override
public void onLocationChanged(AMapLocation amapLocation) {
if (amapLocation != null) {
if (amapLocation.getErrorCode() == 0) {

SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss ");
Date curDate = new Date(System.currentTimeMillis());//获取当前时间
String str = formatter.format(curDate);

position = amapLocation.getCountry() + amapLocation.getProvince() + amapLocation.getCity() + amapLocation.getDistrict() + amapLocation.getStreet()
+ amapLocation.getStreetNum() + amapLocation.getAoiName() + " buildId=" + amapLocation.getAddress() + " floor=" + amapLocation.getFloor();
Log.e(TAG, "position=" + position + " data = " + str);
} else {
Log.e("AmapError", "location Error, ErrCode:"
+ amapLocation.getErrorCode() + ", errInfo:"
+ amapLocation.getErrorInfo());
}
}
}


/**
* //定位成功回调信息,设置相关消息
amapLocation.getLocationType();//获取当前定位结果来源,如网络定位结果,详见定位类型表
amapLocation.getLatitude();//获取纬度
amapLocation.getLongitude();//获取经度
amapLocation.getAccuracy();//获取精度信息
amapLocation.getAddress();//地址,如果option中设置isNeedAddress为false,则没有此结果,网络定位结果中会有地址信息,GPS定位不返回地址信息。
amapLocation.getCountry();//国家信息
amapLocation.getProvince();//省信息
amapLocation.getCity();//城市信息
amapLocation.getDistrict();//城区信息
amapLocation.getStreet();//街道信息
amapLocation.getStreetNum();//街道门牌号信息
amapLocation.getCityCode();//城市编码
amapLocation.getAdCode();//地区编码
amapLocation.getAoiName();//获取当前定位点的AOI信息
amapLocation.getBuildingId();//获取当前室内定位的建筑物Id
amapLocation.getFloor();//获取当前室内定位的楼层
*/
}
这样就可以实现监听发送过来的短信并发送当前地址回去。