2009-03-19 55 views
19

Tôi gặp sự cố khi đọc tin nhắn SMS từ thiết bị. Khi mua nhà cung cấp nội dung cho URI 'content://sms/inbox', mọi thứ đều ổn, tôi có thể đọc cột "người" để tìm khóa ngoài vào bảng người và cuối cùng liên hệ với người liên hệ và tên của họ.Nội dung SMS của Android (nội dung: // sms/đã gửi)

Tuy nhiên, tôi cũng muốn duyệt qua các thư đã gửi. Khi đọc từ 'nội dung: // sms/đã gửi', trường người luôn xuất hiện là 0. Có phải trường này đúng để đọc để xác định dữ liệu người nhận cho thư đã gửi không? Nếu vậy - bất kỳ ý tưởng nào tại sao tôi luôn luôn là 0?

Tất cả thử nghiệm của tôi đã được thực hiện trong trình mô phỏng và tôi đã tạo 3 liên hệ. Tôi đã gửi thư đến các địa chỉ liên hệ đó từ trình mô phỏng theo số cách thông thường bạn gửi tin nhắn.

Chỉ cần nhắc lại, tôi có thể xem 4 tin nhắn đã gửi và đọc văn bản nội dung liên quan . Vấn đề của tôi là tôi dường như không đọc được mã số "người" và do đó tôi không thể biết ai là người nhận.

Mọi trợ giúp sẽ được đánh giá cao.

Rất cám ơn,

Martin.

Trả lời

17

Sử dụng cột địa chỉ. Tôi đoán cột người bị bỏ qua bởi vì mọi người có thể gửi tin nhắn SMS đến số điện thoại không có trong danh sách liên lạc.

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

Đây im gắn mã mà tôi đã viết để gửi msg cho người dùng mà tôi lựa chọn từ danh bạ điện thoại

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

này sẽ mở ra danh sách liên lạc ........... ...................

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

Đây là cách bạn có thể xử lý và lấy số điện thoại từ danh bạ .......... .................................................. ......

Bây giờ gọi gửi msg với danh sách các số và msg để thiết lập ..

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

này sẽ gửi thông điệp ................... ................ U cần reciever để nhận thông điệp phát sóng

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

Bạn cũng có thể thử nó ra. Nó hoạt động cho tôi .. Cảm ơn

+0

Làm cách nào để triển khai lớp SmsReceiver? Nó nên ở đâu? –

+3

Tôi đã nhìn thấy điều này được sử dụng ở nơi khác, ở một số nơi. Tỷ lệ cá nhân của bạn là người đã viết điều này rất khó: http://www.google.com/search?q=%22private+void+sendSMS(String+phoneNumber%2C+String+message)+%7B+%22 +% 22PendingIntent + sentPI +% 3D + PendingIntent.getBroadcast% 22 & Tôi tin WEIMENGLEE trên MobiForge là tác giả gốc. – DMags

Các vấn đề liên quan