【问题标题】:Android SMS Content (content://sms/sent)Android 短信内容 (content://sms/sent)
【发布时间】:2010-10-14 07:52:30
【问题描述】:

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

但是,我也想遍历发送的消息。读书的时候 从content://sms/sent 开始,person 字段始终显示为 0。

这是为查找收件人数据而读取的正确字段吗? 发送的消息? 如果是这样 - 知道为什么我的总是 0 吗?

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

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

【问题讨论】:

    标签: android contacts android-sms


    【解决方案1】:

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

    // 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
      }
    }
    

    【讨论】:

      【解决方案2】:

      这里我附上我编写的代码,用于向我从电话簿中选择的用户发送消息

      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");
               }  
           }  
      

      这就是你如何处理和获取电话簿中的电话号码 ..................................................... ......

      现在调用 send msg 与要设置的号码列表和 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 类?它应该在哪里?
      • 我在其他地方看到过这个,在很多地方。你是写这篇文章的人的可能性极小:google.com/…我相信 MobiForge 上的 WEIMENGLEE 是原作者。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-07
      • 1970-01-01
      • 2012-01-22
      相关资源
      最近更新 更多