[英]Sending and Receiving SMS and MMS in Android (pre Kit Kat Android 4.4)
[英]Sending mms in android 4.4
我正在嘗試僅從我的應用程序發送彩信。 我在 android 開發人員教程( http://android-developers.blogspot.com/2013/10/getting-your-sms-apps-ready-for-kitkat.html )的幫助下將其設為默認消息應用程序。
我的清單:
偵聽傳入 SMS 消息的 BroadcastReceiver:
<receiver android:name="com.test.SmsReceiver"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_DELIVER" />
</intent-filter>
</receiver>
偵聽傳入 MMS 消息的 BroadcastReceiver
<receiver android:name="com.test.MmsReceiver"
android:permission="android.permission.BROADCAST_WAP_PUSH">
<intent-filter>
<action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" />
<data android:mimeType="application/vnd.wap.mms-message" />
</intent-filter>
</receiver>
從電話快速響應傳遞消息的服務
<service android:name="com.test.HeadlessSmsSendService"
android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.RESPOND_VIA_MESSAGE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="sms" />
<data android:scheme="smsto" />
<data android:scheme="mms" />
<data android:scheme="mmsto" />
</intent-filter>
</service>
NewMmsActivity允許用戶發送新短信/彩信的活動:
<activity android:name="com.test.NewMmsActivity"
android:configChanges="keyboard|keyboardHidden|locale|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:label="@string/app_name"
android:launchMode="singleTop"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.SENDTO" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="sms" />
<data android:scheme="smsto" />
<data android:scheme="mms" />
<data android:scheme="mmsto" />
</intent-filter>
</activity>
但是,當我嘗試在NewMmsActivity中發送彩信時,它不起作用,而是像這樣打開對話框:
代碼:
Intent mmsIntent = new Intent(Intent.ACTION_SEND);
mmsIntent.putExtra("sms_body", "text");
mmsIntent.putExtra("address", "99999999");
mmsIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(fileString)));
mmsIntent.setType("image/jpeg");
startActivity(mmsIntent);
如果我使用 Intent.ACTION_SENDTO 什么也不會發生。 Intent 啟動時沒有任何問題,但什么也沒有發生。
我錯過了什么?
作為默認應用程序,您負責發送彩信本身,而不是打開另一個應用程序來發送,這就是您的代碼所做的。 目前,Android 沒有用於 MMS 的簡單 API,因為它有用於 SMS 的 API。 此外,它是框架的一個非常糟糕的文檔方面,實現它所需的代碼量和解釋超出了 Stack Overflow 的范圍。 歡迎您檢查本機應用程序的源代碼以獲取指導,但請記住,這不是一項簡單的任務,因為默認應用程序負責處理 MMS 所需的一切,包括發送、接收和內容提供程序事務。
我認為問題在於您試圖同時發送圖像和文本數據,但您的類型設置為圖像。 嘗試將其改為:
mmsIntent.setType("*/*");
我發現發送彩信的最簡單方法是在此處找到的 android-smsmms 庫: https : //github.com/klinker41/android-smsmms
對於獲取 mmsc、代理和端口,我使用了:
final Cursor apnCursor = SqliteWrapper.query(mContext, this.mContext.getContentResolver(),
Uri.withAppendedPath(Telephony.Carriers.CONTENT_URI, "current"), APN_PROJECTION, null, null, null);
String type = null;
if (apnCursor.moveToFirst()) {
do {
type = apnCursor.getString(3);
if(type.equals("default,supl,mms") ||
type.equals("mms")) {
mmsc = apnCursor.getString(0);
proxy = apnCursor.getString(1);
port = apnCursor.getString(2);
}while (apnCursor.moveToNext());
在 if 循環中,我正在檢查 APN 是否有 MMS 數據,否則我需要轉到下一個。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.