SMS機能を使用してSMSを送信しています。自分の番号から連絡先リストを取得したいのですが、電話番号の編集テキストをクリックすると、電話の連絡先リストを開く必要があり、選択している連絡先番号を取得する必要があります。送信。
現在、エミュレーターでは、「SMS が失敗しました。後でもう一度試してください!」と表示されます。デバイスでそれを使用すると、連絡先をクリックした後、「Myapplication has stopped」と表示されます
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)