Android 小部件意图
我有一个小部件类和一个更新小部件的服务类。
我在 onUpdate() 的小部件类中添加了以下代码:
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.countdownwidget);
Intent Intent1 = new Intent(Intent.ACTION_MAIN);
Intent1.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent pendingIntent1 = PendingIntent.getActivity(context, 0, Intent1, 0);
views.setOnClickPendingIntent(R.id.button1, pendingIntent);
Intent Intent2 = new Intent(Intent.ACTION_MAIN);
Intent2.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent pendingIntent2 = PendingIntent.getActivity(context, 0, Intent2, 0);
views.setOnClickPendingIntent(R.id.button2, pendingIntent2);
我还在 onStart() 的小部件服务中添加了以下代码
Intent Intent1 = new Intent(Intent.ACTION_MAIN);
Intent1.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent pendingIntent1 = PendingIntent.getActivity(getApplicationContext(), 0, Intent1, 0);
remoteView.setOnClickPendingIntent(R.id.button1, pendingIntent1);
Intent Intent2 = new Intent(Intent.ACTION_MAIN);
Intent2.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent pendingIntent2 = PendingIntent.getActivity(getApplicationContext(), 0, Intent2, 0);
remoteView.setOnClickPendingIntent(R.id.button2, pendingIntent1);
我遇到的问题是,一旦隐式意图注册应用程序以在 Button1 上启动,按钮 2 与按钮 1 相同。我怎样才能使这两个意图表现不同?即注册并启动不同的应用程序。它使用一个按钮,但另一个按钮启动与第一个按钮相同的东西。上周我一直在寻找让它工作的方法,通读了所有内容,但没有结果。 我将不胜感激你的帮助。 谢谢。
I have a widget class and a service class updating the widget.
I have added in the widget class in onUpdate() the following code:
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.countdownwidget);
Intent Intent1 = new Intent(Intent.ACTION_MAIN);
Intent1.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent pendingIntent1 = PendingIntent.getActivity(context, 0, Intent1, 0);
views.setOnClickPendingIntent(R.id.button1, pendingIntent);
Intent Intent2 = new Intent(Intent.ACTION_MAIN);
Intent2.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent pendingIntent2 = PendingIntent.getActivity(context, 0, Intent2, 0);
views.setOnClickPendingIntent(R.id.button2, pendingIntent2);
And I have also added the following code in the widget service in the onStart()
Intent Intent1 = new Intent(Intent.ACTION_MAIN);
Intent1.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent pendingIntent1 = PendingIntent.getActivity(getApplicationContext(), 0, Intent1, 0);
remoteView.setOnClickPendingIntent(R.id.button1, pendingIntent1);
Intent Intent2 = new Intent(Intent.ACTION_MAIN);
Intent2.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent pendingIntent2 = PendingIntent.getActivity(getApplicationContext(), 0, Intent2, 0);
remoteView.setOnClickPendingIntent(R.id.button2, pendingIntent1);
The problem I am having is that once the implicit intent registers the app to launch on button1, the button2 is identical to button1. How can i make the 2 intents behave differently? i.e register and launch different apps.Its working with one button, but the other button launches the same thing of the first button.I have have been looking to get this to work for the last week, reading things all over but with no result.
I would appreciate your help.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,当然。您的所有四个
Intents
都是相同的:ACTION_MAIN
、CATEGORY_LAUNCHER
,没有指定任何其他内容。我很惊讶这个Intent
居然还能起作用。因此,让按钮执行不同操作的第一步是实际上具有不同的意图。
另外,请不要使用
getApplicationContext()
。只需使用this
,因为Service
是一个Context
。Well, of course. All four of your
Intents
are identical:ACTION_MAIN
,CATEGORY_LAUNCHER
, with nothing else specified. I am surprised thisIntent
even works.So, the first step to having the buttons do different things is to actually have different
Intents
.Also, please do not use
getApplicationContext()
. Just usethis
, as aService
is aContext
.