我正在使用短信功能发送短信,我想从我的号码中获取联系人列表,如果我单击 phoneno 的编辑文本,它必须打开我手机的联系人列表,并且它必须获取我选择的联系人号码,它必须发送。
现在在模拟器中显示“短信失败,请稍后再试!” 如果我在设备中使用它,单击任何联系人后,它会显示“我的应用程序已停止”
Sendsmsactivity.java
public class SendSMSActivity extends Activity {
public static final int RQS_PICKCONTACT = 1;
Button buttonSend;
//EditText textPhoneNo;
EditText phoneNo;
EditText textSMS;
int columnIndex_number;
static String stringNumber;
String sms;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
buttonSend = (Button) findViewById(R.id.buttonSend);
textSMS = (EditText) findViewById(R.id.editTextSMS);
/*textPhoneNo = (EditText) findViewById(R.id.editTextPhoneNo);
textPhoneNo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(Intent.ACTION_PICK, People.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
//Uri uri = Uri.parse("smsto:" + textPhoneNo);
//Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
}
});*/
phoneNo = (EditText)findViewById(R.id.phoneNo);
phoneNo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final Uri uriContact = ContactsContract.Contacts.CONTENT_URI;
Intent intentPickContact = new Intent(Intent.ACTION_PICK, uriContact);
startActivityForResult(intentPickContact, RQS_PICKCONTACT);
}
});
/*Bundle extras = getIntent().getExtras();
if(extras !=null) {
String sms = extras.getString("firstKeyName");
}*/
Intent intent1= getIntent(); // gets the previously created intent
final String firstKeyName = intent1.getStringExtra("firstKeyName");
textSMS.setText(firstKeyName);
buttonSend.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//String phoneNo = textPhoneNo.getText().toString();
try {
SmsManager smsManager = SmsManager.getDefault();
//String name = null;
smsManager.sendTextMessage(stringNumber, null, firstKeyName, null, null);
Toast.makeText(getApplicationContext(), "SMS Sent!",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again later!",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
finish();
}
});
}
/*@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT):
if (resultCode == Activity.RESULT_OK) {
//Uri uri = Uri.parse("smsto:" + textPhoneNo);
//Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
final String name = c.getString(c.getColumnIndexOrThrow(People.NAME));
textPhoneNo.setText(name);
}
}
break;
}
}*/
protected void onActivityResult(int requestCode,int resultCode,Intent data)
{
if(resultCode == RESULT_OK)
{
if(requestCode == RQS_PICKCONTACT)
{
Uri returnUri = data.getData();
Cursor cursor = getContentResolver().query(returnUri, null, null, null, null);
if(cursor.moveToNext()){
int columnIndex_ID = cursor.getColumnIndex(ContactsContract.Contacts._ID);
String contactID = cursor.getString(columnIndex_ID);
int columnIndex_HASPHONENUMBER = cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
final String stringHasPhoneNumber = cursor.getString(columnIndex_HASPHONENUMBER);
if(stringHasPhoneNumber.equalsIgnoreCase("1")){
Cursor cursorNum = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactID,null,null);
//Get the first phone number
if(cursorNum.moveToNext())
{
columnIndex_number = cursorNum.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
final String stringNumber = cursorNum.getString(columnIndex_number);
phoneNo.setText(stringNumber);
}
}else{
phoneNo.setText("NO Phone Number");
}
}else{
Toast.makeText(getApplicationContext(), "NO data!", Toast.LENGTH_LONG).show();
}
}
}
}
}
它没有从我的手机获取联系人,我的代码中是否有任何错误。
Logcat error
06-07 10:32:03.543: E/AndroidRuntime(1015): FATAL EXCEPTION: main
06-07 10:32:03.543: E/AndroidRuntime(1015): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.PICK dat=content://contacts }
06-07 10:32:03.543: E/AndroidRuntime(1015): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1622)
06-07 10:32:03.543: E/AndroidRuntime(1015): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
06-07 10:32:03.543: E/AndroidRuntime(1015): at android.app.Activity.startActivityForResult(Activity.java:3370)
06-07 10:32:03.543: E/AndroidRuntime(1015): at android.app.Activity.startActivityForResult(Activity.java:3331)
06-07 10:32:03.543: E/AndroidRuntime(1015): at com.example.jsonandroid.SendSMSActivity$1.onClick(SendSMSActivity.java:41)
06-07 10:32:03.543: E/AndroidRuntime(1015): at android.view.View.performClick(View.java:4202)
06-07 10:32:03.543: E/AndroidRuntime(1015): at android.view.View$PerformClick.run(View.java:17340)
06-07 10:32:03.543: E/AndroidRuntime(1015): at android.os.Handler.handleCallback(Handler.java:725)
06-07 10:32:03.543: E/AndroidRuntime(1015): at android.os.Handler.dispatchMessage(Handler.java:92)
06-07 10:32:03.543: E/AndroidRuntime(1015): at android.os.Looper.loop(Looper.java:137)
06-07 10:32:03.543: E/AndroidRuntime(1015): at android.app.ActivityThread.main(ActivityThread.java:5039)
06-07 10:32:03.543: E/AndroidRuntime(1015): at java.lang.reflect.Method.invokeNative(Native Method)
06-07 10:32:03.543: E/AndroidRuntime(1015): at java.lang.reflect.Method.invoke(Method.java:511)
06-07 10:32:03.543: E/AndroidRuntime(1015): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-07 10:32:03.543: E/AndroidRuntime(1015): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-07 10:32:03.543: E/AndroidRuntime(1015): at dalvik.system.NativeStart.main(Native Method)