You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Compose中如何通过Intent打开邮件应用收件箱(新标签页)

Android Compose 跳转邮件应用收件箱及多窗口打开方案

一、直接跳转邮件应用收件箱

不需要使用ACTION_SENDTO动作,通过ACTION_MAIN配合CATEGORY_APP_EMAIL可直接启动默认邮件应用,多数邮件应用启动后默认进入收件箱页面。Compose中实现代码如下:

import android.content.Intent
import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
import android.content.Intent.CATEGORY_APP_EMAIL
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext

@Composable
fun OpenMailInboxButton() {
    val context = LocalContext.current
    Button(onClick = {
        val mailIntent = Intent(Intent.ACTION_MAIN).apply {
            addCategory(CATEGORY_APP_EMAIL)
            flags = FLAG_ACTIVITY_NEW_TASK
        }
        context.startActivity(mailIntent)
    }) {
        Text("打开邮件收件箱")
    }
}

二、在新标签页(多窗口)打开邮件应用

若需在系统多窗口/新标签页模式下打开邮件应用,需为Intent添加FLAG_ACTIVITY_LAUNCH_ADJACENT标记,同时确保设备支持多窗口功能:

@Composable
fun OpenMailInNewTabButton() {
    val context = LocalContext.current
    Button(onClick = {
        val mailIntent = Intent(Intent.ACTION_MAIN).apply {
            addCategory(CATEGORY_APP_EMAIL)
            flags = FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT
        }
        // 先检查是否有应用能处理该Intent
        if (context.packageManager.resolveActivity(mailIntent, 0) != null) {
            context.startActivity(mailIntent)
        }
    }) {
        Text("在新标签页打开邮件应用")
    }
}

三、打开指定邮件类应用

如果需要指定打开某款邮件应用(如Gmail、Outlook),可通过包名直接获取其启动Intent,同时添加 fallback 逻辑应对应用未安装的情况:

示例:打开Gmail收件箱

@Composable
fun OpenGmailButton() {
    val context = LocalContext.current
    Button(onClick = {
        // 获取Gmail的启动Intent
        val gmailIntent = context.packageManager.getLaunchIntentForPackage("com.google.android.gm")?.apply {
            flags = FLAG_ACTIVITY_NEW_TASK
            // Gmail支持通过extra直接定位到收件箱
            putExtra("android.intent.extra.EMAIL", arrayOf(""))
        }
        
        if (gmailIntent != null) {
            context.startActivity(gmailIntent)
        } else {
            // 未安装Gmail时,启动默认邮件应用
            val fallbackIntent = Intent(Intent.ACTION_MAIN).apply {
                addCategory(CATEGORY_APP_EMAIL)
                flags = FLAG_ACTIVITY_NEW_TASK
            }
            context.startActivity(fallbackIntent)
        }
    }) {
        Text("打开Gmail收件箱")
    }
}

常用邮件应用包名:

  • Gmail: com.google.android.gm
  • Outlook: com.microsoft.office.outlook
  • 三星邮件: com.samsung.android.email.provider

内容的提问来源于stack exchange,提问作者EdoDo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 03:47:44