无法从内容提供商处检索某些数据

时间:2015-03-05 07:42:18

标签: android

此处应用程序接收传入消息并存储到内容提供程序“putSMSToDatabase(...)”中。

然后我需要从内容提供程序数据库中获取/检索字符串值“BALANCE”(即String mydbresult = cursor.getString(indexCardBalance)),如下所示,但无法执行此操作。

FYI:代码行“int indexCardBalance = cursor.getColumnIndex(BALANCE)”返回“-1”;如果这不是预期值,那我该如何处理呢?

有人能建议我解决方案吗?

代码如下:

public class SmsReceiver extends BroadcastReceiver 
{   
    public static final String SMS_URI = "content://sms";   
    public static final String ADDRESS = "address";
    public static final String DATE = "date";
    public static final String READ = "read";
    public static final String STATUS = "status";
    public static final String TYPE = "type";
    public static final String BALANCE = "balance";
    public static final String BODY = "body";
    public static final String SEEN = "seen";
    public static final int MESSAGE_TYPE_INBOX = 1;
    public static final int MESSAGE_TYPE_SENT = 2;    
    public static final int MESSAGE_IS_NOT_READ = 0;
    public static final int MESSAGE_IS_READ = 1;    
    public static final int MESSAGE_IS_NOT_SEEN = 0;
    public static final int MESSAGE_IS_SEEN = 1;
    public static final double cardBalance = 1000.00;

    // user input the AES secret key
    public static final String secretKeyString = "1122334455667788";;

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

     Bundle bundle = intent.getExtras();

     if (bundle != null) {
         // Specify the bundle to get object based on SMS protocol "pdus"
         Object[] object = (Object[]) bundle.get("pdus");
         SmsMessage sms[] = new SmsMessage[object.length]; 

         // Get ContentResolver object for pushing encrypted SMS to incoming folder
         ContentResolver contentResolver = context.getContentResolver();
         String msgContent = "";
         String originNum = "";

            for (int i = 0; i < object.length; i++) {
                sms[i] = SmsMessage.createFromPdu((byte[]) object[i]);
                //get the sender phone number
                originNum = sms[i].getDisplayOriginatingAddress();   
                // get the received SMS content
                msgContent = sms[i].getDisplayMessageBody();     
                // Put encrypted incoming sms into database
                putSmsToDatabase( contentResolver, sms, object, msgContent);

                try{
                        Cursor cursor = contentResolver.query( Uri.parse( "content://sms/inbox" ), null, null, null, null);
                        int indexCardBalance = cursor.getColumnIndex( BALANCE );
                        do
                        {
                            String mydbresult = cursor.getString(indexCardBalance );
                            double strdbAmnt = Double.parseDouble(mydbresult);
                        }
                        while( cursor.moveToNext() );
                    }
                } 

                catch(Exception e){
                    Toast.makeText(context, "abc", Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                }
            }

        }

    }

    // Put incoming sms into database
    private void putSmsToDatabase( ContentResolver contentResolver, SmsMessage[] sms, Object[] object, String msgContent )
    {
        for (int i = 0; i < object.length; i++){
        // Create SMS row
        ContentValues values = new ContentValues();
        values.put( ADDRESS, sms[i].getOriginatingAddress() );
        values.put( DATE, sms[i].getTimestampMillis() );
        values.put( READ, MESSAGE_IS_NOT_READ );
        values.put( STATUS, sms[i].getStatus() );
        values.put( TYPE, MESSAGE_TYPE_INBOX );
        values.put( SEEN, MESSAGE_IS_NOT_SEEN );
        values.put( BALANCE, cardBalance );
        values.put( BODY, msgContent );

        contentResolver.insert( Uri.parse( SMS_URI ), values );        
        }
    }

}

0 个答案:

没有答案