Bug*_*der 5 android android-intent android-activity android-shortcut
我有一个带有上下文菜单的Non-Launcher活动.该菜单包含一个选项,可将活动添加到android主屏幕作为快捷方式.
我使用以下代码来创建快捷方式.
private void ShortcutIcon(){
Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Test");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
}
Run Code Online (Sandbox Code Playgroud)
正确设置必要的权限和意图过滤器.当我运行此代码时,快捷方式已成功创建.在快捷方式单击时,活动将按预期打开.
但是,我的活动显示了一些动态数据.为此,我需要将一个小字符串变量传递给活动.
我之前尝试使用此代码setAction
(就像您将额外数据传递给正常意图启动活动一样)
addIntent.putExtra("key_primarykey", value_i_want_to_pass);
Run Code Online (Sandbox Code Playgroud)
但是,当用户点击活动内部的快捷方式时,value_i_want_to_pass
会显示Null.
一些应用程序Whatsapp
允许完全相同.您可以保存聊天的快捷方式.还有一些dialer apps
允许添加联系人作为快捷方式,以便当您点击快捷方式时,会自动启动语音呼叫.
我想知道如何将一些数据从我的快捷方式传递给我的活动.
您正在发送的数据addIntent
将被启动器的广播接收器捕获。
只需更改以下行
addIntent.putExtra("key_primarykey", value_i_want_to_pass);
Run Code Online (Sandbox Code Playgroud)
到
shortcutIntent.putExtra("key_primarykey", value_i_want_to_pass);
Run Code Online (Sandbox Code Playgroud)
并在设置为 之前将其与您的代码shorcutIntent
一起编写。
这样您设置的快捷方式就会返回正确的值。所以修改后的代码如下。shortcutIntent
addIntent
Action Intent
private void ShortcutIcon(){
Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
shortcutIntent.putExtra("key_primarykey", value_i_want_to_pass);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Test");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
380 次 |
最近记录: |