我已经知道如何从收件箱中读取消息,但我想实现一个android应用程序到只读交易消息并在列表视图中显示它与交易金额,信用卡借记卡等。当前完整的代码获取短信数据。如何根据需要过滤短信数据。
public List<SmsInfo> getSmsInfo() {
String[] projection = new String[] { "_id", "address", "person",
"body", "date", "type" };
// @SuppressWarnings("deprecation")
// Cursor cursor = activity.managedQuery(uri, projection, null, null,
// "date desc");
ContentResolver cr = activity.getContentResolver();
Cursor cursor = cr.query(uri, projection, null, null, "date desc");
int nameColumn = cursor.getColumnIndex("person");
int phoneNumberColumn = cursor.getColumnIndex("address");
int smsbodyColumn = cursor.getColumnIndex("body");
int dateColumn = cursor.getColumnIndex("date");
int typeColumn = cursor.getColumnIndex("type");
if (cursor != null) {
int i = 0;
while (cursor.moveToNext() && i++ < 20) {
SmsInfo smsInfo = new SmsInfo();
smsInfo.setName(cursor.getString(nameColumn));
smsInfo.setDate(dateFromLongToString(cursor.getString(dateColumn)));
smsInfo.setPhoneNumber(cursor.getString(phoneNumberColumn));
smsInfo.setSmsbody(cursor.getString(smsbodyColumn));
smsInfo.setType(cursor.getString(typeColumn));
String personName = getPeople2(smsInfo.getPhoneNumber());
smsInfo.setName(null == personName ? smsInfo.getPhoneNumber()
: personName);
infos.add(smsInfo);
}
cursor.close();
}
return infos;
}
基本上跨国消息地址包含一些模式。例如。
AM-HDFCBK
因此,我用正则表达式来获取与模式相关的消息。
Pattern regEx =Pattern.compile("[a-zA-Z0-9] {2} [a-zA-Z0-9]{6}");
protected BroadcastReceiver myReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
String format = bundle.getString("format");
currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i], format);
Log.e("Current Message", format + " : " + currentMessage.getDisplayOriginatingAddress());
} else {
currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
}
Pattern regEx =
Pattern.compile("[a-zA-Z0-9]{2}-[a-zA-Z0-9]{6}");
Matcher m = regEx.matcher(currentMessage.getDisplayOriginatingAddress());
if (m.find()) {
try {
String phoneNumber = m.group(0);
Long date = currentMessage.getTimestampMillis();
String message = currentMessage.getDisplayMessageBody();
Log.e("SmsReceiver Mine", "senderNum: " + phoneNumber + "; message: " + message);
} catch (Exception e) {
e.printStackTrace();
}
} else {
Log.e("Mismatch", "Mismatch value");
}
}
}
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" + e);
}
}
};
之后,您可以检查消息正文是否包含像credited , debited
这样的单词,您可以访问它。
作为一个快速的想法,我想给你一些建议:
首先,从收件箱中对任何交易类型的消息进行排序将是相当具有挑战性的,您所能做的就是浏览每条消息并阅读正文并找到所需的消息列表,但这也不可行。
例如,您必须访问地址字段,并做必要的短信拥有所有字段,如:地址,正文,接收日期等。
也正如你提到的,你知道如何从收件箱中读取消息,我跳过了这一部分。附上一些链接,这可能会帮助你
读取事务消息:
private void readMessages(){
final int textViewID = searchView.getContext().getResources().
getIdentifier("android:id/search_src_text", null, null);
final AutoCompleteTextView searchTextView = (AutoCompleteTextView)
searchView.findViewById(textViewID);
try {
Field mCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
mCursorDrawableRes.setAccessible(true);
mCursorDrawableRes.set(searchTextView, 0); //This sets the cursor resource ID to 0 or @null which will make it visible on white background
} catch (Exception e) {}
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String dateVal = "";
Cursor cursor = this.getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);
if (cursor.moveToFirst()) { // must check the result to prevent exception
do {
String msgData = cursor.getString(cursor.getColumnIndexOrThrow("body")).toString();
String date = cursor.getString(cursor.getColumnIndexOrThrow("date")).toString();
Long dateV = Long.parseLong(date);
int start = 0;
int end = 0;
String msg = "";
String add = cursor.getString(2);
dateVal = formatter.format(new Date(dateV));
if(!(spam.contains(add) || promo.contains(add))) {
if(msgData.contains("credited")|| msgData.contains("debited") || msgData.contains("withdrawn")) {
messages.add(dateVal + ":" + msgData + "axqw" + add);
contentMessage.add(msgData);
}
}
} while (cursor.moveToNext());
} else {
// empty box, no SMS
}
}
每个事务消息都具有以下特征:
- 发送方的格式始终为XX-XXXX
- 消息中会有一个/c, account no .
- 将会有诸如可用平衡、可用平衡、平衡、组合平衡、可用平衡等关键词。
- 将有交易关键字,如借记,贷记,付款。
如果你能结合所有这些条件,那么你会发现一个事务性消息。
这里有一个可能有用的库https://github.com/minimal-scouser/trny