2011-05-20 110 views
0

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

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)为空。我希望号码出现在短信窗体的短信框中。因此,如果用户点击“汤姆琼斯”,然后点击“发送短信”,汤姆琼斯的号码应该已经填写在客户端。我的代码不这样做。

我也试图与这些线路发送短信,但他们造成了强制关闭:

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); 
+0

我做类似的东西在这里! http://stackoverflow.com/questions/14452808/sending-and-receiving-mms-in-android – toobsco42 2013-01-22 07:51:26

回答

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); 
+0

“上下文无法解析为变量”。如果我将此更改为Main.this,则强制再次关闭。 – erdomester 2011-05-20 14:16:38

+0

哦..顺便说一下,你在哪里放这个代码?我的意思是,在什么样的班级里面?如果你在一个Activity中使用它,那么你需要用'this'替换'context'。不是'Main.this'。如果您没有将代码放置在Activity中,那么您需要提供有关您的应用程序结构的更多详细信息以及放置此代码的位置和原因。 – 2011-05-22 04:33:22

+0

我把它放在一个活动中。谢谢 :) – erdomester 2011-05-22 11:40:57

0

要完成答案,如果文本太长,消息不会消失。您必须考虑编码的最大长度。更多信息此处短信管理器在少于160个字符时发送mutlipart消息。

我,如果你想

//到处使用

SMSUtils.sendSMS(context, phoneNumber, message); 

完全SMSMethod //清单

<!-- 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); 

    } 
}