如何使用BroadcastReceiver删除传入的Sms?

时间:2012-05-22 10:55:54

标签: java android

我一直在开发可以使用SMS命令管理设备的应用程序;应用程序必须从SMS执行命令并删除此Sms。所以,管理工作的代码,但我不能删除短信。我有以下BroadcastReceiver:

public class SmsReceiver extends BroadcastReceiver {

    private static final Uri SMS_INBOX_URI = Uri.parse("content://sms");

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        String body = "";
        String number = "";

        Bundle bundle = intent.getExtras();
        SmsMessage[] msgs = null;
        if (bundle != null) {
            // ---retrieve the SMS message received---
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];
            for (int i = 0; i < msgs.length; i++) {
                msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                body += msgs[i].getMessageBody().toString();
                number +=msgs[i].getOriginatingAddress();
            }
        }

        SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(context);
        String key=prefs.getString(context.getString(R.string.acc_key_key), "");

        if (body.trim().equals("#"+key)) {
            sendGPSCoordinates(context, number);
        } else if (body.trim().equals("?"+key)){
            sendResponces(context);
        }
        deleteSms(body, number, context);
    }

    private void deleteSms(String body, String number, Context context) {

        ContentResolver resolver=context.getContentResolver();
        int count=resolver.delete(SMS_INBOX_URI, "body = ? AND address = ?", new String[]{body, number});
        Log.e("count", String.valueOf(count));
/*      Cursor results=resolver.query(SMS_INBOX_URI, null, null, null, null);
        for (String str:results.getColumnNames()) {
            Log.e("column", str);
        }
}

我拥有具有所需权限的AndroidManifest.xml。应用程序接收SMS,从Intent获取SMS数据,管理手机并尝试删除此SMS,但它总是返回count等于0.我试图更改sms / inbox的URI,但它不起作用。请告诉我,我该怎么做?谢谢。

3 个答案:

答案 0 :(得分:1)

我认为,您知道,SMS-Message是按订购广播发送的。因此,您的Reciever能够在本机SMS应用程序获得它之前获取它们(仅在Manifest中设置比本机更高的优先级)。因此,它应该只是cancel正在发送的广播,SMS永远不会到达本机sms-app。

你可以这样试试:

public class SmsReceiver extends BroadcastReceiver {

private static final Uri SMS_INBOX_URI = Uri.parse("content://sms");

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    String body = "";
    String number = "";

    Bundle bundle = intent.getExtras();
    SmsMessage[] msgs = null;
    if (bundle != null) {
        // ---retrieve the SMS message received---
        Object[] pdus = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];
        for (int i = 0; i < msgs.length; i++) {
            msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
            body += msgs[i].getMessageBody().toString();
            number +=msgs[i].getOriginatingAddress();
        }
    }

    SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(context);
    String key=prefs.getString(context.getString(R.string.acc_key_key), "");

    if (body.trim().equals("#"+key)) {
        sendGPSCoordinates(context, number);
    } else if (body.trim().equals("?"+key)){
        sendResponces(context);
    }
    abortBroadcast();
}

答案 1 :(得分:0)

@Override
    public final void onReceive(final Context context, final Intent intent) {
        final String action = intent.getAction();
         Bundle extras = intent.getExtras();

            String messages = "";


            if ( extras != null )
            {
                // Get received SMS array
                Object[] smsExtra = (Object[]) extras.get( SMS_EXTRA_NAME );

                // Get ContentResolver object for pushing encrypted SMS to the incoming folder
               // ContentResolver contentResolver = context.getContentResolver();

                for ( int i = 0; i < smsExtra.length; ++i )
                {
                    SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]);

                    String body = sms.getMessageBody().toString();
                    String address = sms.getOriginatingAddress();
                    long date = sms.getTimestampMillis();

}

在此写入查询之后获取最后一条消息

然后触发你的删除方法。

它可以删除最后收到的消息。

你可以理解它。

答案 2 :(得分:0)

希望这可以帮助你...

@覆盖     public void onReceive(Context context,Intent intent)     {

    Bundle bundle = intent.getExtras();
    SmsMessage[] msgs = null;
    String number="";
    String Message="";      
    if (bundle != null)
    {
        //---retrieve the SMS message received---
        Object[] pdus = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];         
        for (int i=0; i<msgs.length; i++){
            msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);             
            number = msgs[i].getOriginatingAddress();  
            Message = Message + msgs[i].getMessageBody().toString();
        }
        //---display the new SMS message---
        Toast.makeText(context, number,Toast.LENGTH_LONG).show();
        Log.d("number:::", number);            
        PubVar.IN_SMS++;
        Savesms( number,Message,PubVar.PHONENO, context);
        long counted=deleteSMS(context);
        //long count=counted++;
        //PubVar.DELETED_SMS=counted;
        System.out.println("Deleted sms :"+ counted);
    }

}

//message deletion
private int deleteSMS(Context context)
{
    Uri deleteUri = Uri.parse("content://sms");
    int count = 0;
    Cursor c = context.getContentResolver().query(deleteUri, null, null,null, null);
    while (c.moveToNext()) 
    {
        try
        {
            // Delete the SMS

            String pid = c.getString(0); // Get id;
            String uri = "content://sms/" + pid;
            count = context.getContentResolver().delete(Uri.parse(uri), null, null);
            PubVar.DELETED_SMS=PubVar.DELETED_SMS+count;
        }
        catch (Exception e) 
        {
            System.out.println("error in deleting " + e.toString());
        }
    }
    c.close();

    return count;

}