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:
Here is the android manifest:
I hope it is something stupid I
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:
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]