无法从设备

时间:2016-07-28 11:32:17

标签: android sms android-sms

我想从设备上获取所有发送短信(短信)。 我可以从收件箱中获取所有这些内容:

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( “内容://短信/收件箱”);

  

Uri.parse( “内容:// SMS /发送”);

返回的列表大小为0。

我尝试将应用设置为默认短信应用,但它不能正常工作

我在 Lollipop(Android 5)上工作。

如果可以,请帮忙。

1 个答案:

答案 0 :(得分: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" />

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

供参考runtime permission