In my app, there is list containing phone contacts. When user clicks on a contact, he can choose from a menu to send an sms to the contact (or email, or other things).
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();
}
The intent part is designed for sending e-mail - it shows user a list to choose from clients, which is what i want. You see there are two variables for the contact name and the phone number. User clicks on a name of the list then clicks on send sms, a pop up list appears to choose from sms, email, bluetooth etc. I choose go sms or any other installed sms app from the pop up list, the client appears with the fields (to, text) empty. I want the number to appear in the sms box of the sms form. So if user clicks on "Tom Jones" and then clicks on "send sms", Tom Jones'number should already be filled in the client. My code does not do this.
I also tried sending sms with these lines, but they resulted in force close:
SmsManager sm = SmsManager.getDefault();
String number2 = "6508570720";//this should the numb开发者_如何学编程er 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);
I have noticed that, using this code randomly crashes my app (Don't know the reason for sure)
sm.sendTextMessage(number2, null, "Test SMS Message", null, null);
Instead, if I do this, it never crashes:
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);
To complete your answer, if the text is too long, the message does not go away. You have to respect max length depending of encoding. More information here SMS Manager send mutlipart message when there is less than 160 characters.
My Complete SMSMethod if you want
//TO USE EveryWhere
SMSUtils.sendSMS(context, phoneNumber, message);
//Manifest
<!-- 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);
}
}
精彩评论