2009-03-19 27 views
19

端末からのSMSメッセージの読み取りに問題があります。 URI「content://sms/inbox」のコンテンツプロバイダを取得するときには、 はすべて問題ありません。「人物」列を読んで という外部キーを見つけ、最終的に連絡先とその という名前にアクセスできます。Android SMSコンテンツ(コンテンツ:// SMS /送信済み)

しかし、送信されたメッセージもまた行きたいと思っています。 を 'content:// sms/sent'から読み取ると、人物フィールドは常に0と表示されます。 の送信者メッセージの受信者データを見つけるために、 はこれを正しいフィールドにしていますか?もしそうなら、私の考え方はいつも0です。

私のすべてのテストはエミュレータで行われており、私は3 という連絡先を作成しました。私は のエミュレータからそれらの連絡先にメッセージを送信しました。通常の方法でメッセージを送信します。

繰り返しておきますと、送信された4件のメッセージが表示され、本文に関連付けられている を読むことができます。私の問題は、 「人物」IDを読み取ることができないため、受信者が誰であるかを理解できないことです。

ご協力いただければ幸いです。

多くのありがとう、

マーティン。

答えて

17

アドレス欄を使用してください。私は人々が連絡先リストにない電話番号に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);    
      } 
     } 
     ); 

から選択するユーザーにMSGを送信するために書かれているコードを取り付けるイム........... ...................

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

これは、uは電話帳 から電話番号を処理して得ることができる方法です.......... .................................................. ......

今数と設定する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);  
    } 

これはメッセージ を送信します................... ................ Uがメッセージを受け取るためにレシーバが必要 はあなたにもそれを試してみることができます

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

を放送しました。 それは私のために働く。 ありがとう

+0

どのように私はSmsReceiverクラスを実装できますか?それはどこにあるべきですか? –

+3

私はこれを、他の場所で、さまざまな場所で使用しています。あなたがこれを書いた人である可能性は非常に低いです:http://www.google.com/search?q=%22private+void+sendSMS(String+phoneNumber%2C+String+message)+%7B+%22 +%22PendingIntent + sentPI +%3D + PendingIntent.getBroadcast%22&私は、MobiForgeのWEIMENGLEEがオリジナルの著者だと思います。 – DMags

関連する問題