首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android SMS内容(内容://sms/已发送)

Android SMS内容(内容://sms/已发送)
EN

Stack Overflow用户
提问于 2009-03-19 14:28:41
回答 2查看 20.5K关注 0票数 20

我在阅读设备上的短信时遇到了问题。当为URI content://sms/inbox获取内容提供者时,一切都很好。我可以阅读person列,在people表中查找外键,并最终到达联系人及其名称。

然而,我也想遍历发送的消息。从content://sms/sent读取时,person字段始终显示为0。

这是要读取的正确字段以定位发送的邮件的收件人数据吗?如果是的话-知道为什么我的总是0吗?

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

重申一下,我可以看到4条发送的消息,并阅读相关的正文文本。我的问题是,我似乎看不懂“人”的身份,因此我无法确定谁是收件人。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2009-03-19 17:29:28

使用address列。我猜person列被忽略了,因为人们可以将SMS发送到不在联系人列表中的电话号码。

代码语言:javascript
复制
// 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
  }
}
票数 18
EN

Stack Overflow用户

发布于 2010-09-01 10:17:18

这里我附加了我编写的代码,以便将msg发送给我从电话簿中选择的用户。

代码语言:javascript
复制
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);             
            }
        }
        );

本表格将公开联络人名单.

代码语言:javascript
复制
 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。

代码语言:javascript
复制
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);       
    }

这将发出信息.你需要接收者来接收广播的信息

代码语言:javascript
复制
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();
        }   
    }
}

你也可以试试。它对我有用..。谢谢

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/662420

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档