开发者

IllegalArgument exception Unknown Url

开发者 https://www.devze.com 2023-03-07 15:14 出处:网络
I get an Illegal argument exception开发者_Python百科 : Unknown url for the following code wen i try and access content://sms/ at the delete part

I get an Illegal argument exception开发者_Python百科 : Unknown url for the following code wen i try and access content://sms/ at the delete part

package com.messageHider;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class smsReceiver extends BroadcastReceiver {
    Uri uri=Uri.parse("context://sms/inbox");
    @Override
    public void onReceive(Context context, Intent intent) {
         Bundle bundle=intent.getExtras();
         SmsMessage[]message=null;
         String number="";
         String body="";
         if(bundle!=null)
         {
            Object[]pdus=(Object[]) bundle.get("pdus");
            message=new SmsMessage[pdus.length];
            for(int counter=0;counter<message.length;counter++)
            {
                message[counter]=SmsMessage.createFromPdu((byte[])pdus[counter]);
                number=message[counter].getDisplayOriginatingAddress().toString();
                body=message[counter].getMessageBody().toString();
            }
         }
        //Compare
         dbConnection conn=new dbConnection(context);
         SQLiteDatabase db=conn.getReadableDatabase();
         Cursor cursor=db.query(dbConnection.TABLE_CONTACTS,null,dbConnection.CONTACT+"=?",new String[]{number},null,null,null);
         cursor.moveToFirst();
         int count=cursor.getCount();

        if(count>0)
         {
            int rows=context.getContentResolver().delete(Uri.parse("content://sms/"+number),null,null);
            if(rows>0)
            {
                   Toast.makeText(context,"Messagehidden",Toast.LENGTH_LONG).show();
            }
         }
    }

}

What seems to be the problem?


Uri uri=Uri.parse("context://sms/inbox");

It should be content scheme
Uri.parse("content://sms/inbox");

If it is about second Uri try to use ContentUris.withAppendedId(Uri.parse("content://sms/inbox"), number) to compose Uri http://developer.android.com/reference/android/content/ContentUris.html

UPDATE according comments below To have sms cursor filtered by phone number you should set WHERE string

getContentResolver().delete(
            Uri.parse("content://sms"),
            "address LIKE '%" + phoneNumber + "'",
            null);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号