Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 访问通讯录中的联系人发送短信?_Java_Android_Android Edittext_Sms_Android Contacts - Fatal编程技术网

Java 访问通讯录中的联系人发送短信?

Java 访问通讯录中的联系人发送短信?,java,android,android-edittext,sms,android-contacts,Java,Android,Android Edittext,Sms,Android Contacts,所以我的手机短信课开始工作了。但是,我希望当用户点击编辑文本输入电话号码时…手机的电话簿会加载,用户可以从中选择一个或多个联系人,短信会转到所有选择的联系人。如何使用短信息通讯录加载和选择联系人 我有点卡住了,请给我一个解释:) 类别: Button btnSendSMS; EditText txtPhoneNo; EditText txtMessage; private static final int ACTION_PICK_CONTACT=1; /*

所以我的手机短信课开始工作了。但是,我希望当用户点击编辑文本输入电话号码时…手机的电话簿会加载,用户可以从中选择一个或多个联系人,短信会转到所有选择的联系人。如何使用短信息通讯录加载和选择联系人

我有点卡住了,请给我一个解释:)

类别:

    Button btnSendSMS;
    EditText txtPhoneNo;
    EditText txtMessage;

  private static final int ACTION_PICK_CONTACT=1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_sms);
        btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
        txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);
        txtMessage = (EditText) findViewById(R.id.txtMessage);

        txtPhoneNo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
            startActivityForResult(i, ACTION_PICK_CONTACT);
          }
        });


        btnSendSMS.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                String phoneNo = txtPhoneNo.getText().toString();
                String message = txtMessage.getText().toString() + displayLocation();
                displayLocation();

                if (phoneNo.length()>0 && message.length()>0)
                    sendSMS(phoneNo, message);
                else
                    Toast.makeText(getBaseContext(),
                            "Please enter both phone number and message.",
                            Toast.LENGTH_SHORT).show();
            }
        });


    }


    public void onActivityResult(int reqCode, int resCode, Intent data) {
        super.onActivityResult(reqCode, resCode, data);

        switch (reqCode) {
            case (ACTION_PICK_CONTACT) :
                if (resCode == Activity.RESULT_OK) {

                    Uri contact = data.getData();
                    Cursor cursor =  managedQuery(contact, null, null, null, null);
                    if (cursor.moveToFirst()) {
                        String contact_id =cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));

                        Cursor phoneCursor = getContentResolver().query(
                                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contact_id,
                                null, null);
                        phoneCursor.moveToFirst();
                        String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex("data1")); //THE NUMBER TO SEND YOUR SMS
                    }
                }
                break;
        }
    }

    private String displayLocation(){
            LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, new LocationListener(){
                @Override
                public void onStatusChanged(String s, int i, Bundle bundle) {}
                @Override
                public void onProviderEnabled(String s) {}
                @Override
                public void onProviderDisabled(String s) {}
                @Override
                public void onLocationChanged(final Location location) {}
            });
            Location myLocation = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
            double longitude = myLocation.getLongitude();
            double latitude = myLocation.getLatitude();
           return "https://www.google.co.id/maps/@"+latitude+","+longitude;
    }





    //---sends a SMS message to another device---
    private void sendSMS(String phoneNumber, String message)
    {

        PendingIntent pi = PendingIntent.getActivity(this, 0,
                new Intent(this, Home.class), 0);
            SmsManager sms = SmsManager.getDefault();
            sms.sendTextMessage(phoneNumber, null, message, pi, null);


        String SENT = "SMS_SENT";
        String DELIVERED = "SMS_DELIVERED";

        PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
                new Intent(SENT), 0);

        PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
                new Intent(DELIVERED), 0);

        //---when the SMS has been sent---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "SMS sent",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case android.telephony.gsm.SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(getBaseContext(), "Generic failure",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case android.telephony.gsm.SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(getBaseContext(), "No service",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case android.telephony.gsm.SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(getBaseContext(), "Null PDU",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case android.telephony.gsm.SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(getBaseContext(), "Radio off",
                                Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        }, new IntentFilter(SENT));

        //---when the SMS has been delivered---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "SMS delivered",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case Activity.RESULT_CANCELED:
                        Toast.makeText(getBaseContext(), "SMS not delivered",
                                Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        }, new IntentFilter(DELIVERED));

        android.telephony.gsm.SmsManager smms = android.telephony.gsm.SmsManager.getDefault();
        smms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
    }
像这样使用它:

int ACTION_PICK_CONTACT=1;

//Call this in your onclick event
Intent i = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(i, ACTION_PICK_CONTACT);

//This is called when user has selected a contact
public void onActivityResult(int reqCode, int resCode, Intent data) {
   super.onActivityResult(reqCode, resultCode, data);

   switch (reqCode) {
     case (ACTION_PICK_CONTACT) :
        if (resCode == Activity.RESULT_OK) {

          Uri contact = data.getData();
          Cursor cursor =  managedQuery(contact, null, null, null, null);
          if (cursor.moveToFirst()) {
            String contact_id =cursor.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));

            Cursor phoneCursor = getContentResolver().query( 
                   ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, 
                   ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contact_id, 
                   null, null);
            phoneCursor.moveToFirst();
            String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex("data1")); //THE NUMBER TO SEND YOUR SMS
            txtPhoneNo.setText(phoneNumber);
          }
        }
        break;
    }
}

使用'new Intent(Intent.ACTION_PICK,Contacts.CONTENT_URI)'@itechevo…我已经尝试过这样的技巧:txtPhoneNo.setOnClickListener(onNewIntent(Intent.ACTION_PICK,Contacts.Contacts.CONTENT_URI));并使实现方法返回null。还是不起作用?你能详细说明一下吗:)@itechevo…我已经用你的代码编辑了我的代码,做了一些更改,因为有错误。它会加载电话簿,但当我单击某个用户时,它不会出现在带有号码的编辑文本中?为什么不存储?是否使用从onActivityResult中提取的号码设置编辑文本框?@itechevo…我没有字符串phoneNumber,并尝试仅引用txtPhoneNo,但这不起作用,因此我该怎么办?我相信这是最后一行了?在
String phoneNumber=phoneCursor.getString(phoneCursor.getColumnIndex(“数据1”)之后尝试
txtPhoneNo.setText(phoneNumber)
@itechevo…谢谢你,真的很感激…我怎么能有不止一个选择呢?比如选择多个联系人?