private void intentfun(String phoneNumber, String message) {
Intent smsIntent = new Intent(Intent.ACTION_SENDTO);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.setData(Uri.parse("smsto:"+phoneNumber));
// smsIntent.putExtra("address", phoneNumber);
smsIntent.putExtra("sms_body",message);
startActivity(smsIntent);
}
1)这是我的代码。当我运行此代码时,在按摩窗口中打开并显示按摩api,其中用户编号和短信体已经填满。这个消息准备发送。我必须点击发送按钮然后按摩将发送到用户的号码。但我不想要这个窗口。我想发送短信而不显示用户编号和短信正文已经填满的用户按摩盒。有可能吗?
2)
private void sendSMS(String phoneNumber, String message) {
SmsManager sms = SmsManager.getDefault();
PendingIntent sentPI;
String SENT = "SMS_SENT";
sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
sms.sendTextMessage(phoneNumber, null, message, sentPI, null);
}
此代码也不适用于我的三星s7562手机。我已经在项目中定义了权限。
<uses-permission android:name="android.permission.SEND_SMS" />
当此代码执行时没有任何反应,没有错误,没有异常,没有通话时间余额扣除。
答案 0 :(得分:0)
尝试这样:
String phoneNumber = "0123456789";
String message = "Hello World!";
SmsManager smsManager = SmsManager.getDefault();
ArrayList<String> parts = smsManager.divideMessage(message);
smsManager.sendMultipartTextMessage(phoneNumber, null, parts, null, null);
并将此代码粘贴到AndroidManifest文件中。
<uses-permission android:name="android.permission.SEND_SMS" />
答案 1 :(得分:0)
import java.util.ArrayList;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.telephony.SmsManager;
import android.util.Log;
import android.widget.Button;
import android.widget.Toast;
public class SMSSender {
private Context context;
private SMSData smsData;
private enum ACTIONS {
ACTION_SENT("SENT"), ACTION_DELEVERED("SMS_DELIVERED");
String action;
ACTIONS(String act) {
action = act;
}
public String getAction() {
return action;
}
}
public static enum SMS_RESULT {
SMS_SUCCESS, SMS_FAIL;
}
// private String SENT = "SMS_SENT";
// private String DELIVERED = "SMS_DELIVERED";
private BroadcastReceiver sendMessageRecevier, messageDeliveryReceiver;
private Button btn;
public SMSSender(Context ctx) {
this.context = ctx;
}
private void init() {
}
public void setSMSData(SMSData data) {
smsData = data;
}
public void send() throws IllegalStateException {
if (smsData == null) {
throw new IllegalStateException();
}
sendSMS();
}
// ---sends a SMS message to another device---
private void sendSMS() {
PendingIntent sentPI = PendingIntent.getBroadcast(context, 0,
new Intent(ACTIONS.ACTION_SENT.getAction()), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(context, 0,
new Intent(ACTIONS.ACTION_DELEVERED.getAction()), 0);
registerSentReceiver();
registerDeliveryReceiver();
// ---when the SMS has been delivered---
SmsManager sms = SmsManager.getDefault();
try {
ArrayList<String> parts = sms.divideMessage(smsData.getPayload());
int numParts = parts.size();
ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>();
for (int i = 0; i < numParts; i++) {
sentIntents.add(sentPI);
deliveryIntents.add(deliveredPI);
}
sms.sendMultipartTextMessage(smsData.target, null, parts,
sentIntents, deliveryIntents);
Log.v("TEST", " Send sms phone " + smsData.target
+ " message " + smsData.getPayload());
Toast.makeText(context, "Sending SMS...", Toast.LENGTH_SHORT)
.show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(context, "Error Message", Toast.LENGTH_SHORT).show();
}
}
private void registerDeliveryReceiver() {
messageDeliveryReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
SMSResultReceiver receiver = smsData.getSMSResultReceiver();
int result = getResultCode();
if (receiver == null) {
return;
}
if (Activity.RESULT_OK == result) {
receiver.onDelivered(SMS_RESULT.SMS_SUCCESS);
} else {
receiver.onDelivered(SMS_RESULT.SMS_FAIL);
}
}
};
context.registerReceiver(messageDeliveryReceiver, new IntentFilter(
ACTIONS.ACTION_DELEVERED.getAction()));
}
private void registerSentReceiver() {
sendMessageRecevier = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
SMSResultReceiver receiver = smsData.getSMSResultReceiver();
int result = getResultCode();
if (receiver == null) {
return;
}
if (Activity.RESULT_OK == result) {
addToSentLog();
receiver.onSent(SMS_RESULT.SMS_SUCCESS);
} else {
receiver.onSent(SMS_RESULT.SMS_FAIL);
}
}
};
// ---when the SMS has been sent---
context.registerReceiver(sendMessageRecevier, new IntentFilter(
ACTIONS.ACTION_SENT.getAction()));
}
private void addToSentLog() {
try {
ContentValues values = new ContentValues();
values.put("address", smsData.target);
values.put("body", smsData.getPayload());
context.getContentResolver().insert(
Uri.parse("content://sms/sent"), values);
} catch (Exception e) {
e.printStackTrace();
}
}
}
答案 2 :(得分:0)
在清单文件
下添加权限<uses-permission android:name="android.permission.SEND_SMS" />
使用以下代码发送短信
注意:下面的代码也支持长消息,并将消息分成部分发送短信
public void sendMessage(String number, String message) {
SmsManager manager = SmsManager.getDefault();
String message = appPreferenceManager.getUserSelectedSafeModeMessage();
ArrayList<String> parts = manager.divideMessage(message);
manager.sendMultipartTextMessage(number, null, parts, null, null);
}