gpt4 book ai didi

android - 快捷方式主屏幕,如何发送额外内容

转载 作者:太空狗 更新时间:2023-10-29 16:20:45 24 4
gpt4 key购买 nike

我在某处读到(但我再也找不到了),应该可以从设备主屏幕上的快捷方式发送额外内容。我成功地创建了一个快捷方式,但是 Bundle extras = getIntent().getExtras(); 给出了一个空指针。

我创建的快捷方式如下:

   Intent shortcutIntent = new Intent(this.getApplicationContext(),
Shortcut_Activity.class);

shortcutIntent.setAction(Intent.ACTION_MAIN);

Intent addIntent = new Intent();
addIntent
.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(this.getApplicationContext(),
R.drawable.ic_shortcut));
addIntent.putExtra("ID", id); //THIS IS THE EXTRA DATA I WANT TO ATTACH
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
this.getApplicationContext().sendBroadcast(addIntent);

这可能吗?如果是,怎么做?

最佳答案

是的,我已经实现了,这是代码

private void addShortcut() {
//Adding shortcut for MainActivity
//on Home screen
Intent shortcutIntent = new Intent(getApplicationContext(),
HOMESHORTCUT.class);
//Set Extra
shortcutIntent.putExtra("extra", "shortCutTest ");

shortcutIntent.setAction(Intent.ACTION_MAIN);

Intent addIntent = new Intent();
addIntent
.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "HelloWorldShortcut");
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);


}
//GET Extra in HOMESHORTCUT activity.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mTextExtra=new TextView(this);
Bundle bundel=getIntent().getExtras();
String getExString=getIntent().getExtras().getString("extra");
mTextExtra.setText(getExString);
setContentView(mTextExtra);
}

关于android - 快捷方式主屏幕,如何发送额外内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15615595/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com