在安卓系统中从我的应用程序发送彩信

2022-09-02 22:43:59

我想将我的应用程序中的彩信发送到特定号码。我已经搜索并找到了这个代码,但我不知道这个代码是否是我需要的。我的问题是:

- 任何人都可以向我解释这个代码.我是MMS的初学者。

- 另外,我认为此代码是让用户从我的应用程序发送彩信而不将其移动到本机消息传递收件箱(这就是我想要的)我是对的吗?

-我也有一个问题,我不知道我怎么能把这个代码放在我的项目中。

这就是我发现的

MMS只是一个http-post请求。您应该使用额外的网络功能执行请求:

final ConnectivityManager connMgr =  (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
final int result = connMgr.startUsingNetworkFeature( ConnectivityManager.TYPE_MOBILE, Phone.FEATURE_ENABLE_MMS);

如果返回带有值的结果,则必须等待正确的状态。注册并等待,直到出现:Phone.APN_REQUEST_STARTEDBroadCastReciverPhone.APN_ALREADY_ACTIVE

final IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
context.registerReceiver(reciver, filter);

如果后台连接已准备就绪,则构建内容并执行请求。如果你想使用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:来自MMS-APNs的代理,
  • 端口:来自彩信-APN 的端口

请注意,某些类来自内部包。需要从安卓git下载。请求应使用用户 apn 空间代码中的 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;
      }

请帮帮我


答案 1

你为什么不使用安卓系统功能:

请看一下

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); }
}

干杯

汤姆


答案 2

我在另一个线程中发现了一个指向github项目的链接,该项目可以100%https://github.com/klinker41/android-smsmms

请注意,强制设置仅

Settings sendSettings = new Settings();

sendSettings.setMmsc(mmsc);
sendSettings.setProxy(proxy);
sendSettings.setPort(port);

你可以得到这样的东西(在Android上以编程方式设置APN - vincent091的answear上找到):

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));

推荐