Android 启动器快捷方式
我制作了一个简单的打卡/打卡时钟应用程序。我想向用户添加在主屏幕上创建快捷方式的选项,该快捷方式将切换应用程序的状态(超时/超时),但我根本不希望此快捷方式在屏幕上打开应用程序。
这是我的 setupShortcut()
private void setupShortcut() {
Intent shortcutIntent = new Intent(this, Toggle.class);
// shortcutIntent.setClassName(this, Toggle.class.getName());
shortcutIntent.putExtra(EXTRA_KEY, "ToggleShortcut");
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "ToggleShortcut");
Parcelable iconResource = Intent.ShortcutIconResource.fromContext(this, R.drawable.app_sample_code);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
setResult(RESULT_OK, intent);
}
Toggle.class 是一个为我切换状态的活动。在清单中,我有这些设置,
<activity android:name=".Toggle" android:exported="true" android:theme="@android:style/Theme.Translucent.NoTitleBar">
现在我可以在主屏幕上创建一个快捷方式,然后按它。当我第一次按下它时,它会启动 Toggle 活动并很好地完成它,但它还会在屏幕上打开 TimeClock 活动。如果我按下后退按钮,我就会回到家。我现在可以按此快捷键,它将启动切换活动而不更改屏幕。在我添加之前:shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);它每次都会打开 TimeClock 活动。现在这只是第一次。但我希望当按下切换快捷键时它永远不会在屏幕上显示任何内容。有谁知道如何摆脱它?
I have made a simple punch in / punch out time clock application. I want to add the user the option of making a shortcut on the homescreen that will toggle the state of the app(time out / time in) but I don't want this shortcut to open up the app on the screen at all.
here is my setupShortcut()
private void setupShortcut() {
Intent shortcutIntent = new Intent(this, Toggle.class);
// shortcutIntent.setClassName(this, Toggle.class.getName());
shortcutIntent.putExtra(EXTRA_KEY, "ToggleShortcut");
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "ToggleShortcut");
Parcelable iconResource = Intent.ShortcutIconResource.fromContext(this, R.drawable.app_sample_code);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
setResult(RESULT_OK, intent);
}
Toggle.class is an activity that toggles the state for me. In the manifest I have these settings on it
<activity android:name=".Toggle" android:exported="true" android:theme="@android:style/Theme.Translucent.NoTitleBar">
As it is now I can create a shortcut on the home screen then press it. The first time I press it it starts the Toggle activity and completes it fine, but it also opens up the TimeClock activity on the screen. If I then hit the back button I go back to the home. I can now press this shortcut and it will start the Toggle activity and not change the screen. Before I added: shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); It would open up the TimeClock activity every time. Now it is just the first time. But I want it to never show anything on the screen when the Toggle shortcut is pressed. Does anyone have any idea of how to get rid of that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
启动器的问题在于它们启动“活动”,而您想要的是启动“服务”,以便您可以在后台修改应用程序状态。听起来你真正想做的是制作一个 Widget< /a>,不是启动器快捷方式。您可以将小部件配置为具有一个按钮,该按钮可启动将在后台切换内容的服务。
The problem with launchers is that they start Activities, whereas what you want is to start a Service so you can modify the app state in the background. It sounds like what you really want to do is make a Widget, not a launcher shortcut. You can configure the widget to have a button that sets off a Service that will toggle things in the background.
您可以“更简单地”创建一个隐藏的活动来执行您想要的操作!
此活动必须使用透明主题,因此在没有任何特定条件的情况下,它永远不会在 onCreate() 结束时显示并调用 finish() 。
You can "more simply" make a hidden activity that will do what you want!
This activity must use transparent themes so it never shows and call finish() at the end of the onCreate() without any particular conditions.