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

Android静态快捷方式如何向LoginActivity传递参数?

Android静态快捷方式传递参数实现方案

你只需要在静态快捷方式的<intent>标签内部添加<extra>子标签,就能实现类似intent.putExtra("owner","ds")的传参效果,具体操作分两步:

1. 修改快捷方式XML配置文件

在你的<intent>标签里直接嵌套<extra>标签,指定参数的键和值即可。修改后的完整XML代码如下:

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
  <shortcut android:shortcutId="owner" android:enabled="true" android:icon="@drawable/ic_person_black_24dp" android:shortcutShortLabel="@string/owner" android:shortcutLongLabel="@string/owner" android:shortcutDisabledMessage="@string/owner">
    <intent android:action="android.intent.action.VIEW" android:targetPackage="com.ap.ap" android:targetClass="com.ap.ap.LoginActivity">
        <!-- 添加传参的extra标签 -->
        <extra android:name="owner" android:value="ds" />
    </intent>
    <categories android:name="android.shortcut.conversation" />
  </shortcut>
</shortcuts>

2. 在LoginActivity中获取参数

启动LoginActivity后,直接通过Intent对象获取传递过来的参数就行,示例代码如下:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    // 获取快捷方式传递的参数
    String ownerValue = getIntent().getStringExtra("owner");
    // 这里就可以使用ownerValue变量了,比如打印或者做其他业务逻辑
    if (ownerValue != null) {
        Log.d("LoginActivity", "收到快捷方式参数:" + ownerValue);
    }
}

这样修改后,点击静态快捷方式跳转LoginActivity时,就能拿到指定的参数了,是不是很简单?

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:52:57