根據這篇文章,我正在打開一個電子郵件客戶端以使用以下代碼發送多個附件:
Intent emailSelectorIntent = new Intent(Intent.ACTION_SENDTO);
emailSelectorIntent.setData(Uri.parse("mailto:"));
Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.putExtra(Intent.EXTRA_EMAIL, addresses);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
ArrayList<Uri> uriList = new ArrayList<>();
uriList.add(FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID ".provider", fileA));
uriList.add(FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID ".provider", fileB));
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(Intent.EXTRA_STREAM, uriList);
intent.setSelector(emailSelectorIntent);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
請注意,正如上面鏈接的帖子中所教導的,我需要兩個Intents才能使其用于發送附件(并將選擇器限制為僅電子郵件客戶端)......emailSelectorIntent并且intent,前者通過intent.setSelector(emailSelectorIntent).
為了使選擇器打開,我的清單中還需要以下內容:
<intent-filter>
<action android:name="android.intent.action.SENDTO" />
<data android:scheme="mailto" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
But when the selector opens, not only do I get email clients showing (eg gmail) I am also getting my own app in the list... which is not capable of sending emails.
這篇文章建議不要放入android.intent.action.SENDTO清單,但如果我洗掉它,那么選擇器將不再打開。如果放回去,選擇器會打開,但會使用我自己的應用程式作為電子郵件客戶端的選項(它不是)。
那么在這種情況下如何避免我自己的應用出現在選擇器串列中呢?
請注意,我targetSdk的32情況可能是相關的(請參閱此處@CommonsWare 的評論)。
uj5u.com熱心網友回復:
我有點迷惑,擁有它<intent-filter>會改變任何事情。無論如何,您真的不希望這樣,因為它可能導致其他應用程式將ACTION_SENDTO請求路由到您的應用程式,這是您不想要的。
沒有
android.intent.action.SENDTO在清單中,startActivity()因為測驗intent.resolveActivity(getPackageManager()) != null失敗而永遠不會被呼叫
這是由于包可見性規則。您的選擇是添加<queries>說您需要查看誰都支持您的,或者用/Intent替換resolveActivity()呼叫:trycatch
try {
startActivity(intent);
} catch (Exception e) {
// whatever you want to do to gracefully degrade in case there is no suitable activity to start
}
不確定這將如何在舊版本的 Android 上運行?
<queries>在舊版本的 Android 上將被忽略,但在舊版本的 Android 上包可見性不是問題。就個人而言,我更喜歡try/ catch,因為它可以處理更多場景。
uj5u.com熱心網友回復:
@CommonsWare的回答(一如既往)非常務實且內容豐富。我想我也更喜歡try/catch那里提出的解決方案,并將采用它。但是離開resolveActivity()支票并<queries>在清單的頂層添加專案的替代方法也可以,如下所示:
<queries>
<intent>
<action android:name="android.intent.action.SENDTO" />
<data android:scheme="mailto" />
<category android:name="android.intent.category.DEFAULT" />
</intent>
</queries>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/477368.html
標籤:安卓
