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
相关产品推荐
相关产品推荐

