【问题标题】:Can't show a layout and delete sms at the same time?不能同时显示布局和删除短信?
【发布时间】:2014-11-12 17:35:09
【问题描述】:

我想快速删除从某个电话号码收到的短信,以至于它不会显示给用户(为了“阻止”这个向用户发送垃圾邮件的电话号码)。我还希望我的 android 应用程序显示布局,但似乎不可能同时进行!

这是我删除短信的方法:

 public void deleteSMS(Context context, 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" }, null, null, null);

        if (c != null && c.moveToFirst()) {
            do {
                long id = c.getLong(0);
                String address = c.getString(2);


                if (address.equals(number)) {

                    context.getContentResolver().delete(
                        Uri.parse("content://sms/" + id), null, null);
                    Toast.makeText(this, "SMS deleted", Toast.LENGTH_SHORT).show();
                }
            } while (c.moveToNext());
        }
    } catch (Exception e) {

    }

然后,为了阻止电话号码“123456”,我这样做了:

setContentView(R.layout.activity_main);
int r = 0;
         while (r<1){
             deleteSMS(this, "123456");}

每当我发送一条短信时,该应用程序都会完美删除短信,但它不显示任何布局或祝酒词。当我这样做时:

setContentView(R.layout.activity_main);
     deleteSMS(this, "123456");

它显示所有布局、祝酒词和删除短信。你有解决办法吗?

【问题讨论】:

  • it show all the layout, toasts, and delete the sms ...这不是你想要的吗?
  • 这就是我想要的,但它只有在我删除一次短信时才有效(在第二种情况下)!我希望它在 deleteSMS() 总是重新启动时工作(就像第一种情况一样)。谢谢你的回复:)
  • 我很困惑,为什么你要删除的不仅仅是你收到的消息?如果您在收到一条消息时删除它,并且您没有来自该号码的任何其他消息,则不应该有任何其他消息要删除,所以我不确定我是否理解这个问题。对不起:|
  • 它必须删除从某人那里收到的所有短信。自从应用程序启动后,过去从这个人那里收到的所有短信或将来(在应用程序运行时)将收到的短信都会被立即删除。第二个代码“setContentView(R.layout.activity_main); deleteSMS(this, "123456");”只需删除过去收到的短信即可。
  • 让 deleteSMS 返回一个布尔值(如果成功则返回 true,否则返回 false),如果失败则中断 while 循环(while(r

标签: android android-layout sms smsmanager


【解决方案1】:

只是给你一个开始,因为(在多次 cmets 之后)你似乎打算在应用程序始终运行的地方这样做)

public class MainActivity extends Activity{

    private final static int SERVICE_ID = 1234;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Intent notificationIntent = new Intent(this, DeleteSmsService.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent. FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        Notification notice = new Notification.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle(getString(R.string.app_name))
            .setContentText("Deleting SMS Messages")
            .setContentIntent(pendingIntent).build();

        notice.flags |= Notification.FLAG_NO_CLEAR;

        startForeground(SERVICE_ID, notice);
    }
}

然后创建一个服务

public DeleteSmsService extends Service{

    private ContentResolver contentResolver;

    @Override
    public void onCreate(){
        super.onCreate();

        contentResolver = getContext().getContentResolver();

        new Thread(){
            @Override
            public void run(){
                while(true){
                    deleteSms(contentResolver, "123456");
                }
            }
        }.start();
    }

    public void deleteSMS(ContentResolver cr, String number) { 

        try {

            Uri uriSms = Uri.parse("content://sms/inbox");
            Cursor c = cr.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);
                    String address = c.getString(2);

                    if (address.equals(number)) {
                        cr.delete(Uri.parse("content://sms/" + id), null, null);
                        Toast.makeText(this, "SMS deleted", Toast.LENGTH_SHORT).show();
                    }
                } while (c.moveToNext());
            }
        } catch (Exception e) {

    }
}

我还没有测试过这些,我不知道其中是否有错误,或者格式是否可能关闭,因为我只是快速输入了它。这是为了让你开始做你想做的最好的事情,而不会陷入 UI 线程并保持正常的约定。您可能需要使用线程处理程序,但不确定(再次没有测试过)。

同样,我不会这样做,但这只是为了帮助您开始尝试做的事情,因为这是您似乎打算做的事情。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多