2016-10-14 61 views
-3

我已經知道如何從收件箱中讀取消息,但我想實現一個Android應用程序只讀交易消息並將其顯示在列表視圖與交易金額,信用借記等,爲我的完整代碼。 current complete code for fetching sms data。如何根據需要過濾短信數據。如何閱讀和顯示只從交易信息從收件箱到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; 
    } 
+0

沒有ü嘗試我的答案? – Piyush

回答

1

對於一個快速的想法,我想建議你幾個方法:

首先,這將是非常具有挑戰性的排序從收件箱中的任何交易類型的消息,所有你能做的就是要麼經歷每封郵件並閱讀正文並找出您需要的郵件列表,但這樣做不可行。

例如,您必須訪問地址字段,並做必要的短信,因爲短信擁有所有字段,如:地址,正文,收到日期等。

也正如你所說,你知道如何閱讀收件箱中的郵件,我正在跳過這部分。附加幾個可能對你有幫助的鏈接 ref this library,one more

1

基本上跨國消息地址包含了一些模式。例如。

AM-HDFCBK

所以看到的是,我做的正則表達式來獲取該模式相關的消息。

模式正則表達式= 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話,你可以訪問它。

+0

可以üPLZ告訴我如何使用這個示例的烏爾代碼... http://pastebin.com/YYxktV7D – Adi

+0

這是將從收件箱中獲取您的跨國消息的接收器 – Piyush

+0

好吧,讓我試試..我是新的在這個領域需要一些幫助。 – Adi

0

使用這種讀取交易信息:

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 
    } 
} 
相關問題