Grabbing phone number from contacts.


Last Updated:

  1. LStrike

    LStrike New Member This Topic's Starter

    Joined:
    Feb 25, 2010
    Messages:
    3
    Likes Received:
    0
    Hello there,

    I have a problem. I want to use phone numbers from my contact list to send sms with my own application.
    If I use two emulators it works pretty good, but on my real device (T-Mobile G1 with Android 1.6) not.


    Is there anybody who has an idea?
    I have read a lot of posts and tutorials and copy some lines of code.

    My code:


    Code (Text):
    1.  
    2. import android.app.Activity;
    3. import android.app.PendingIntent;
    4. import android.content.Intent;
    5. import android.database.Cursor;
    6. import android.net.Uri;
    7. import android.os.Bundle;
    8. import android.telephony.SmsManager;
    9. import android.view.View;
    10. import android.widget.Button;
    11. import android.widget.EditText;
    12. import android.widget.Toast;
    13. import android.provider.Contacts.People;
    14. import android.provider.Contacts.People.Phones;
    15.  
    16.  
    17. public class SMS extends Activity {
    18.      protected static final int CONTACT_ACTIVITY =  100;
    19.      /** Called when the activity is first  created. */
    20.  
    21.      Button button;
    22.      Button contactButton;
    23.      EditText telNr;
    24.      EditText msg;
    25.  
    26.      @Override
    27.      public void  onCreate(Bundle icicle)  {
    28.           super.onCreate(icicle);
    29.           setContentView(R.layout.main);
    30.  
    31.           button = (Button) findViewById(R.id.sendButton);
    32.           contactButton = (Button) findViewById(R.id.contactButton);
    33.           telNr = (EditText)  findViewById(R.id.telNr);
    34.           msg = (EditText)  findViewById(R.id.msg);
    35.  
    36.           button.setOnClickListener(new View.OnClickListener() {
    37.  
    38.                @Override
    39.                public void  onClick(View v) {
    40.                     String tNr = telNr.getText().toString();
    41.                     String ms = msg.getText().toString();
    42.  
    43.                     if (tNr.length()  > 0 && ms.length() > 0) {
    44.                          sendSMS(tNr, ms);
    45.                     } else  {
    46.                           Toast.makeText(getBaseContext(),
    47.                           "Please enter both phone  number and message.",
    48.                           Toast.LENGTH_SHORT).show();}
    49.                          //msg.setText("Bitte eine  Telefonnummer und eine Nachricht eingeben");
    50.                     }
    51.                
    52.           });
    53.            
    54.           contactButton.setOnClickListener(new View.OnClickListener() {
    55.                
    56.                @Override
    57.                public void  onClick(View v) {
    58.                     // TODO Auto-generated method stub
    59.                      Uri uri = Uri.parse("content://contacts/people");
    60.                    Intent contacts_intent = new  Intent(Intent.ACTION_PICK,  uri);
    61.                    startActivityForResult(contacts_intent,  CONTACT_ACTIVITY);  
    62.                    
    63.                }
    64.           });
    65.      }
    66.  
    67.      private void  sendSMS(String  telNummer, String message)  {
    68.           PendingIntent pi = PendingIntent.getActivity(this, 0,  new Intent(this,SMS.class), 0);
    69.           SmsManager sms = SmsManager.getDefault();
    70.           sms.sendTextMessage(telNummer,  null, message, pi, null);
    71.            
    72.           telNr.setText("");
    73.           msg.setText("");
    74.      }
    75.      
    76.      public void  onActivityResult(int  requestCode, int resultCode, Intent data){
    77.            super.onActivityResult(requestCode, resultCode, data);
    78.            
    79.            switch(requestCode){
    80.            case(CONTACT_ACTIVITY): {
    81.               if(resultCode  == Activity.RESULT_OK)  {
    82.                 Cursor c = getContentResolver().query(People.CONTENT_URI, new String[]{People.NUMBER}, null, null,  null);  
    83.                 if(c.moveToFirst()){
    84.                      telNr.setText(c.getString(0));
    85.                 }
    86.                  
    87.                  
    88.               }
    89.            break;
    90.            }
    91.                
    92.            }
    93.         }  
    94. }
    Code (Text):
    1.  
    2.    
    Here is the android manifest:


    Code (Text):
    1.  
    2.                [FONT=monospace]
    3. <?xml version="1.0" encoding="utf-8"?>
    4. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    5.       package="de.markus"
    6.       android:versionCode="1"
    7.       android:versionName="1.0">
    8.     <application android:icon="@drawable/icon"  android:label="@string/app_name">
    9.         <activity android:name=".SMS"
    10.                   android:label="@string/app_name">
    11.             <intent-filter>
    12.                 <action android:name="android.intent.action.MAIN"  />
    13.                 <category android:name="android.intent.category.LAUNCHER"  />
    14.             </intent-filter>
    15.         </activity>
    16.  
    17.     </application>
    18.     <uses-sdk android:minSdkVersion="4" />
    19.      
    20.      
    21.     <uses-permission android:name="android.permission.SEND_SMS"/>
    22.     <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    23.     <uses-permission android:name="android.permission.READ_CONTACTS"/>
    24.      
    25.      
    26.      
    27.  
    28. </manifest>[/FONT]
    29.      
    I hope it is something stupid I
     

    Advertisement
  2. alostpacket

    alostpacket Over Macho Grande? VIP Member

    Joined:
    Nov 29, 2009
    Messages:
    7,978
    Likes Received:
    3,603
    Erm... I know this probably isnt the answer you're looking for but...

    People.CONTENT_URI was deprecated in Eclair (2.0) and wont return a phone number from anyone running 2.0 +

    So you may want to rethink the whole thing :(


    But as for your code, the problem seems to be that when you get the activity result you aren't calling the right method on the cursor. You need to get the column index first and you need to be sure that you are calling the specific contact the user picked. This is how I would write onActivityResult:

    Code (Text):
    1.  
    2. public void onActivityResult(int requestCode, int resultCode, Intent returnedIntent)
    3. {
    4.          super.onActivityResult(requestCode, resultCode, returnedIntent);
    5.          
    6.          switch(requestCode)
    7.          {
    8.               case CONTACT_ACTIVITY :
    9.                    
    10.                    Uri pickedContactURI = returnedIntent.getData();
    11.  
    12.                
    13.                    if(resultCode == Activity.RESULT_OK)  
    14.                    {
    15.                        Cursor c = getContentResolver().query(pickedContactURI, new String[]{People.NUMBER}, null, null,  null);  
    16.                        
    17.                        int columnIndex = c.getColumnIndex ( People.NUMBER );
    18.                        
    19.                        if(c.moveToFirst())
    20.                        {
    21.                           telNr.setText(  c.getString ( columnIndex)  );
    22.                        }
    23.            
    24.            
    25.                    }
    26.               break;
    27.          }
    28. }
    29.  
    Also I think you may need to change CONTACT_ACTIVITY to PICK_CONTACT but I'm not 100% sure.
     

Share This Page

Loading...