1

我正在使用短信功能发送短信,我想从我的号码中获取联系人列表,如果我单击 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)
4

1 回答 1

1

您可以参考以下内容并进行修改。我在手机上对其进行了测试。请发布崩溃的 logcat 详细信息,我们只能猜测错误。

您正在单击按钮内调用完成(),这将完成活动。不确定您要做什么。

MainActivity.java

public class SendSmsActivity extends Activity {
Button buttonSend,get;
private static final int CONTACT_PICKER_RESULT = 1001;
EditText phoneNo;
EditText textSMS;
int columnIndex_number;
static String stringNumber;
String sms;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.send);
    phoneNo = (EditText)findViewById(R.id.editText1);
    textSMS = (EditText) findViewById(R.id.editText2);
    get = (Button) findViewById(R.id.button1);
    get.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
             Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
                    Contacts.CONTENT_URI);
            startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);


        }
        });
    buttonSend = (Button) findViewById(R.id.button2);    
    buttonSend.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v){
          try {

              String sms =  textSMS.getText().toString().trim();
                Toast.makeText(getApplicationContext(), phoneNo.getText().toString()+sms,
                        Toast.LENGTH_LONG).show();
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(phoneNo.getText().toString(), null, sms, 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();
          }


        }
    });
}

 @Override  
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
 if (resultCode == RESULT_OK) {  
    switch (requestCode) {  
    case CONTACT_PICKER_RESULT:

        Cursor cursor = null;  
        String phoneNumber = "";
        List<String> allNumbers = new ArrayList<String>();
        int phoneIdx = 0;
        try {  
            Uri result = data.getData();  
            String id = result.getLastPathSegment();  
            cursor = getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + "=?", new String[] { id }, null);  
            phoneIdx = cursor.getColumnIndex(Phone.DATA);
            if (cursor.moveToFirst()) {
                while (cursor.isAfterLast() == false) {
                    phoneNumber = cursor.getString(phoneIdx);
                    allNumbers.add(phoneNumber);
                    phoneNo.setText(cursor.getString(phoneIdx));

                    cursor.moveToNext();
                }
            } else {
                //no results actions
            }  
        } catch (Exception e) {  
           //error actions
        } finally {  
            if (cursor != null) {  
                cursor.close();
            } 
        }  
        break;  
    }  
} else {
   //activity result error actions
}  
}
}

发送.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
 <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginTop="35dp"
    android:layout_toRightOf="@+id/button1"
    android:text="Phone No"
    android:textSize="25sp" />

  <EditText
    android:id="@+id/editText1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginTop="78dp"
    android:padding="20dp"
    android:ems="10" >

    <requestFocus />
   </EditText>

    <TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp"
    android:text="SMS Content"
    android:textSize="25sp" />
    <EditText
    android:id="@+id/editText2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:layout_marginTop="20dp"
    android:padding="20dp" />
    <LinearLayout 
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="horizontal" 
     android:paddingTop="20dp"
    >

    <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:layout_marginLeft="34dp"
    android:text="Get" />

    <Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="60dp"
    android:paddingLeft="20dp"
    android:text="Send" />

 </LinearLayout>
 </LinearLayout>

在清单中添加权限

  <uses-permission android:name="android.permission.READ_CONTACTS"/>
  <uses-permission android:name="android.permission.SEND_SMS" />

来自模拟器的快照。如果您需要发送短信,则必须使用真实设备

如果数字之间有空格,您可以修剪空格

在此处输入图像描述

于 2013-06-07T10:06:15.577 回答