3

我正在开发一个 Android 应用程序,我想用一些新文本替换上次收到的 SMS 的消息正文。

我正在使用BroadcastReceiver其中我想将最后收到的短信的消息正文存储在一个变量中并从收件箱中删除短信,现在删除后我想在收件箱中放入一条新的编码消息。

现在我面临的问题是如何从收件箱中删除最后收到的短信。我在这方面开发了一些代码,但它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)
        {

        }


    }
4

1 回答 1

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());
    }
}
于 2017-02-12T16:07:17.160 回答