Android 小部件意图

发布于 2024-09-17 18:37:04 字数 1456 浏览 3 评论 0原文

我有一个小部件类和一个更新小部件的服务类。

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放血 2024-09-24 18:37:04

我遇到的问题是,一旦隐式意图注册应用程序以在按钮1上启动,按钮2
与button1相同。

嗯,当然。您的所有四个 Intents 都是相同的:ACTION_MAINCATEGORY_LAUNCHER,没有指定任何其他内容。我很惊讶这个 Intent 居然还能起作用。

因此,让按钮执行不同操作的第一步是实际上具有不同的意图。

另外,请不要使用getApplicationContext()。只需使用 this,因为 Service 是一个 Context

The problem I am having is that once the implicit intent registers the app to launch on button1, the button2
is identical to button1.

Well, of course. All four of your Intents are identical: ACTION_MAIN, CATEGORY_LAUNCHER, with nothing else specified. I am surprised this Intent 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 use this, as a Service is a Context.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文