如何从Android中的收件箱中删除上次收到的短信

时间:2013-06-27 18:58:08

标签: android

我正在开发一个Android应用程序,我想在其中用一些新文本替换上次收到短信的邮件正文。

我正在使用BroadcastReceiver,其中我想将最后收到的短信的邮件正文存储在变量中,并从收件箱中删除短信,而现在删除后我想在收件箱中放入新的编码邮件。< / p>

现在我面临的问题是如何从收件箱中删除最后收到的短信。我已经在这方面开发了一些代码但它从收件箱中删除了second last(之前的)短信。请检查下面的代码,并帮助我继续我的应用程序,我将非常感谢您的善意行为。

public void deleteLastSMS()
    {

//      abortBroadcast();

        String body = null;
        String num = null;

        try
        {
            Uri uri = Uri.parse("content://sms/inbox");         
            Cursor c =contex.getContentResolver().query(uri, null, null ,null,null); 
            if(c.moveToFirst()) 
            { 
                body = c.getString(c.getColumnIndexOrThrow("body")).toString();
                num = c.getString(c.getColumnIndexOrThrow("address")).toString();
            }


            int id = c.getInt(0);
            int thread_id = c.getInt(1);
            Uri thread = Uri.parse( "content://sms");
            contex.getContentResolver().delete( thread, "thread_id=? and _id=?", new String[]{String.valueOf(thread_id), String.valueOf(id)} );

        }

        catch(CursorIndexOutOfBoundsException ee)
        {

        }


    }

1 个答案:

答案 0 :(得分:0)

自从过去一小时以来,我一直在看这个,这是我到目前为止找到的东西,我帮忙:)

void deleteMessage(Context context){
    Uri uriSms = Uri.parse("content://sms/inbox");
    Cursor c = context.getContentResolver().query(uriSms, null,null,null,null);
    int id = c.getInt(0);
    int thread_id = c.getInt(1); //get the thread_id
    context.getContentResolver().delete(Uri.parse("content://sms/conversations/" + thread_id),null,null);
}

void deleteSMS(Context context){
    Uri deleteUri = Uri.parse("content://sms");
    context.getContentResolver().delete(deleteUri, "address=? and date=?", new String[] {strMessageFrom,strTimeStamp});
}

public void deleteSMS1(Context context, String message, String number) {
    try {
        Uri uriSms = Uri.parse("content://sms/inbox");
        Cursor c = context.getContentResolver().query(
                uriSms,
                new String[] { "_id", "thread_id", "address", "person",
                        "date", "body" }, "read=0", null, null);

        if (c != null && c.moveToFirst()) {
            do {
                long id = c.getLong(0);
                long threadId = c.getLong(1);
                String address = c.getString(2);
                String body = c.getString(5);
                String date = c.getString(3);
                Log.e("log>>>",
                        "0>" + c.getString(0) + "1>" + c.getString(1)
                                + "2>" + c.getString(2) + "<-1>"
                                + c.getString(3) + "4>" + c.getString(4)
                                + "5>" + c.getString(5));
                Log.e("log>>>", "date" + c.getString(0));

                if (message.equals(body) && address.equals(number)) {
                    // mLogger.logInfo("Deleting SMS with id: " + threadId);
                    context.getContentResolver().delete(
                            Uri.parse("content://sms/" + id), "date=?",
                            new String[] { c.getString(4) });
                    Log.e("log>>>", "Delete success.........");
                }
            } while (c.moveToNext());
        }
    } catch (Exception e) {
        Log.e("log>>>", e.toString());
    }
}