Android 安卓短信内容(content://sms/sent)

Android 安卓短信内容(content://sms/sent),android,contacts,android-sms,Android,Contacts,Android Sms,我在从设备读取SMS消息时遇到问题。 为URI获取内容提供程序时content://sms/inbox, 一切都很好。我可以阅读person列,在people表中找到外键,并最终找到联系人及其联系人 名字 但是,我也希望遍历发送的消息。读书时 从…起content://sms/sent,人员字段始终显示为0 要读取此字段以定位的收件人数据是否正确 发送的消息?如果是,你知道为什么我的总是0吗 我所有的测试都是在模拟器中完成的,我已经创建了3个 联络。我已经在中从emulator向这些联系人发送了

我在从设备读取SMS消息时遇到问题。 为URI获取内容提供程序时content://sms/inbox, 一切都很好。我可以阅读person列,在people表中找到外键,并最终找到联系人及其联系人 名字

但是,我也希望遍历发送的消息。读书时 从…起content://sms/sent,人员字段始终显示为0

要读取此字段以定位的收件人数据是否正确 发送的消息?如果是,你知道为什么我的总是0吗

我所有的测试都是在模拟器中完成的,我已经创建了3个 联络。我已经在中从emulator向这些联系人发送了消息 你发送信息的正常方式

只是重申一下,我可以看到4条发送的消息并阅读 关联的正文文本。我的问题是我似乎看不懂这本书
个人ID,因此我无法确定收件人是谁。

使用地址列。我猜person列被忽略了,因为人们可以向联系人列表中没有的电话号码发送短信

// address contains the phone number
Uri phoneUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, address);
if (phoneUri != null) {
  Cursor phoneCursor = getContentResolver().query(phoneUri, new String[] {Phones._ID, Contacts.Phones.PERSON_ID}, null, null, null);
  if (phoneCursor.moveToFirst()) {
    long person = phonesCursor.getLong(1); // this is the person ID you need
  }
}

这里我附上代码,我写了发送消息给用户,我从电话簿中选择

addcontact.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View V)
            {
                Intent ContactPickerIntent = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
                startActivityForResult(ContactPickerIntent, CONTACT_PICKER_RESULT);             
            }
        }
        );
这将打开联系人列表

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
         if (resultCode == RESULT_OK)
         {  
             switch (requestCode) 
             {  
             case CONTACT_PICKER_RESULT:
                 Cursor cursor=null;
                 try
                 {   
                     Uri result = data.getData();
                     Log.v(DEBUG_TAG, "Got a contact result: " + result.toString());

                     // get the contact id from the Uri     
                     String id = result.getLastPathSegment();

                     // query for everything contact number  
                     cursor = getContentResolver().query(  
                          Phone.CONTENT_URI, null,  
                          Phone.CONTACT_ID + "=?",  
                          new String[]{id}, null); 

                     cursor.moveToFirst();
                     int phoneIdx = cursor.getColumnIndex(Phone.DATA);  
                     if (cursor.moveToFirst())
                     {   
                         phonenofromcontact = cursor.getString(phoneIdx);
                         finallistofnumberstosendmsg +=","+phonenofromcontact;
                         Log.v(DEBUG_TAG, "Got email: " + phonenofromcontact);  
                     }
                     else 
                     {                                
                         Log.w(DEBUG_TAG, "No results"); 
                     }
                 }
                 catch(Exception e)
                 {
                     Log.e(DEBUG_TAG, "Failed to get contact number", e);
                 }
                 finally
                 {
                     if (cursor != null)
                     {  
                         cursor.close();
                     }
                 }
                 phonePhoneno= (EditText)findViewById(R.id.Phonenofromcontact);
                 phonePhoneno.setText(finallistofnumberstosendmsg);
                 //phonePhoneno.setText(phonenofromcontact);
                 if(phonenofromcontact.length()==0)
                 {
                     Toast.makeText(this, "No contact number found for this contact",
                             Toast.LENGTH_LONG).show(); 
                 }
                break;  
             }  
         } 
         else
         {  
             Log.w(DEBUG_TAG, "Warning: activity result not ok");
         }  
     }  
这就是你如何处理并从电话簿中获取电话号码的方法 "

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
         if (resultCode == RESULT_OK)
         {  
             switch (requestCode) 
             {  
             case CONTACT_PICKER_RESULT:
                 Cursor cursor=null;
                 try
                 {   
                     Uri result = data.getData();
                     Log.v(DEBUG_TAG, "Got a contact result: " + result.toString());

                     // get the contact id from the Uri     
                     String id = result.getLastPathSegment();

                     // query for everything contact number  
                     cursor = getContentResolver().query(  
                          Phone.CONTENT_URI, null,  
                          Phone.CONTACT_ID + "=?",  
                          new String[]{id}, null); 

                     cursor.moveToFirst();
                     int phoneIdx = cursor.getColumnIndex(Phone.DATA);  
                     if (cursor.moveToFirst())
                     {   
                         phonenofromcontact = cursor.getString(phoneIdx);
                         finallistofnumberstosendmsg +=","+phonenofromcontact;
                         Log.v(DEBUG_TAG, "Got email: " + phonenofromcontact);  
                     }
                     else 
                     {                                
                         Log.w(DEBUG_TAG, "No results"); 
                     }
                 }
                 catch(Exception e)
                 {
                     Log.e(DEBUG_TAG, "Failed to get contact number", e);
                 }
                 finally
                 {
                     if (cursor != null)
                     {  
                         cursor.close();
                     }
                 }
                 phonePhoneno= (EditText)findViewById(R.id.Phonenofromcontact);
                 phonePhoneno.setText(finallistofnumberstosendmsg);
                 //phonePhoneno.setText(phonenofromcontact);
                 if(phonenofromcontact.length()==0)
                 {
                     Toast.makeText(this, "No contact number found for this contact",
                             Toast.LENGTH_LONG).show(); 
                 }
                break;  
             }  
         } 
         else
         {  
             Log.w(DEBUG_TAG, "Warning: activity result not ok");
         }  
     }  
现在调用send msg,列出要设置的号码和消息

private void sendSMS(String phoneNumber, String message)
    {
        String SENT = "SMS_SENT";
        String DELIVERED = "SMS_DELIVERED";

        PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
            new Intent(SENT), 0);

        PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
            new Intent(DELIVERED), 0);

      //---when the SMS has been sent---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "SMS sent", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(getBaseContext(), "Generic failure", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(getBaseContext(), "No service", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(getBaseContext(), "Null PDU", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(getBaseContext(), "Radio off", 
                                Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        },new IntentFilter(SENT));

        //---when the SMS has been delivered---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "SMS delivered", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case Activity.RESULT_CANCELED:
                        Toast.makeText(getBaseContext(), "SMS not delivered", 
                                Toast.LENGTH_SHORT).show();
                        break;                        
                }
            }
        }, new IntentFilter(DELIVERED));        

        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);       
    }
这将发送消息 ................................... 你需要接收器来接收广播的信息

public class SmsReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        //---get the SMS message passed in---
        Bundle bundle = intent.getExtras();        
        SmsMessage[] msgs = null;
        String str = ""; 
        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]);                
                str += "SMS from " + msgs[i].getOriginatingAddress();                     
                str += " :";
                str += msgs[i].getMessageBody().toString();
                str += "\n";        
            }
            //---display the new SMS message---
            Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
        }   
    }
}
你也可以试试。 它对我有用。。
谢谢

我如何实现SmsReceiver类?它应该在哪里?我在其他地方看到过,在很多地方。你写这篇文章的可能性很小:我相信MobiForge上的WEIMENGLEE是原作者。