简体   繁体   English

当小部件具有配置活动时,无法从小部件启动活动

[英]Trouble with launching an activity from a widget, when the widget has a configuration activity

I am having trouble with launching an activity by tapping on a home screen widget when the widget has a configuration activity. 当窗口小部件具有配置活动时,我无法通过点击主屏幕窗口小部件来启动活动。

Normally , I have the Intent and the PendingIntent in the onUpdate method in the widget provider class.But , from what understand , that doesn't get called when you have a configure activity for your widget. 通常,我在小部件提供程序类的onUpdate方法中具有Intent和PendingIntent,但是据了解,当您为小部件进行configure活动时不会调用该方法。

basically , I need to know where to put this bit of code 基本上,我需要知道将这段代码放在哪里

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);  

Do let me know if you need to see the code for my configure activity and widget provider Thanks for taking the time to read this and for any help that you can give. 让我知道是否需要查看我的configure活动和窗口小部件提供程序的代码。感谢您抽出宝贵的时间阅读本文以及获得的任何帮助。

Cheers 干杯

As you have already see here , onUpdate is not called when you have a configure activity, the first time the widget is putted. 正如您已经在此处看到的那样,在您第一次进行窗口小部件放置时,在进行configure活动时不会调用onUpdate。

This is how it works : The user choose the widget is the list, and when he placed it on the spot he wants, the activity is bring on front automatically. 它是这样工作的:用户选择小部件作为列表,然后将其放在所需的位置时,活动将自动显示在最前面。 The user can then configure what he needs. 然后,用户可以配置他需要的东西。 Hence, onUpdate is not called at this time ! 因此,此时不调用onUpdate! But if later the user click on the widget, onUpdate will be called, and then you can handle the click to bring any activity on. 但是,如果以后用户单击窗口小部件,则会调用onUpdate,然后您可以处理该单击以进行任何活动。 I'm not sure you can call the configure acitivty to modifiy the widget like that, you will probably have to add code in order to make the changes work. 我不确定您是否可以调用configure acitivty这样修改小部件,您可能必须添加代码才能使更改生效。

@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);


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM