我正在创建一个记录来自特定发件人的邮件的应用程序。例如,我想在Listview中从银行HDFC获取所有短信。收件箱中的发件人地址被命名为& #34; 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();
}
}
}
答案 0 :(得分:0)
试试这个:
img=cv2.imread("D:\\Matlab\\Images\\d.jpg",cv2.IMREAD_COLOR)
像这样使用:
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");