如何申请变更将在onEnabled主屏幕时,窗口小部件? [英] How to apply changes to widget when placing it on the homescreen in onEnabled?

查看:248
本文介绍了如何申请变更将在onEnabled主屏幕时,窗口小部件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我试图把我的天气小部件我想给用户在那一刻选择一个城市的一个选项,主屏幕和小部件开始显示该城市的天气。为此,我已在Provider类利用onEnabled的方法。

所以我在做什么这里刚开始有些preference类,我利用字符串数组给用户选择的选项的活动。然后里面OnShared preferenceChangeListener onEnabled我再次开始与城市的URL另一个活动。这个新acitvity类将处理的天气在启动时,当我试图把天气​​小部件的homecreen首次逻辑。所以我只是想设置一些文本框在这个新的活动课,让他们将出现在窗口小部件时我把他们在屏幕上,但我不能我不知道它没有被应用的原因。这里是我下面的AppWidgetProvider的code。

 公共类MyWeatherAppWidgetProvider扩展AppWidgetProvider {
私人字符串URL;
@覆盖
公共无效onEnabled(上下文的背景下){
    // TODO自动生成方法存根
    super.onEnabled(上下文);
    最后上下文this_context =背景;    意向意图=新意图(context.getApplicationContext(),编辑prefs.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(意向);    共享preferences preFS = preferenceManager.getDefaultShared preferences(背景);    OnShared preferenceChangeListener监听器=新的共享preferences.OnShared preferenceChangeListener(){
           公共无效onShared preferenceChanged(共享preferences共享preferences,
                    字符串键){
               共享preferences preFS = preferenceManager.getDefaultShared preferences(this_context);
               URL = prefs.getString(城市,http://www.yr.no/place/Nepal/Bagmati/Kathmandu/forecast.xml);
               的System.out.println(URL);
               意向意图=新意图(this_context.getApplicationContext(),VaxjoWeather.class);
               intent.putExtra(城市,URL);
               intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
               this_context.startActivity(意向);            }
        };    prefs.registerOnShared preferenceChangeListener(监听);
    URL = prefs.getString(城市,http://www.yr.no/place/Nepal/Bagmati/Kathmandu/forecast.xml);
    的System.out.println(URL);}


解决方案

我不会依赖 onEnabled 正确为你做的事情。据我所知,还有它允许时,窗口小部件配置被取消widgetIds留连忘返的错误。这意味着,当你把一个第一小和取消配置,一个ID将仍然存储在系统中的具体AppWidgetProvider,从而onEnabled不叫当你再次把它,因为它仍然有一个鬼的ID已在其appWidgetProviderIds。

When I try to put my weather widget on the homescreen I want to give the user an option of choosing a city at that moment and the widget starts showing the weather for that city. For this I have made use of onEnabled method in Provider class.

So what I am doing here is just starting an activity to some preference class where I make use of string array to give the user the option to choose. And then OnSharedPreferenceChangeListener inside onEnabled again I am starting another activity with the url of city. This new acitvity class will handle the logic of weather at start up when I try to put the weather widget on the homecreen for the first time. So I am just trying to set some textFields in this new activity class so that they will appear in the widget when i put them on the screen, but I cant I dont know why its not being applied. Here is the code below of my AppWidgetProvider.

public class MyWeatherAppWidgetProvider extends AppWidgetProvider{
private String url;
@Override
public void onEnabled(Context context) {
    // TODO Auto-generated method stub
    super.onEnabled(context);
    final Context this_context = context;

    Intent intent = new Intent(context.getApplicationContext(), EditPrefs.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

    OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener() { 
           public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
                    String key) {
               SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this_context);
               url = prefs.getString("cities", "http://www.yr.no/place/Nepal/Bagmati/Kathmandu/forecast.xml");
               System.out.println(url);
               Intent intent = new Intent(this_context.getApplicationContext(), VaxjoWeather.class);
               intent.putExtra("city", url);
               intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
               this_context.startActivity(intent);

            }
        };

    prefs.registerOnSharedPreferenceChangeListener(listener);


    url = prefs.getString("cities", "http://www.yr.no/place/Nepal/Bagmati/Kathmandu/forecast.xml");
    System.out.println(url);

}

解决方案

I wouldn't rely on onEnabled to do things properly for you. As far as I know there is still a bug which allows for widgetIds to linger when widget configuration gets cancelled. This means that when you place a first widget and cancel the configuration, an Id is still stored in the system for the specific AppWidgetProvider, and thus onEnabled isn't called when you place it again since it still has a 'ghost' id already in its appWidgetProviderIds.

这篇关于如何申请变更将在onEnabled主屏幕时,窗口小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆