【问题标题】:Android widget Intent without layout没有布局的Android小部件意图
【发布时间】:2014-07-28 15:08:58
【问题描述】:

是否可以让 Intent 在后台运行?就像我假设的一项服务。

当用户点击一个小部件时,我需要执行一个操作(调用网络服务)。

下面是我的代码

Intent intent = new Intent();
intent.putExtra("webServiceId", mWSId);
PendingIntent pending = PendingIntent.getActivity(context, 0,intent, 0);
views.setOnClickPendingIntent(R.id.btnWidgetToggle, pending);
appWidgetManager.updateAppWidget(mAppWidgetId, views);

Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
        mAppWidgetId);
setResult(RESULT_OK, resultValue);

finish();

我是否应该有意对服务或类似的东西执行操作? 谢谢!

更新: 我现在试图通过调用 IntentService 来做到这一点,但它没有调用它。我的代码如下:

Intent intent = new Intent(context, widgetService.class);
intent.putExtra("webServiceId",mWSId);
PendingIntent pending = PendingIntent.getService(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.btnWidgetToggle, pending);

有什么想法吗?

【问题讨论】:

  • 是的,启动一个服务。我会推荐IntentService
  • @Karakuri 感谢您的确认。你知道如何实施吗?我用我当时正在尝试的内容更新了上面的代码......
  • 你在AndroidManifest.xml注册服务了吗?
  • @Karakuri:起来!就是这样!!!!谢谢

标签: android web-services android-intent widget android-widget


【解决方案1】:
  • 我不确定您要达到什么目的
  • 我是对的,您想在点击 widget(view) 时调用小部件

  • 这可以通过调用onClick 操作以简单的方式实现 一个小部件并将web-service 调用代码放在 onCllickListener

示例

button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
              //Place your webservice invocation code here
}}

如果您想以某种方式使用服务,请查看android documentation 了解如何使用它

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    我通过调用 IntentService 解决了这个问题。

    这是我的代码:

    Intent intent = new Intent(context, widgetService.class);
    intent.putExtra("webServiceId",mWSId);
    PendingIntent pending = PendingIntent.getService(context, 0, intent, 0);
    views.setOnClickPendingIntent(R.id.btnWidgetToggle, pending);
    

    谢谢@Karakuri

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多