您的当前位置:首页正文

【Android】Android彩信发送的两种方式+源代码

来源:独旅网
【Android】Android彩信发送的两种⽅式+源代码

第⼀种:直接调⽤彩信发送接⼝

Android 彩信发送的两种⽅式

  实现代码如下,

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\//彩信的主题

intent.putExtra(\"address\//彩信发送⽬的号码intent.putExtra(\"sms_body\//彩信中⽂字内容intent.putExtra(Intent.EXTRA_TEXT, \"it's EXTRA_TEXT\");intent.setType(\"image/*\");// 彩信附件类型

intent.setClassName(\"com.android.mms\startActivity(intent);

  看到彩信发送的代码,跟短信发送的代码有很⼤的不同,彩信发送不同于短信发送,调⽤系统的彩信发送会出现发送界⾯。  有朋友就要问了,这样不适合我的需求,我需要实现⾃定义彩信发送,并且不调⽤系统彩信。第⼆种⽅法将满⾜我们的需求

第⼆种:⾃定义彩信发送

  ⾃定义彩信发送,⽆需进⼊彩信发送界⾯,需要调⽤系统源码 PDU 实现。  ⾸先给出发送代码  

//彩信发送函数

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 list = APNManager.getSimMNC(context); new Thread() { @Override

public void run() { try {

byte[] res = MMSSender.sendMMS(context, list, mmsInfo.getMMSBytes()); } catch (Exception e) {

// TODO Auto-generated catch block e.printStackTrace(); } }; }.start();

}

APNManager.getSimMNC ⽤来设置 彩信Url和代理端⼝MMSSender.sendMMS 实现彩信的发送APNManager类源代码View Code

package 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 android.net.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 = \"http://mmsc.monternet.com\"; 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\");// 当前的APN private static String[] projection = { \"_id\ \"proxy\

private static String APN_NET_ID = null; private static String APN_WAP_ID = null;

public static List getSimMNC(Context context) {

TelephonyManager telManager = (TelephonyManager) context

.getSystemService(Context.TELEPHONY_SERVICE); String imsi = telManager.getSubscriberId(); if (imsi != null) {

ArrayList list = new ArrayList(); 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,则切换⾄wap if (!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; }

// 切换成NETAPN

public static boolean ChangeNetApn(final Context context) { final String wapId = getWapApnId(context); String apnId = getCurApnId(context); // 若当前apn是wap,则切换⾄net if (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\ return true; }

return true; }

// 切换成WAPAPN

public static boolean ChangeWapApn(final Context context) { final String netId = getWapApnId(context); String apnId = getCurApnId(context); // 若当前apn是net,则切换⾄wap if (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\ return true; }

return true; }

// 获取当前APN

public 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\ 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\

resolver.update(PREFERRED_APN_URI, values, null, null); Log.d(\"xml\ }

// 获取WAP APN

public static String getWapApnId(Context context) {

ContentResolver contentResolver = context.getContentResolver(); // 查询cmwapAPN

Cursor 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\ 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; }

// 获取所有APN

public static ArrayList getAPNList(final Context context) { ContentResolver contentResolver = context.getContentResolver(); Cursor cr = contentResolver.query(APN_TABLE_URI, projection, null, null, null);

ArrayList apnList = new ArrayList();

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

public static ArrayList getAvailableAPNList(final Context context) { // current不为空表⽰可以使⽤的APN

ContentResolver contentResolver = context.getContentResolver(); Cursor cr = contentResolver.query(APN_TABLE_URI, projection, \"current is not null\null, null);

ArrayList apnList = new ArrayList(); 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;

@Override

public String toString() {

return \"id=\" + id + \ } }}

MMSSender类源代码View Code

//发送类

package com.rayray.util;import java.io.DataInputStream;import java.io.IOException;import java.net.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 = \"http://mmsc.monternet.com\"; // 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 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 START

ByteArrayEntity 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 END

HttpParams params = client.getParams();

HttpProtocolParams.setContentCharset(params, \"UTF-8\"); System.out.println(\"准备执⾏发送\");

// PlainSocketFactory localPlainSocketFactory = // PlainSocketFactory.getSocketFactory(); HttpResponse response = client.execute(post); System.out.println(\"执⾏发送结束, 等回执。。\"); StatusLine status = response.getStatusLine(); Log.d(TAG, \"status \" + status.getStatusCode());

if (status.getStatusCode() != 200) { // HTTP 200 表服务器成功返回⽹页 Log.d(TAG, \"!200\");

throw new IOException(\"HTTP error: \" + status.getReasonPhrase()); }

HttpEntity resentity = response.getEntity(); byte[] body = null; if (resentity != null) {

try {

if (resentity.getContentLength() > 0) {

body = new byte[(int) resentity.getContentLength()];

DataInputStream dis = new DataInputStream( resentity.getContent()); try {

dis.readFully(body); } finally { try {

dis.close();

} catch (IOException e) {

Log.e(TAG,

\"Error closing input stream: \" + e.getMessage()); } } }

} finally {

if (entity != null) {

entity.consumeContent(); } } }

Log.d(TAG, \"result:\" + new String(body));

System.out.println(\"成功!!\" + new String(body)); return body;

} catch (IllegalStateException e) { Log.e(TAG, \"\

// handleHttpConnectionException(e, mmscUrl); } catch (IllegalArgumentException e) { Log.e(TAG, \"\

// handleHttpConnectionException(e, mmscUrl); } catch (SocketException e) {

Log.e(TAG, \"\

// handleHttpConnectionException(e, mmscUrl); } catch (Exception e) {

Log.e(TAG, \"\

// handleHttpConnectionException(e, mmscUrl); } finally {

if (client != null) { // client.;

}

APNManager.shouldChangeApnBack(context); }

return new byte[0]; }}

注意,这⾥需要从系统源码中引⼊ com.google.android.mms包,并将相关引⽤包调通,才可以使⽤。

//////////////////////////////////////////////

需要获取源代码的朋友,可以通过下⾯两种⽅式获取

(2)请在评论中填写邮件地址,会通过邮箱发送源码。

看到⼤家的反馈,源码调试有些问题,我抽时间会再更新代码,请谅解,同时感谢⼤家的帮助和⽀持

原创声明 转载请注明本⽂出⾃

感谢⼤家的推荐和收藏你的⽀持! 我们的动⼒!

因篇幅问题不能全部显示,请点此查看更多更全内容