簡體   English   中英

Android4.4 無法使用“vnd.android-dir/mms-sms”處理短信意圖

[英]Android4.4 can not handle sms intent with “vnd.android-dir/mms-sms”

我的應用程序有一個按鈕來啟動默認的短信活動,除了新的 Android 4.4(kitkat) 之外,它在所有 android 版本上都運行良好,這是代碼:

public void onClick(View arg0) {
    Intent smsIntent = new Intent(Intent.ACTION_VIEW);
    smsIntent.setType("vnd.android-dir/mms-sms");
    smsIntent.putExtra("address", member.getPhoneNumber().trim());
    context.startActivity(smsIntent);
}

我收到錯誤消息

11-08 02:08:32.815: E/AndroidRuntime(14733): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW typ=vnd.android-dir/mms-sms (has extras) }

我知道谷歌對默認短信應用程序處理短信意圖的方式進行了一些更改。 但我的應用程序不是短信應用程序,但它僅具有使用收件人號碼啟動默認短信應用程序的功能。 所以請幫忙。

要使用填充的號碼啟動 SMS 應用程序,請使用操作ACTION_SENDTO

Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("smsto:" + Uri.encode(phoneNumber)));
startActivity(intent);

這將適用於 Android 4.4。 它也應該適用於早期版本的 Android,但是由於 API 從未公開,因此行為可能會有所不同。 如果您之前的方法沒有問題,我可能會堅持使用 4.4 之前的版本並使用ACTION_SENDTO用於 4.4+。

要在不插入號碼的情況下啟動短信應用程序,您應該刪除 setType

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("sms:"));
intent.putExtra("sms_body", "smsMsgVar");
startActivity(intent);

當您使用模擬器進行測試時發生了問題,

請使用實際設備進行測試。

在 Kotlin 中,此代碼有效:

val defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(activity)  
            val sendIntent = Intent(Intent.ACTION_SEND)
            sendIntent.type = "text/plain"
            sendIntent.putExtra("address", "sms:"+contactNumber)
            sendIntent.putExtra(Intent.EXTRA_TEXT, getString(R.string.share_msg_body))
            Timber.e("defaultSmsPackageName: "+defaultSmsPackageName)
            if (defaultSmsPackageName != null){ //Can be null in case that there is no default, then the user would be able to choose any app that support this intent.
                sendIntent.setPackage(defaultSmsPackageName)
                activity!!.startActivity(sendIntent)
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM