我的应用程序可以选择创建主屏幕快捷方式。
我这样创建快捷方式:
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,
new Intent(ShortcutCreateActivity.this, ShortcutCreateActivity.class).putExtra(EXTRA_PROFILE_ID, profileId).putExtra(EXTRA_PROFILE_ACTION, item));
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, profileTitle);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, bitmap);
这按预期工作...当用户单击快捷方式时,将启动 ShortcutCreateActivity,我将执行所需的操作。
但是!我想避免打开 Activity 的需要。相反,我想创建一个发送广播的快捷方式。
我尝试将 Intent 替换为:
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,
new Intent(INTENT_ACTION_THAT_I_WANT)
但是当我点击快捷方式时,它会显示 Toast“App isn't installed”。
是否可以创建发送广播而不是启动 Activity 的主屏幕快捷方式?
(开始 Activity 并快速完成它不是一种选择)。
谢谢!
最佳答案
Is it possible to create a home screen shortcut that sends a broadcast instead of launching an Activity?
严格来说,不是。
但是,您可以让它指向一个使用 Theme.NoDisplay
设置的 Activity ,在该 Activity 的 onCreate()
中您发送广播并调用 finish()
而不调用 setContentView()
。从视觉上看,这与让快捷方式发送广播本身没有区别。
关于android - 创建发送广播的快捷方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16134636/