【问题标题】:Why is setOnClickPendingIntent not working?为什么 setOnClickPendingIntent 不起作用?
【发布时间】:2019-08-06 03:12:26
【问题描述】:

我通过 .setOnClickPendingIntent 将挂起的意图附加到小部件按钮,并且在单击按钮时没有得到任何响应。奇怪的是,当我在我的下载服务中实现相同的代码时,它想要工作(但不会因为服务在循环中运行多个周期,所以活动只是弹出并在单击按钮时再次消失。)

我需要它才能在我的主线程上运行,但如果我弄清楚为什么它没有按预期调用,我就无法终生。

监听器在下面的方法中附加到 RemoteViews 按钮上:

public static void updateWidget(Context context, AppWidgetManager appWidgetManager, int widgetId) {
    mContext = context;

    RemoteViews widgetViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

    // Create a PendingIntent to open the config activity
    Intent configIntent = new Intent(context, ConfigActivity.class);
    configIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
    configIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);

    // Keep in mind PendingIntent uniqueness rules. Use the widget ID as a request code
    PendingIntent configPendingIntent = PendingIntent.getActivity(context, widgetId, configIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    // Attach the PendingIntent to our config button
    widgetViews.setOnClickPendingIntent(R.id.configButton, configPendingIntent);

    appWidgetManager.updateAppWidget(widgetId, widgetViews);

    if(loadChached() != null) {
        Weather weather = loadChached();

        widgetViews.setTextViewText(R.id.conditionsDisplay, weather.getConditions());
        widgetViews.setTextViewText(R.id.tempDisplay, String.valueOf(weather.getDisplayTemp()));
        widgetViews.setTextViewText(R.id.timestampDisplay, weather.getTimeStamp());

        if(loadPreferences(context).equals("Dark")) {
            Log.i(TAG, "updateWidget: Activating Dark Theme");
            widgetViews.setTextColor(R.id.conditionsDisplay, context.getResources().getColor(android.R.color.background_dark));
            widgetViews.setTextColor(R.id.tempDisplay, context.getResources().getColor(android.R.color.background_dark));
            widgetViews.setTextColor(R.id.timestampDisplay, context.getResources().getColor(android.R.color.background_dark));
        }

        else if(loadPreferences(context).equals("Light")) {
            Log.i(TAG, "updateWidget: Activating Light Theme");
            widgetViews.setTextColor(R.id.conditionsDisplay, context.getResources().getColor(android.R.color.white));
            widgetViews.setTextColor(R.id.tempDisplay, context.getResources().getColor(android.R.color.white));
            widgetViews.setTextColor(R.id.timestampDisplay, context.getResources().getColor(android.R.color.white));
        }

        appWidgetManager.updateAppWidget(widgetId, widgetViews);
    }

    Intent updateIntent = new Intent(context, DownloadIntentService.class);
    updateIntent.setAction(WeatherWidgetProvider.ACTION_UPDATE_WEATHER);

    Log.i(TAG, "onUpdate: Starting Service");

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        try {
            context.startForegroundService(updateIntent);
        }

        catch (Exception e) {
            e.printStackTrace();
        }
    }

    else {
        try {
            context.startService(updateIntent);
        }

        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

我希望启动的活动已在清单中注册,并带有一个意图过滤器:

<activity android:name=".ConfigActivity">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
        </intent-filter>
    </activity>

非常感谢任何帮助。

【问题讨论】:

  • 当您单击按钮时,您在 logcat 中看到任何内容吗?不要过滤 logcat,因为你可能会错过一些重要的东西!
  • 它没有被调用。问题是我也在从服务中调用 .updateWidget ,覆盖在提供程序中添加到它的任何内容。一旦我将所有更新移回主线程,问题就解决了。

标签: android android-widget android-pendingintent remoteview


【解决方案1】:

问题是我也在从服务调用 .updateWidget ,覆盖了提供程序中添加到它的任何内容。一旦我将所有更新移回主线程,问题就解决了。

【讨论】:

  • 您可以通过单击答案旁边的绿色复选标记来接受此答案。这会将问题从未回答的问题列表中删除。
猜你喜欢
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
  • 2013-03-07
  • 2016-07-05
  • 2011-08-06
  • 2013-01-18
  • 2012-07-09
相关资源
最近更新 更多