访问收件箱邮件

时间:2013-01-07 06:58:12

标签: android sms

我想访问收件箱邮件。为此,我正在编写以下代码:

Uri uriSMSURI = Uri.parse("content://sms/inbox");
Cursor cur = getContentResolver().query(uriSMSURI, null, null, null,null);
String sms = "";
cur.moveToNext();
sms = cur.getString(0);

但是我收到以下错误:

01-07 12:31:37.222: E/AndroidRuntime(687): FATAL EXCEPTION: main
01-07 12:31:37.222: E/AndroidRuntime(687): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.inboxshowingapp/com.example.inboxshowingapp.MainActivity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
01-07 12:31:37.222: E/AndroidRuntime(687):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-07 12:31:37.222: E/AndroidRuntime(687):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-07 12:31:37.222: E/AndroidRuntime(687):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-07 12:31:37.222: E/AndroidRuntime(687):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-07 12:31:37.222: E/AndroidRuntime(687):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-07 12:31:37.222: E/AndroidRuntime(687):     at android.os.Looper.loop(Looper.java:123)
01-07 12:31:37.222: E/AndroidRuntime(687):     at android.app.ActivityThread.main(ActivityThread.java:4627)
01-07 12:31:37.222: E/AndroidRuntime(687):     at java.lang.reflect.Method.invokeNative(Native Method)
01-07 12:31:37.222: E/AndroidRuntime(687):     at java.lang.reflect.Method.invoke(Method.java:521)
01-07 12:31:37.222: E/AndroidRuntime(687):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-07 12:31:37.222: E/AndroidRuntime(687):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-07 12:31:37.222: E/AndroidRuntime(687):     at dalvik.system.NativeStart.main(Native Method)
01-07 12:31:37.222: E/AndroidRuntime(687): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
01-07 12:31:37.222: E/AndroidRuntime(687):     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
01-07 12:31:37.222: E/AndroidRuntime(687):     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
01-07 12:31:37.222: E/AndroidRuntime(687):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
01-07 12:31:37.222: E/AndroidRuntime(687):     at android.database.CursorWrapper.getString(CursorWrapper.java:135)
01-07 12:31:37.222: E/AndroidRuntime(687):     at com.example.inboxshowingapp.MainActivity.onCreate(MainActivity.java:32)
01-07 12:31:37.222: E/AndroidRuntime(687):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-07 12:31:37.222: E/AndroidRuntime(687):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-07 12:31:37.222: E/AndroidRuntime(687):     ... 11 more

如何解决此问题?

2 个答案:

答案 0 :(得分:0)

首先,您必须确认已在清单文件中添加了权限。

以下权限适用于邮件

android.permission.RECEIVE_SMS

android.permission.SEND_SMS

如果是这样,那么你就错过了如果你有多个联系人以及要检索所有联系人信息的条件,那么你需要像下面那样塑造你的代码:

public List<Sms> getSms() {
    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;
}

答案 1 :(得分:0)

替换此

  cur.moveToNext();

用这个:

  cur.moveToFirst();

并将此权限设置为您的清单

  <uses-permission android:name="android.permission.READ_SMS" />