0

我正在制作一个记录来自特定发件人的消息的应用程序。例如,我想在 Listview 中从银行 HDFC 获取所有 SMS。我的收件箱中的发件人地址被命名为“AM-HDFC”。我尝试了以下代码,但应用程序崩溃说找不到 URI。可能的解决方案是什么?

public class MsgReader extends AppCompatActivity {
    Cursor c;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_msg_reader);
        List<Sms> smslist=getAllSms("inbox");
        RecyclerView rView = (RecyclerView) findViewById(R.id.list);
        rView.setLayoutManager(new LinearLayoutManager(this));
        rView.setHasFixedSize(true);
        MsgAdapter rcAdapter = new MsgAdapter(smslist);
        rView.setAdapter(rcAdapter);

    }
    public List<Sms> getAllSms(String folderName) {

        List<Sms> lstSms = new ArrayList<Sms>();
        Sms objSms = new Sms();
        Uri message = Uri.parse("content://sms/");
        ContentResolver cr = this.getContentResolver();

        c= cr.query(Uri.parse("content://mms-sms/AM-HDFC"), null, null, null, null);
        this.startManagingCursor(c);
        int totalSMS = c.getCount();

        if (c.moveToFirst()) {
            for (int i = 0; i < totalSMS; i++) {

                objSms = new Sms();
                objSms.setId(c.getString(c.getColumnIndexOrThrow("_id")));
                objSms.setAddress(c.getString(c.getColumnIndexOrThrow("address")));
                objSms.setMsg(c.getString(c.getColumnIndexOrThrow("body")));
                objSms.setReadState(c.getString(c.getColumnIndex("read")));
                objSms.setTime(c.getString(c.getColumnIndexOrThrow("date")));

                lstSms.add(objSms);
                c.moveToNext();
            }
        }
         else {
         throw new RuntimeException("You have no SMS in " + folderName);
         }
        c.close();

        return lstSms;
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (c != null) {
            c.close();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (c != null) {
            c.close();
        }
    }
}
4

1 回答 1

0

尝试这个 :

public void getSmsInbox(Context context, String address) {
    String selection = Telephony.Sms.ADDRESS+"='"+address+"'";

    ContentResolver cr = context.getContentResolver();
    Cursor c = cr.query(Telephony.Sms.CONTENT_URI, null, selection, null, null);
    if (c != null) {
        if (c.moveToFirst()) {
            for (int j = 0; j < c.getCount() ; j++) {
                String smsDate = c.getString(c.getColumnIndexOrThrow(Telephony.Sms.DATE));
                Date dateFormat = new Date(Long.valueOf(smsDate));
                String body = c.getString(c.getColumnIndexOrThrow(Telephony.Sms.BODY));
                Log.d(TAG, address+", "+dateFormat.toString()+" : "+body);
                c.moveToNext();
            }
        }
        c.close();
    } else {
        Toast.makeText(this, "No message to show!", Toast.LENGTH_SHORT).show();
    }
}

像这样使用它:

getSmsInbox(MainActivity.this, "AM-HDFC");

并且不要忘记请求许可:

<uses-permission android:name="android.permission.READ_SMS" />
于 2017-09-24T10:36:59.100 回答