我正在制作一个记录来自特定发件人的消息的应用程序。例如,我想在 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();
}
}
}