我想发送MMS,但看来Intent无法正常工作,并且无法格式化SmsManager pdu。
这行不通-可以在位图上很好地查看图像,但是我无法创建pdu。如何获得为SmsManager.sendMultimediaMessage正确格式化的PDU?我发现的文档往往很旧。我找不到一种简单的方法来验证我是否正确制作了PDU。协助使用此功能将有所帮助。
@TargetApi(Build.VERSION_CODES.N)
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
protected void sendText(String message, byte[] image, String phoneNo, MmsMessage myMessage){
// sending message passthrough to text
sendText(message, phoneNo);
我可以从一条消息中得到一张照片,这行得通。
SavePhotoTask saveToFile = new SavePhotoTask();
SmsManager smsManager = SmsManager.getDefault();
String path = saveToFile.doInBackground(image);
Uri uri = Uri.parse("file://"+ path);
这是我尝试使用https://github.com/Xlythe/AndroidTextManager 这没有触发或发送彩信。
//TextManager manager = TextManager.getInstance(context);
// manager.send(message).to(phoneNo);
这是我尝试使用意图的尝试。它只是将消息放入android消息的文本区域,但不发送消息。它也不会附加图像。我猜这是为了传递内部消息:
// Intent i = new Intent(Intent.ACTION_VIEW);
// i.putExtra("address",phoneNo);
// i.putExtra("sms_body",message);
// i.putExtra(Intent.EXTRA_STREAM,uri );
// i.setType("vnd.android-dir/mms-sms");
// main.startActivity(i);
这是我尝试构建pdu并通过SmsManager.sendMultimediaMessage发送它的尝试。它没有发送。我找不到日志来告诉我为什么它没有发送。我猜想PDU的格式不正确。另外,我找不到HttpUtils.httpConnection的所有详细信息。
// final ConnectivityManager connMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
//final int result = connMgr.get( ConnectivityManager.TYPE_MOBILE, Phone.FEATURE_ENABLE_MMS);
// String subject = "hi there!";
// String recipient = phoneNo;
// final SendReq sendRequest = new SendReq();
//
// final EncodedStringValue[] sub = EncodedStringValue.extract(subject);
// if (sub != null && sub.length > 0) {
// sendRequest.setSubject(sub[0]);
// }
// final EncodedStringValue[] phoneNumbers = EncodedStringValue
// .extract(recipient);
// if (phoneNumbers != null && phoneNumbers.length > 0) {
// sendRequest.addTo(phoneNumbers[0]);
// }
// ArrayList<PduPart> parts = myMessage.getPduParts();
// final PduBody pduBody = new PduBody();
// for(PduPart part : parts){
//// final PduPart partPdu = new PduPart();
//// partPdu.setName("image".getBytes());
//// partPdu.setContentType("image/png".getBytes());
//// partPdu.setData(image);
// pduBody.addPart(part);
// }
// }
// sendRequest.setBody(pduBody);
// final PduComposer composer = new PduComposer(this.context, sendRequest);
// final byte[] bytesToSend = composer.make();
// smsManager.sendDataMessage(phoneNo,null,(short)2948,bytesToSend,null, null);
// smsManager.sendMultimediaMessage(context,uri,null,null,null);
//
// HttpUtils.httpConnection(context, 4444L, MMSCenterUrl,
// bytesToSendFromPDU, HttpUtils.HTTP_POST_METHOD, !TextUtils
// .isEmpty(MMSProxy), MMSProxy, port);
// Send a text based SMS
//smsManager.sendMultimediaMessage();
//SendReq request = new SendReq();
// smsManager.sendDataMessage(phoneNo, null, port, smsBody, null, null);
}
这就是我得到pdu零件的方式:
private ArrayList<PduPart> getPartOfMMS(String mmsID) {
String selectionPart = "mid=" + mmsID;
Uri uri = Uri.parse("content://mms/part");
Cursor cursor = context.getContentResolver().query(uri, null,
selectionPart, null, null);
ArrayList<PduPart> parts = new ArrayList<PduPart>();
try {
if (cursor.moveToFirst()) {
do {
PduPart tempPart= new PduPart();
tempPart.setData(cursor.getBlob(cursor.getColumnIndex(Telephony.Mms.Part._DATA)));
tempPart.setName(cursor.getString(cursor.getColumnIndex(Telephony.Mms.Part.NAME)).getBytes());
//tempPart.setContentId(cursor.getBlob(cursor.getColumnIndex(Telephony.Mms.Part.CONTENT_ID)));
tempPart.setContentType(cursor.getString(cursor.getColumnIndex(Telephony.Mms.Part.CONTENT_TYPE)).getBytes());
//tempPart.setContentDisposition(cursor.getBlob(cursor.getColumnIndex(Telephony.Mms.Part.CONTENT_DISPOSITION)));
//tempPart.setContentLocation(cursor.getBlob(cursor.getColumnIndex(Telephony.Mms.Part.CONTENT_LOCATION)));
//tempPart.setCharset(cursor.getInt(cursor.getColumnIndex(Telephony.Mms.Part.CHARSET)));
parts.add(tempPart);
} while (cursor.moveToNext());
}
return null;
} finally {
cursor.close();
}
}
这是用于获取部件的ID。这些工作正常,我可以将图像保存在图像部分之外:
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public MmsMessage getLastMmsId(){
int id = getLastMmsIds(1).get(0);
//byte[] part = getPartOfMMS(id);
MmsMessage message = getMmsText(id);
return message;
}
public List<Integer> getLastMmsIds(int count){
String selection = "";
String[] selectionArgs = new String[]{""};
// new String[]{"_id", "thread_id", "date","address", "body"}
String[] projection = new String[] {"_id", "date", "msg_box", "read", "sub", "thread_id"};
Cursor cursor = getContentResolver().query(Telephony.Mms.Inbox.CONTENT_URI,
projection, null, null, "date DESC LIMIT "+ count);
Long threadId = -1L;
if(cursor == null){
return null;
}
ArrayList<Integer> ids = new ArrayList<Integer>();
while (cursor.moveToNext())
{
int key = cursor.getInt (cursor.getColumnIndexOrThrow ("_id"));
threadId = cursor.getLong (cursor.getColumnIndexOrThrow ("thread_id"));
String read = cursor.getString (cursor.getColumnIndexOrThrow ("read")); // phone #
long date = cursor.getLong (cursor.getColumnIndexOrThrow ("date"));
String sub = cursor.getString (cursor.getColumnIndexOrThrow ("sub"));
String body = cursor.getString (cursor.getColumnIndexOrThrow ("msg_box"));
String q = String.format ("%04d %04d %10s %s %s",
key, threadId, read,
DateUtils.formatDateTime(context,date*1000,0),
body == null ? "" : body.substring (0,Math.min(10,body.length()-1)));
Log.d ("robert", q); // simple wrapper for Log.d()
ids.add(key);
}
cursor.close();
return ids;
}