【Android 】Android 彩信发送的两种⽅式+源代码Android 彩信发送的两种⽅式第⼀种:直接调⽤彩信发送接⼝ 实现代码如下, 看到彩信发送的代码,跟短信发送的代码有很⼤的不同,彩信发送不同于短信发送,调⽤系统的彩信发送会出现发送界⾯。
有朋友就要问了,这样不适合我的需求,我需要实现⾃定义彩信发送,并且不调⽤系统彩信。
第⼆种⽅法将满⾜我们的需求第⼆种:⾃定义彩信发送 ⾃定义彩信发送,⽆需进⼊彩信发送界⾯,需要调⽤系统源码 PDU 实现。
⾸先给出发送代码APNManager.getSimMNC ⽤来设置 彩信Url 和代理端⼝MMSSender.sendMMS 实现彩信的发送APNManager 类源代码Intent intent = new Intent(Intent.ACTION_SEND);intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(url));// uri 为你的附件的uriintent.putExtra("subject", "it's subject"); //彩信的主题intent.putExtra("address", "10086"); //彩信发送⽬的号码intent.putExtra("sms_body", "it's content"); //彩信中⽂字内容intent.putExtra(Intent.EXTRA_TEXT, "it's EXTRA_TEXT");intent.setType("image/*");// 彩信附件类型intent.setClassName("com.android.mms","poseMessageActivity");startActivity(intent);//彩信发送函数public static void sendMMS(final Context context, String number,String subject, String text, String imagePath, String audioPath) {final MMSInfo mmsInfo = new MMSInfo(context, number, subject, text,imagePath, audioPath);final List<String> list = APNManager.getSimMNC (context);new Thread() {@Overridepublic void run() {try {byte [] res = MMSSender.sendMMS (context, list,mmsInfo.getMMSBytes());} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}};}.start();}View Codepackage com.rayray.util;import java.util.ArrayList;import java.util.List;import android.content.ContentResolver;import android.content.ContentValues;import android.content.Context;import android.database.Cursor;import .Uri;import android.telephony.TelephonyManager;import android.text.TextUtils;import android.util.Log;public class APNManager {// 电信彩信中⼼url ,代理,端⼝public static String mmscUrl_ct = "http://mmsc.vnet.mobi";public static String mmsProxy_ct = "10.0.0.200";// 移动彩信中⼼url ,代理,端⼝public static String mmscUrl_cm = "";public static String mmsProxy_cm = "10.0.0.172";// 联通彩信中⼼url ,代理,端⼝public static String mmscUrl_uni = "http://mmsc.vnet.mobi";public static String mmsProxy_uni = "10.0.0.172";private static String TAG = "APNManager";private static final Uri APN_TABLE_URI = Uri.parse("content://telephony/carriers");// 所有的APN 配配置信息位置private static final Uri PREFERRED_APN_URI = Uri.parse("content://telephony/carriers/preferapn");// 当前的APNprivate static String[] projection = { "_id", "apn", "type", "current","proxy", "port" };private static String APN_NET_ID = null ;private static String APN_WAP_ID = null ;public static List<String> getSimMNC(Context context) {TelephonyManager telManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);String imsi = telManager.getSubscriberId();if (imsi != null) {ArrayList<String> list = new ArrayList<String>();if (imsi.startsWith("46000") || imsi.startsWith("46002")) {// 因为移动⽹络编号46000下的IMSI已经⽤完,所以虚拟了⼀个46002编号,134/159号段使⽤了此编号 // 中国移动list.add(mmscUrl_cm);list.add(mmsProxy_cm);} else if (imsi.startsWith("46001")) {// 中国联通list.add(mmscUrl_uni);list.add(mmsProxy_uni);} else if (imsi.startsWith("46003")) {// 中国电信list.add(mmscUrl_ct);list.add(mmsProxy_ct);}shouldChangeApn(context);return list;}return null;}private static boolean shouldChangeApn(final Context context) {final String wapId = getWapApnId(context);String apnId = getCurApnId(context);// 若当前apn不是wap,则切换⾄wapif (!wapId.equals(apnId)) {APN_NET_ID = apnId;setApn(context, wapId);// 切换apn需要⼀定时间,先让等待2秒try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}return true;}return false;}public static boolean shouldChangeApnBack(final Context context) {// 彩信发送完毕后检查是否需要把接⼊点切换回来if (null != APN_NET_ID) {setApn(context, APN_NET_ID);return true;}return false;}// 切换成NETAPNpublic static boolean ChangeNetApn(final Context context) {final String wapId = getWapApnId(context);String apnId = getCurApnId(context);// 若当前apn是wap,则切换⾄netif (wapId.equals(apnId)) {APN_NET_ID = getNetApnId(context);setApn(context, APN_NET_ID);// 切换apn需要⼀定时间,先让等待⼏秒,与机⼦性能有关try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}Log.d("xml", "setApn");return true;}return true;}// 切换成WAPAPNpublic static boolean ChangeWapApn(final Context context) {final String netId = getWapApnId(context);String apnId = getCurApnId(context);// 若当前apn是net,则切换⾄wapif (netId.equals(apnId)) {APN_WAP_ID = getNetApnId(context);setApn(context, APN_WAP_ID);// 切换apn需要⼀定时间,先让等待⼏秒,与机⼦性能有关try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}Log.d("xml", "setApn");return true;}return true;}// 获取当前APNpublic static String getCurApnId(Context context) {ContentResolver resoler = context.getContentResolver();// String[] projection = new String[] { "_id" };Cursor cur = resoler.query(PREFERRED_APN_URI, projection, null, null, null);String apnId = null;if (cur != null && cur.moveToFirst()) {apnId = cur.getString(cur.getColumnIndex("_id"));}Log.i("xml", "getCurApnId:" + apnId);return apnId;}public static APN getCurApnInfo(final Context context) {ContentResolver resoler = context.getContentResolver();// String[] projection = new String[] { "_id" };Cursor cur = resoler.query(PREFERRED_APN_URI, projection, null, null, null);APN apn = new APN();if (cur != null && cur.moveToFirst()) {apn.id = cur.getString(cur.getColumnIndex("_id"));apn.apn = cur.getString(cur.getColumnIndex("apn"));apn.type = cur.getString(cur.getColumnIndex("type"));}return apn;}public static void setApn(Context context, String id) {ContentResolver resolver = context.getContentResolver();ContentValues values = new ContentValues();values.put("apn_id", id);resolver.update(PREFERRED_APN_URI, values, null, null);Log.d("xml", "setApn");}// 获取WAP APNpublic static String getWapApnId(Context context) {ContentResolver contentResolver = context.getContentResolver();// 查询cmwapAPNCursor cur = contentResolver.query(APN_TABLE_URI, projection,"apn = \'cmwap\' and current = 1", null, null);// wap APN 端⼝不为空if (cur != null && cur.moveToFirst()) {do {String id = cur.getString(cur.getColumnIndex("_id"));String proxy = cur.getString(cur.getColumnIndex("proxy"));if (!TextUtils.isEmpty(proxy)) {Log.i("xml", "getWapApnId" + id);return id;}} while (cur.moveToNext());}return null;}public static String getNetApnId(Context context) {ContentResolver contentResolver = context.getContentResolver();Cursor cur = contentResolver.query(APN_TABLE_URI, projection,"apn = \'cmnet\' and current = 1", null, null);if (cur != null && cur.moveToFirst()) {return cur.getString(cur.getColumnIndex("_id"));}return null;}// 获取所有APNpublic static ArrayList<APN> getAPNList(final Context context) {ContentResolver contentResolver = context.getContentResolver();Cursor cr = contentResolver.query(APN_TABLE_URI, projection, null, null, null);ArrayList<APN> apnList = new ArrayList<APN>();if (cr != null && cr.moveToFirst()) {do {Log.d(TAG,cr.getString(cr.getColumnIndex("_id")) + ";"+ cr.getString(cr.getColumnIndex("apn")) + ";"+ cr.getString(cr.getColumnIndex("type")) + ";"+ cr.getString(cr.getColumnIndex("current"))+ ";"+ cr.getString(cr.getColumnIndex("proxy")));APN apn = new APN();apn.id = cr.getString(cr.getColumnIndex("_id"));apn.apn = cr.getString(cr.getColumnIndex("apn"));apn.type = cr.getString(cr.getColumnIndex("type"));apnList.add(apn);} while (cr.moveToNext());cr.close();}return apnList;}// 获取可⽤的APNpublic static ArrayList<APN> getAvailableAPNList(final Context context) { // current不为空表⽰可以使⽤的APNContentResolver contentResolver = context.getContentResolver();Cursor cr = contentResolver.query(APN_TABLE_URI, projection,"current is not null", null, null);ArrayList<APN> apnList = new ArrayList<APN>();if (cr != null && cr.moveToFirst()) {do {Log.d(TAG,cr.getString(cr.getColumnIndex("_id")) + ";"+ cr.getString(cr.getColumnIndex("apn")) + ";"+ cr.getString(cr.getColumnIndex("type")) + ";"+ cr.getString(cr.getColumnIndex("current"))+ ";"+ cr.getString(cr.getColumnIndex("proxy")));APN apn = new APN();apn.id = cr.getString(cr.getColumnIndex("_id"));apn.apn = cr.getString(cr.getColumnIndex("apn"));apn.type = cr.getString(cr.getColumnIndex("type"));apnList.add(apn);} while (cr.moveToNext());cr.close();}return apnList;}// ⾃定义APN包装类static class APN {String id;String apn;String type;@Overridepublic String toString() {return "id=" + id + ",apn=" + apn + ";type=" + type;}}}MMSSender类源代码View Code//发送类package com.rayray.util;import java.io.DataInputStream;import java.io.IOException;import .SocketException;import java.util.List;import org.apache.http.HttpEntity;import org.apache.http.HttpHost;import org.apache.http.HttpResponse;import org.apache.http.StatusLine;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpPost;import org.apache.http.conn.params.ConnRoutePNames;import org.apache.http.entity.ByteArrayEntity;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.params.BasicHttpParams;import org.apache.http.params.HttpConnectionParams;import org.apache.http.params.HttpParams;import org.apache.http.params.HttpProtocolParams;import org.apache.http.protocol.HTTP;import android.content.Context;import android.util.Log;/*** @author* @version创建时间:2012-2-1 上午09:32:54*/public class MMSSender {private static final String TAG = "MMSSender";// public static String mmscUrl = "";// public static String mmscProxy = "10.0.0.172";public static int mmsProt = 80;private static String HDR_VALUE_ACCEPT_LANGUAGE = "";private static final String HDR_KEY_ACCEPT = "Accept";private static final String HDR_KEY_ACCEPT_LANGUAGE = "Accept-Language";private static final String HDR_VALUE_ACCEPT = "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic";public static byte[] sendMMS(Context context, List<String> list, byte[] pdu)throws IOException {System.out.println("进⼊sendMMS⽅法");// HDR_VALUE_ACCEPT_LANGUAGE = getHttpAcceptLanguage();HDR_VALUE_ACCEPT_LANGUAGE = HTTP.UTF_8;String mmsUrl = (String) list.get(0);String mmsProxy = (String) list.get(1);if (mmsUrl == null) {throw new IllegalArgumentException("URL must not be null.");}HttpClient client = null;try {// Make sure to use a proxy which supports CONNECT.// client = HttpConnector.buileClient(context);HttpHost httpHost = new HttpHost(mmsProxy, mmsProt);HttpParams httpParams = new BasicHttpParams();httpParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, httpHost);HttpConnectionParams.setConnectionTimeout(httpParams, 10000);client = new DefaultHttpClient(httpParams);HttpPost post = new HttpPost(mmsUrl);// mms PUD STARTByteArrayEntity entity = new ByteArrayEntity(pdu);entity.setContentType("application/vnd.wap.mms-message");post.setEntity(entity);post.addHeader(HDR_KEY_ACCEPT, HDR_VALUE_ACCEPT);post.addHeader(HDR_KEY_ACCEPT_LANGUAGE, HDR_VALUE_ACCEPT_LANGUAGE);post.addHeader("user-agent","Mozilla/5.0(Linux;U;Android 2.1-update1;zh-cn;ZTE-C_N600/ZTE-C_N600V1.0.0B02;240*320;CTC/2.0)AppleWebkit/530.17(KHTML,like Gecko) Version/4.0 Mobile Safari/530.17");// mms PUD ENDHttpParams params = client.getParams();HttpProtocolParams.setContentCharset(params, "UTF-8");System.out.println("准备执⾏发送");// PlainSocketFactory localPlainSocketFactory =// PlainSocketFactory.getSocketFactory();HttpResponse response = client.execute(post);System.out.println("执⾏发送结束,等回执。