Android发送短信

时间:2011-05-20 10:07:32

标签: android sms

在我的应用中,有包含手机通讯录的列表。当用户点击联系人时,他可以从菜单中选择将短信发送给联系人(或电子邮件或其他内容)。

Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
                    while(people.moveToNext()) {
                      int nameFieldColumnIndex = people.getColumnIndex(PhoneLookup.DISPLAY_NAME);
                      String contact = people.getString(nameFieldColumnIndex);
                      int numberFieldColumnIndex = people.getColumnIndex(PhoneLookup.NUMBER);
                      String number = people.getString(numberFieldColumnIndex);
                        if (contact.equals(myArr2_cs[item]))
                            {
                            Intent int_sms = new Intent(Intent.ACTION_SEND);
                            int_sms.setType("text/plain");
                            int_sms.putExtra(Intent.EXTRA_EMAIL  , new String[]{number});
                            int_sms.putExtra(Intent.EXTRA_SUBJECT, "");
                            int_sms.putExtra(Intent.EXTRA_TEXT, "");
                            try {
                               startActivity(Intent.createChooser(int_sms, "Sending SMS.."));
                            } catch (android.content.ActivityNotFoundException ex) {
                               Toast.makeText(MainActivity.this, "No installed sms clients found", Toast.LENGTH_SHORT).show();
                            }

意图部分用于发送电子邮件 - 它向用户显示可从客户端中选择的列表,这就是我想要的。 您会看到联系人姓名和电话号码有两个变量。 用户点击列表名称,然后点击发送短信,弹出列表似乎从短信,电子邮件,蓝牙等选择。我从弹出列表中选择短信或任何其他已安装的短信应用程序,客户端出现与字段(to,text)为空。我希望数字出现在短信表格的短信框中。因此,如果用户点击“Tom Jones”然后点击“发送短信”,Tom Jones的数字应该已经填入客户端。我的代码没有这样做。

我也尝试使用这些行发送短信,但他们导致强制关闭:

SmsManager sm = SmsManager.getDefault();
String number2 = "6508570720";//this should the number variable
sm.sendTextMessage(number2, null, "Test SMS Message", null, null);

OR

Intent sendIntent= new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", "smsBody");
sendIntent.putExtra("address", number);
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);

2 个答案:

答案 0 :(得分:0)

我注意到,使用此代码会随机崩溃我的应用程序(不知道原因)

sm.sendTextMessage(number2, null, "Test SMS Message", null, null);

相反,如果我这样做,它永远不会崩溃:

PendingIntent sent = PendingIntent.getBroadcast(context, 0, new Intent(), 0);
PendingIntent delivered = PendingIntent.getBroadcast(context, 0, new Intent(), 0);
sm.sendTextMessage(number2, null, "Test SMS Message", sent, delivered);

答案 1 :(得分:0)

要完成答案,如果文字太长,邮件就不会消失。您必须根据编码尊重最大长度。更多信息SMS Manager在少于160个字符时发送mutlipart消息。

如果你想要我的完整SMSMethod

//使用EveryWhere

SMSUtils.sendSMS(context, phoneNumber, message);

//清单

    <!-- SMS -->
    <uses-permission android:name="android.permission.SEND_SMS"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

 <receiver
     android:name=".SMSUtils"
     android:enabled="true"
     android:exported="true">
     <intent-filter>
         <action android:name="SMS_SENT"/>
         <action android:name="SMS_DELIVERED"/>
      </intent-filter>
 </receiver>


//JAVA
public class SMSUtils extends BroadcastReceiver {

    public static final String SENT_SMS_ACTION_NAME = "SMS_SENT";
    public static final String DELIVERED_SMS_ACTION_NAME = "SMS_DELIVERED";

    @Override
    public void onReceive(Context context, Intent intent) {
        //Detect l'envoie de sms
        if (intent.getAction().equals(SENT_SMS_ACTION_NAME)) {
            switch (getResultCode()) {
                case Activity.RESULT_OK: // Sms sent
                    Toast.makeText(context, context.getString(R.string.sms_send), Toast.LENGTH_LONG).show();
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE: // generic failure
                    Toast.makeText(context, context.getString(R.string.sms_not_send), Toast.LENGTH_LONG).show();
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE: // No service
                    Toast.makeText(context, context.getString(R.string.sms_not_send_no_service), Toast.LENGTH_LONG).show();
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU: // null pdu
                    Toast.makeText(context, context.getString(R.string.sms_not_send), Toast.LENGTH_LONG).show();
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF: //Radio off
                    Toast.makeText(context, context.getString(R.string.sms_not_send_no_radio), Toast.LENGTH_LONG).show();
                    break;
            }
        }
        //detect la reception d'un sms
        else if (intent.getAction().equals(DELIVERED_SMS_ACTION_NAME)) {
            switch (getResultCode()) {
                case Activity.RESULT_OK:
                    Toast.makeText(context, context.getString(R.string.sms_receive), Toast.LENGTH_LONG).show();
                    break;
                case Activity.RESULT_CANCELED:
                    Toast.makeText(context, context.getString(R.string.sms_not_receive), Toast.LENGTH_LONG).show();
                    break;
            }
        }
    }

    /**
     * Test if device can send SMS
     * @param context
     * @return
     */
    public static boolean canSendSMS(Context context) {
        return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY);
    }

    public static void sendSMS(final Context context, String phoneNumber, String message) {

        if (!canSendSMS(context)) {
            Toast.makeText(context, context.getString(R.string.cannot_send_sms), Toast.LENGTH_LONG).show();
            return;
        }

        PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, new Intent(SENT_SMS_ACTION_NAME), 0);
        PendingIntent deliveredPI = PendingIntent.getBroadcast(context, 0, new Intent(DELIVERED_SMS_ACTION_NAME), 0);

        final SMSUtils smsUtils = new SMSUtils();
        //register for sending and delivery
        context.registerReceiver(smsUtils, new IntentFilter(SMSUtils.SENT_SMS_ACTION_NAME));
        context.registerReceiver(smsUtils, new IntentFilter(DELIVERED_SMS_ACTION_NAME));

        SmsManager sms = SmsManager.getDefault();
        ArrayList<String> parts = sms.divideMessage(message);

        ArrayList<PendingIntent> sendList = new ArrayList<>();
        sendList.add(sentPI);

        ArrayList<PendingIntent> deliverList = new ArrayList<>();
        deliverList.add(deliveredPI);

        sms.sendMultipartTextMessage(phoneNumber, null, parts, sendList, deliverList);

        //we unsubscribed in 10 seconds 
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                context.unregisterReceiver(smsUtils);
            }
        }, 10000);

    }
}