-1

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

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) 上工作。

如果可以的话请帮忙。

4

1 回答 1

2

要阅读收件箱的所有消息或在 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;}

短信类如下:

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" />

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

供参考运行时权限

于 2016-07-28T12:17:44.183 回答