【问题标题】:Trouble with launching an activity from a widget, when the widget has a configuration activity当小部件具有配置活动时,无法从小部件启动活动
【发布时间】:2013-07-02 16:11:11
【问题描述】:

当小部件具有配置活动时,我无法通过点击主屏幕小部件来启动活动。

通常,我在小部件提供程序类的 onUpdate 方法中有 Intent 和 PendingIntent。但是,据了解,当您为小部件配置活动时,不会调用它。

基本上,我需要知道将这段代码放在哪里

Intent intent = new Intent(context, ClassToLaunch.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
views.setOnClickPendingIntent(R.id.widget, pendingIntent);  

如果您需要查看我的配置活动和小部件提供程序的代码,请告诉我 感谢您抽出宝贵时间阅读本文并提供任何帮助。

干杯

【问题讨论】:

    标签: android android-widget


    【解决方案1】:

    正如你已经看到的here,当你有一个配置活动时,onUpdate 不会被调用,第一次小部件被放置。

    它是这样工作的:用户选择的小部件是列表,当他把它放在他想要的位置时,活动就会自动出现在前面。然后用户可以配置他需要的东西。因此,此时不调用 onUpdate !但是,如果稍后用户单击小部件,将调用 onUpdate,然后您可以处理单击以打开任何活动。我不确定您是否可以调用配置活动来修改小部件,您可能必须添加代码才能使更改生效。

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
            Intent intent = new Intent(context, ClassToLaunch.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
            views.setOnClickPendingIntent(R.id.widget, pendingIntent);  
    
            // Push update for this widget to the home screen
            ComponentName thisWidget = new ComponentName(context, WidgetProvider.class);
            AppWidgetManager manager = AppWidgetManager.getInstance(context);
            manager.updateAppWidget(thisWidget, views);
    
    
    }
    

    【讨论】:

    • 当您点击小部件时,通常会调用 onUpdate,除非存在配置问题。在 onUpdate(Log.d("Mywidget", "OnUpdate"); 方法中添加一个日志,然后检查您的配置文件(manifest 和 appwidget_info.xml)。您可以使用 StackView Widget 示例(developer.android.com/guide/topics/appwidgets/…)跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-28
    • 1970-01-01
    • 2012-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多