无法从设备接收发送的短信(短信)

Cannot get sent sms (text messages) from device

我想从设备上获取所有 已发送 短信 (短信)。 我可以通过以下方式从收件箱中获取所有这些:

public List<SMS> getAllInboxSMS(Context ctx) {

    List<SMS> inboxSMSList = new ArrayList<>();

    try {

        Uri uriSms = Uri.parse("content://sms/inbox");
        Cursor c = ctx.getContentResolver().query(uriSms, new String[]{"_id", "thread_id", "address", "person", "date", "body"}, "read=0", null, null);

        if (c != null && c.moveToFirst()) {
            do {
                inboxSMSList.add(new SMS(c));
            } while (c.moveToNext());
        }

    } catch (Exception e) {
        Log.e("getAllInboxSMS", e.toString());
    }

    Log.i("inbox", "size: " + inboxSMSList.size());
    Log.i("inbox", inboxSMSList.toString());

    return inboxSMSList;
}

但是如果我修改

Uri.parse("content://sms/inbox");

Uri.parse("content://sms/sent");

返回的列表大小为0。

我尝试将应用程序 设置为默认短信应用程序,但它也无法正常工作

我正在研究 Lollipop (Android 5)。

如果可以请帮忙

要在 Android 中读取收件箱中的所有消息或以编程方式发送,请使用以下函数。

public List<Sms> getAllSms(String folderName) {
List<Sms> lstSms = new ArrayList<Sms>();
Sms objSms = new Sms();
Uri message = Uri.parse("content://sms/"+folderName);
ContentResolver cr = mActivity.getContentResolver();

Cursor c = cr.query(message, null, null, null, null);
mActivity.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;}

调用上述函数从收件箱或发送文件夹中获取所有消息。

getAllSms("inbox"); // Get all sms from inbox


getAllSms("sent"); // Get all sms from sent

要从收件箱或已发送文件夹中获取所有短信,请使用以下函数

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

Cursor c = cr.query(message, null, null, null, null);
mActivity.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")));
        if (c.getString(c.getColumnIndexOrThrow("type")).contains("1")) {
            objSms.setFolderName("inbox");
        } else {
            objSms.setFolderName("sent");
        }

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

return lstSms;}

短信class如下:

public class Sms{
private String _id;
private String _address;
private String _msg;
private String _readState; //"0" for have not read sms and "1" for have read sms
private String _time;
private String _folderName;


public String getId(){
return _id;
}
public String getAddress(){
return _address;
}
public String getMsg(){
return _msg;
}
public String getReadState(){
return _readState;
}
public String getTime(){
return _time;
}
public String getFolderName(){
return _folderName;
}
public void setId(String id){
_id = id;
}
public void setAddress(String address){
_address = address;
}


public void setMsg(String msg){
_msg = msg;
}

public void setReadState(String readState){
_readState = readState;
}

public void setTime(String time){
_time = time;
}
public void setFolderName(String folderName){
_folderName = folderName;
}

}

不要忘记在 AndroidManifest.xml

中定义以下权限
<uses-permission android:name="android.permission.READ_SMS" />

棉花糖设备支持您检查运行时权限

供参考runtime permission