java 从我的应用程序在 android 中发送彩信
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9932145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Send MMS from My application in android
提问by Maha
I want to send MMS from my application to a specific number. I've searched and found this code but I have no idea if this code what I need or not. My Questions is :
我想从我的应用程序向特定号码发送彩信。我已经搜索并找到了此代码,但我不知道此代码是否需要我。我的问题是:
-can anyone explain this code to me.i am beginner in MMS.
- 谁能向我解释一下这段代码。我是彩信的初学者。
-also, i thought this code is let the user send MMS from my application without move it to the native Messaging inbox (and this is what i want) Am i right?
-此外,我认为此代码可让用户从我的应用程序发送 MMS,而无需将其移动到本机 Messaging 收件箱(这就是我想要的),对吗?
-also i have a problem ,i do not know how can i put this code in my project.
-我还有一个问题,我不知道如何将此代码放入我的项目中。
this is what i found
这是我发现的
MMS is just a http-post request. You should perform the request using extra network feature :
MMS 只是一个 http-post 请求。您应该使用额外的网络功能执行请求:
final ConnectivityManager connMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
final int result = connMgr.startUsingNetworkFeature( ConnectivityManager.TYPE_MOBILE, Phone.FEATURE_ENABLE_MMS);
If you get back the result with Phone.APN_REQUEST_STARTED
value, you have to wait for proper state. Register BroadCastReciver
and wait until Phone.APN_ALREADY_ACTIVE
appears:
如果返回带Phone.APN_REQUEST_STARTED
值的结果,则必须等待正确的状态。注册BroadCastReciver
并等待,直到Phone.APN_ALREADY_ACTIVE
出现:
final IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
context.registerReceiver(reciver, filter);
If background connection is ready, then build content and perform request. If you want to do that using android's internal code, please use this:
如果后台连接准备好,则构建内容并执行请求。如果你想使用 android 的内部代码来做到这一点,请使用这个:
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]);
}
final PduBody pduBody = new PduBody();
if (parts != null) {
for (MMSPart part : parts) {
final PduPart partPdu = new PduPart();
partPdu.setName(part.Name.getBytes());
partPdu.setContentType(part.MimeType.getBytes());
partPdu.setData(part.Data);
pduBody.addPart(partPdu);
}
}
sendRequest.setBody(pduBody);
final PduComposer composer = new PduComposer(this.context, sendRequest);
final byte[] bytesToSend = composer.make();
HttpUtils.httpConnection(context, 4444L, MMSCenterUrl, bytesToSendFromPDU, HttpUtils.HTTP_POST_METHOD, !TextUtils.isEmpty(MMSProxy), MMSProxy, port);
- MMSCenterUrl: url from MMS-APNs,
- MMSProxy: proxy from MMS-APNs,
- port: port from MMS-APNs
- MMSCenterUrl:来自 MMS-APN 的 URL,
- MMSProxy:来自 MMS-APN 的代理,
- 端口:来自 MMS-APN 的端口
Note that some classes are from internal packages. Download from android git is required. The request should be done with url from user's apn-space code:
请注意,某些类来自内部包。需要从 android git 下载。该请求应该使用来自用户的 apn-space 代码的 url 完成:
public class APNHelper {
public class APN {
public String MMSCenterUrl = "";
public String MMSPort = "";
public String MMSProxy = "";
}
public APNHelper(final Context context) {
this.context = context;
}
public List<APN> getMMSApns() {
final Cursor apnCursor = this.context.getContentResolver().query(Uri.withAppendedPath(Telephony.Carriers.CONTENT_URI, "current"), null, null, null, null);
if ( apnCursor == null ) {
return Collections.EMPTY_LIST;
} else {
final List<APN> results = new ArrayList<APN>();
while ( apnCursor.moveToNext() ) {
final String type = apnCursor.getString(apnCursor.getColumnIndex(Telephony.Carriers.TYPE));
if ( !TextUtils.isEmpty(type) && ( type.equalsIgnoreCase(Phone.APN_TYPE_ALL) || type.equalsIgnoreCase(Phone.APN_TYPE_MMS) ) ) {
final String mmsc = apnCursor.getString(apnCursor.getColumnIndex(Telephony.Carriers.MMSC));
final String mmsProxy = apnCursor.getString(apnCursor.getColumnIndex(Telephony.Carriers.MMSPROXY));
final String port = apnCursor.getString(apnCursor.getColumnIndex(Telephony.Carriers.MMSPORT));
final APN apn = new APN();
apn.MMSCenterUrl = mmsc;
apn.MMSProxy = mmsProxy;
apn.MMSPort = port;
results.add(apn);
}
}
apnCursor.close();
return results;
}
Please help me
请帮我
回答by TomTom
why don't you use the android system functions:
你为什么不使用android系统功能:
Please have a look on
请看看
https://developer.android.com/guide/components/intents-common.html
https://developer.android.com/guide/components/intents-common.html
public void composeMmsMessage(String message, Uri attachment) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setData(Uri.parse("smsto:")); // This ensures only SMS apps respond
intent.putExtra("sms_body", message);
intent.putExtra(Intent.EXTRA_STREAM, attachment);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent); }
}
Cheers
干杯
Tom
汤姆
回答by Fabian
I found a link in an other thread to a github project that works 100% https://github.com/klinker41/android-smsmms
我在另一个线程中找到了一个 github 项目的链接,该项目 100% 有效https://github.com/klinker41/android-smsmms
Notice, that obligatory settings are only
请注意,强制性设置仅适用于
Settings sendSettings = new Settings();
sendSettings.setMmsc(mmsc);
sendSettings.setProxy(proxy);
sendSettings.setPort(port);
you can get them something like (found at Set APN programmatically on Android- answear by vincent091):
你可以得到它们(在 Android 上以编程方式设置 APN 中找到- answear by vincent091):
Cursor cursor = null;
if (Utils.hasICS()){
cursor =SqliteWrapper.query(activity, activity.getContentResolver(),
Uri.withAppendedPath(Carriers.CONTENT_URI, "current"), APN_PROJECTION, null, null, null);
} else {
cursor = activity.getContentResolver().query(Uri.withAppendedPath(Telephony.Carriers.CONTENT_URI, "current"),
null, null, null, null);
}
cursor.moveToLast();
String type = cursor.getString(cursor.getColumnIndex(Telephony.Carriers.TYPE));
String mmsc = cursor.getString(cursor.getColumnIndex(Telephony.Carriers.MMSC));
String proxy = cursor.getString(cursor.getColumnIndex(Telephony.Carriers.MMSPROXY));
String port = cursor.getString(cursor.getColumnIndex(Telephony.Carriers.MMSPORT));