以编程方式删除Android SMS

我想在我的Android应用程序中自动删除某些短信。因此,我有一种方法可以完全按照我的意愿去做。但是,仅当我将应用程序直接从Eclipse部署到我的手机时,它才有效。然后,它将删除传入的SMS。但是,如果从市场下载应用程序,则无法使用。但是也没有错误。有人知道我如何解决这个问题,或者这只能在有根设备上起作用吗?


public void deleteSMS(Context context, String message, String number) {

    try {

        mLogger.logInfo("Deleting SMS from inbox");

        Uri uriSms = Uri.parse("content://sms/inbox");

        Cursor c = context.getContentResolver().query(uriSms,

            new String[] { "_id", "thread_id", "address",

                "person", "date", "body" }, null, 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);


                if (message.equals(body) && address.equals(number)) {

                    mLogger.logInfo("Deleting SMS with id: " + threadId);

                    context.getContentResolver().delete(

                        Uri.parse("content://sms/" + id), null, null);

                }

            } while (c.moveToNext());

        }

    } catch (Exception e) {

        mLogger.logError("Could not delete SMS from inbox: " + e.getMessage());

    }

}


慕虎7371278
浏览 703回答 3
3回答

青春有我

将应用程序设置为默认应用程序,请参见此。删除之前,请检查您的应用是否为默认短信应用。使用电话类提供的URI而不是硬编码。public void deleteSMS(Context context,int position){    Uri deleteUri = Uri.parse(Telephony.Sms.CONTENT_URI);    int count = 0;    Cursor c = context.getContentResolver().query(deleteUri, new String[]{BaseColumns._ID}, null,            null, null); // only query _ID and not everything        try {              while (c.moveToNext()) {                // Delete the SMS                String pid = c.getString(Telephony.Sms._ID); // Get _id;                String uri = Telephony.Sms.CONTENT_URI.buildUpon().appendPath(pid)                count = context.getContentResolver().delete(uri,                    null, null);              }        } catch (Exception e) {        }finally{          if(c!=null) c.close() // don't forget to close the cursor        }   }它删除所有(收件箱,发件箱,草稿)短信。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android