• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Grabbing phone number from contacts.

LStrike

Lurker
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:
import android.app.Activity; 
import android.app.PendingIntent; 
import android.content.Intent; 
import android.database.Cursor; 
import android.net.Uri; 
import android.os.Bundle; 
import android.telephony.SmsManager; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 
import android.provider.Contacts.People; 
import android.provider.Contacts.People.Phones; 
 
 
public class SMS extends Activity { 
     protected static final int CONTACT_ACTIVITY =  100; 
     /** Called when the activity is first  created. */ 
 
     Button button; 
     Button contactButton; 
     EditText telNr; 
     EditText msg; 
 
     @Override 
     public void  onCreate(Bundle icicle)  { 
          super.onCreate(icicle); 
          setContentView(R.layout.main); 
 
          button = (Button) findViewById(R.id.sendButton); 
          contactButton = (Button) findViewById(R.id.contactButton); 
          telNr = (EditText)  findViewById(R.id.telNr); 
          msg = (EditText)  findViewById(R.id.msg); 
 
          button.setOnClickListener(new View.OnClickListener() { 
 
               @Override 
               public void  onClick(View v) { 
                    String tNr = telNr.getText().toString(); 
                    String ms = msg.getText().toString(); 
 
                    if (tNr.length()  > 0 && ms.length() > 0) { 
                         sendSMS(tNr, ms); 
                    } else  { 
                          Toast.makeText(getBaseContext(), 
                          "Please enter both phone  number and message.", 
                          Toast.LENGTH_SHORT).show();} 
                         //msg.setText("Bitte eine  Telefonnummer und eine Nachricht eingeben"); 
                    } 
                
          }); 
           
          contactButton.setOnClickListener(new View.OnClickListener() { 
                
               @Override 
               public void  onClick(View v) { 
                    // TODO Auto-generated method stub 
                     Uri uri = Uri.parse("content://contacts/people"); 
                   Intent contacts_intent = new  Intent(Intent.ACTION_PICK,  uri); 
                   startActivityForResult(contacts_intent,  CONTACT_ACTIVITY);  
                    
               } 
          }); 
     } 
 
     private void  sendSMS(String  telNummer, String message)  { 
          PendingIntent pi = PendingIntent.getActivity(this, 0,  new Intent(this,SMS.class), 0); 
          SmsManager sms = SmsManager.getDefault(); 
          sms.sendTextMessage(telNummer,  null, message, pi, null); 
           
          telNr.setText(""); 
          msg.setText(""); 
     } 
      
     public void  onActivityResult(int  requestCode, int resultCode, Intent data){ 
           super.onActivityResult(requestCode, resultCode, data); 
            
           switch(requestCode){ 
           case(CONTACT_ACTIVITY): { 
              if(resultCode  == Activity.RESULT_OK)  { 
                Cursor c = getContentResolver().query(People.CONTENT_URI, new String[]{People.NUMBER}, null, null,  null);  
                if(c.moveToFirst()){ 
                     telNr.setText(c.getString(0)); 
                } 
                 
                 
              } 
           break; 
           } 
               
           } 
        }  
}
Code:
Here is the android manifest:


Code:
               [FONT=monospace] 
<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
      package="de.markus" 
      android:versionCode="1" 
      android:versionName="1.0"> 
    <application android:icon="@drawable/icon"  android:label="@string/app_name"> 
        <activity android:name=".SMS" 
                  android:label="@string/app_name"> 
            <intent-filter> 
                <action android:name="android.intent.action.MAIN"  /> 
                <category android:name="android.intent.category.LAUNCHER"  /> 
            </intent-filter> 
        </activity> 
 
    </application> 
    <uses-sdk android:minSdkVersion="4" /> 
     
     
    <uses-permission android:name="android.permission.SEND_SMS"/> 
    <uses-permission android:name="android.permission.RECEIVE_SMS"/> 
    <uses-permission android:name="android.permission.READ_CONTACTS"/> 
     
     
     
 
</manifest>[/FONT]
I hope it is something stupid I
 
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:
public void onActivityResult(int requestCode, int resultCode, Intent returnedIntent)
{ 
         super.onActivityResult(requestCode, resultCode, returnedIntent); 
          
         switch(requestCode)
         { 
              case CONTACT_ACTIVITY : 
                   
                   Uri pickedContactURI = returnedIntent.getData();

                
                   if(resultCode == Activity.RESULT_OK)  
                   { 
                       Cursor c = getContentResolver().query(pickedContactURI, new String[]{People.NUMBER}, null, null,  null);  
                       
                       int columnIndex = c.getColumnIndex ( People.NUMBER );
                       
                       if(c.moveToFirst())
                       { 
                          telNr.setText(  c.getString ( columnIndex)  ); 
                       } 
           
           
                   } 
              break; 
         } 
}
Also I think you may need to change CONTACT_ACTIVITY to PICK_CONTACT but I'm not 100% sure.
 
Back
Top Bottom