想要开发彩信功能的收发,就必须了解原生SMS彩信是如何发送,因此我们就要基于它的流程来开发彩信的发送。。。。
对看过彩信发送流程的同学,想必就不过多介绍,此篇只简单介绍一下,原生的彩信发送是在WorkingMessage.java 中刷新收信人。生成彩信的可发送的Pdu—SendReq
然后存进数据库中,然后会构建MmsMessageSender,传入收信人和彩信的Uri,,让其发送。后面在启动TransactionService发送,,中间一堆流程就不说了,因此我们
想要自己发送彩信,就要知道它是如何生成彩信可发送的 Pdu,这个是最重要的。贴出简单的demo ,如何生成。
import com.google.android.mms.pdu.CharacterSets; import com.google.android.mms.pdu.EncodedStringValue; import com.google.android.mms.pdu.PduBody; import com.google.android.mms.pdu.PduComposer; import com.google.android.mms.pdu.PduPart; import com.google.android.mms.pdu.SendReq; import com.google.android.mms.pdu.PduPersister; private void SendReq(){ String filename = MmsUtils.getRealFilePath(this,uri); Uri mmsdraft = Uri.parse("content://mms/drafts"); Uri res = null; int size = MmsUtils.getsize(uri,this); final SendReq sendRequest = new SendReq(); final PduBody pduBody = new PduBody(); final PduPart part = new PduPart(); part.setName(filename.getBytes()); part.setContentType((MmsUtils.getFileContentType(filename)).getBytes()); final PduPart partPdu = new PduPart(); partPdu.setCharset(CharacterSets.UTF_8);//UTF_16 partPdu.setName(part.getName()); partPdu.setContentType(part.getContentType()); partPdu.setDataUri(uri); String body = informationEditText.getText().toString().trim(); Log.d("pyh","pduBody == "+body); if (!TextUtils.isEmpty(body)) { final PduPart txtpartPdu = new PduPart(); txtpartPdu.setCharset(CharacterSets.UTF_8); txtpartPdu.setContentType("text/plain".getBytes()); txtpartPdu.setName(("text.txt".getBytes())); txtpartPdu.setData(body.getBytes()); pduBody.addPart(txtpartPdu); Log.d("pyh","pduBody == addPart"); } pduBody.addPart(partPdu); Log.d("pyh","uriName == "+MmsUtils.getFileName(filename)); Log.d("pyh","uriContentType == "+MmsUtils.getFileContentType(filename)); Log.d("pyh","urigetSize == "+MmsUtils.getsize(uri,this)); Log.d("pyh","addresseeText == "+addresseeText.getText().toString()); EncodedStringValue[] rec = EncodedStringValue.extract(addresseeText.getText().toString()); if (rec != null && rec.length > 0) { sendRequest.addTo(rec[0]); } sendRequest.setDate(System.currentTimeMillis() / 1000L); sendRequest.setBody(pduBody); try { PduPersister mPersister = PduPersister.getPduPersister(this); res = mPersister.persist(sendRequest, mmsdraft, true, false, null); Log.d("pyh","PduPersister res == "+res); } catch (Exception e) { Log.d("pyh","Exception == "+e); } Log.d("pyh","res == "+res); try{ Intent intent = new Intent("DY_MMS_SEND_ACTION"); intent.putExtra(PhoneConstants.SUBscriptION_KEY, mMessageSubId); intent.putExtra(BUNDLE_URI, res.toString()); PendingIntent sentIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATe_CURRENT); SmsManager.getSmsManagerForSubscriptionId(mMessageSubId).sendStoredMultimediaMessage(res, MmsUtils.getMmsServiceConfig(), sentIntent); }catch (Exception e) { Log.d("pyh","Exception == "+e); } }
其中Google 包是一定要导入的,方法头几句就是对彩信内容的组装,其中sendRequest.addTo(rec[0]); 就是你要发送的号码,当然你也可以发送多个号码,res = mPersister.persist(sendRequest, mmsdraft, true, false, null); 这个方法就是将你组装好的pdu存进数据库中。 好了这时简单的彩信pdu就已经生成好了,,那我们如何发送呢?
我主要是在MTK的源码中找方法,其中sendStoredMultimediaMessage 就是将其发送,这个方法会调用package/services/Mms 服务包中的方法发送,也就是我们所了解的启动Http 发送,(源码中Transaction.sendPdu()会先设置好网路,然后直接调用HttpUtils 中的httpConnection()方法,用HTTP 把彩信发送出去),但有个这个方法,我们就不必去自己写,是不是很开心。。发送时的PendingIntent 可以得到返回的信息。
public class MMSReceivers extends BroadcastReceiver { public static final String BUNDLE_URI = "bundle_uri"; public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Log.d("pyh","Receivers_MMS_URI = "+(intent.getStringExtra(BUNDLE_URI)); Uri mmsUri = Uri.parse(intent.getStringExtra(NewSmsActivity.BUNDLE_URI)); int result = intent.getIntExtra("result", SmsManager.MMS_ERROR_UNSPECIFIED); switch (getResultCode()) { case Activity.RESULT_OK: Toast.makeText(context, R.string.sms_send_sucess, Toast.LENGTH_SHORT).show(); break; default: Toast.makeText(context, R.string.sms_send_fail, Toast.LENGTH_SHORT).show(); break; } } }
mmsUri就是你所发送短信的数据库中的Uri了。对了还有就是你所发送的彩信附件的格式要有所注意,贴出简单的demo
public static final String IMAGE_BMP = "image/x-ms-bmp"; public static final String IMAGE_XBMP = "image/bmp"; public static final String IMAGE_JPG = "image/jpg"; public static final String IMAGE_WBMP = "image/vnd.wap.wbmp"; public static final String IMAGE_GIF = "image/gif"; public static final String IMAGE_PNG = "image/png"; public static final String IMAGE_JPEG = "image/jpeg"; public static final String TEXT_VCALENDAR = "text/x-vCalendar"; public static final String TEXT_VCARD = "text/x-vCard"; public static final String AUDIO_IMELODY = "audio/imelody"; public static final String AUDIO_OGG = "application/ogg"; public static final String AUDIO_AAC = "audio/aac"; public static final String AUDIO_MPEG = "audio/mpeg"; public static final String AUDIO_3GPP = "audio/3gpp"; public static final String AUDIO_AMR = "audio/amr"; public static final String AUDIO_MP3 = "audio/mp3"; public static final String VIDEO_MP4 = "video/mp4"; public static final String VIDEO_3GPP = "video/3gpp"; public static final String CONFIG_USER_AGENT = "userAgent"; public static final String CONFIG_UA_PROF_URL = "uaProfUrl"; public String getFileContentType(String fileName){ String finalContentType = ""; if(fileName != null){ String suffix = fileName.contains(".") ? fileName.substring(fileName.lastIndexOf("."), fileName.length()) : ""; if (suffix.equals("")) { return null; } else if (suffix.equalsIgnoreCase(".bmp")) { finalContentType = IMAGE_BMP; } else if (suffix.equalsIgnoreCase(".jpg")) { finalContentType = IMAGE_JPG; } else if (suffix.equalsIgnoreCase(".wbmp")) { finalContentType = IMAGE_WBMP; } else if (suffix.equalsIgnoreCase(".gif")) { finalContentType = IMAGE_GIF; } else if (suffix.equalsIgnoreCase(".png")) { finalContentType = IMAGE_PNG; } else if (suffix.equalsIgnoreCase(".jpeg")) { finalContentType = IMAGE_JPEG; } else if (suffix.equalsIgnoreCase(".vcs")) { finalContentType = TEXT_VCALENDAR; } else if (suffix.equalsIgnoreCase(".amr")) { finalContentType = AUDIO_AMR; } else if (suffix.equalsIgnoreCase(".vcf")) { finalContentType = TEXT_VCARD; } else if (suffix.equalsIgnoreCase(".imy")) { finalContentType = AUDIO_IMELODY; // M: fix bug ALPS00355917 } else if (suffix.equalsIgnoreCase(".ogg")) { finalContentType = AUDIO_OGG; } else if (suffix.equalsIgnoreCase(".mp3")) { finalContentType = AUDIO_MP3; } else if (suffix.equalsIgnoreCase(".mp4")) { finalContentType = VIDEO_MP4; } else if (suffix.equalsIgnoreCase(".3gpp")) { finalContentType = VIDEO_3GPP; } else if (suffix.equalsIgnoreCase(".aac")) { finalContentType = AUDIO_AAC; } else if (suffix.equalsIgnoreCase(".mp2")) { finalContentType = AUDIO_MPEG; /// M: fix bug ALPS00444328, 3gp audio contentType will be modified /// when CMCC send to CU } else if (suffix.equalsIgnoreCase(".3gp")) { finalContentType = AUDIO_3GPP; } else { String extension = fileName.contains(".") ? fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()) : ""; finalContentType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); if (finalContentType == null) { return null; } } return finalContentType; } return fileName; }
这样简单的一条彩信就发送出去了,发送出去后,我们就也可以进行查询。sendStoredMultimediaMessage 成功后会将你的彩信存到收件箱中,那简单的数据库查询,大家可能都知道了。在此就不多说了。 (PS:此篇为原创,时间匆忙。可能有很多错误,欢迎大家指正修改。谢谢!)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)