2016-06-08 82 views
0

我正在编写一个应用程序小部件,它从服务器获取数据并将它们显示在appwidget中。 的问题是,在没有互联网连接,在这一点上,系统更新微件,TextView文本值复位与android:text="sometext"将appwidget导致设置值更新为默认值

它发生这样的设置好的默认的文本:

  1. 的Widget放在主屏幕
  2. 互联网连接是活跃
  3. 的Widget成功更新
  4. 来自服务器的响应的文本安装在TextView
  5. 互联网连接不活跃
  6. 系统更新微件的TextView重置为在android:text=""

设置好的,我知道在某个地方我做的并不正确的东西的价值

  • 上的文字,因为在其他与Internet没有连接的小部件(不是我)不会重置。

    文件WidgetProvider.java

    public class WidgetProvider extends AppWidgetProvider { 
    
        public static String LOG_TAG = "MYAPPLOG"; 
    
        @Override 
        public void onReceive(Context context, Intent intent) { 
         super.onReceive(context, intent); 
         Log.d(LOG_TAG, "onReceive"); 
        } 
    
        @Override 
        public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
         super.onUpdate(context, appWidgetManager, appWidgetIds); 
         Log.d(LOG_TAG, "onUpdate"); 
    
         for (int widgetID : appWidgetIds) 
         { 
          updateWidget(context, widgetID); 
         } 
        } 
    
        @Override 
        public void onDeleted(Context context, int[] appWidgetIds) { 
         super.onDeleted(context, appWidgetIds); 
         Log.d(LOG_TAG, "onDeleted"); 
        } 
    
        public void updateWidget(Context context, int widgetID) 
        { 
         context.startService(new Intent(context, UpdatingService.class).putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetID)); 
        } 
    } 
    

    文件UpdatingService.java

    public class UpdatingService extends IntentService { 
    
        public static String LOG_TAG = "MYAPPLOG"; 
    
        public UpdatingService() { 
         super("UpdatingService"); 
        } 
    
        @Override 
        protected void onHandleIntent(Intent intent) { 
    
         // getting widgetID from intent and other vars 
    
         RemoteViews remoteViews = new RemoteViews(getApplicationContext().getPackageName(), 
           R.layout.initial_layout); 
    
          if(isConnected(getApplicationContext())) 
          { 
           String response = getServerResponse(); 
    
           if(response != null) 
           { 
            try { 
             JSONObject JSON = new JSONObject(response); 
             // get data from server 
             // ... 
             // set values to the views 
             remoteViews.setTextViewText(R.id.textView, someText); 
    
            } catch (JSONException e) { 
             e.printStackTrace(); 
             Log.d(LOG_TAG, "JSONObject failed"); 
            } 
           } 
           else 
           { 
            // LOG: error connection to server 
           } 
          } 
          else 
          { 
           // LOG: No internet connection 
          } 
    
         // updating apwidget (set click action for the some button) 
         // if not do update then button will not work 
    
         Intent someIntent = new Intent(getApplicationContext(), WidgetProvider.class); 
         someIntent.setAction(WidgetProvider.ACTION_GOTO); 
         PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 
           widgetID, someIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
         remoteViews.setOnClickPendingIntent(R.id.goToLayout, pendingIntent); 
    
         AppWidgetManager.getInstance(getApplicationContext().getApplicationContext()) 
           .updateAppWidget(widgetID, remoteViews); 
        } 
    
        public boolean isConnected(Context context) { 
         ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
         NetworkInfo ni = cm.getActiveNetworkInfo(); 
         if (ni != null && ni.isConnected()) { 
          return true; 
         } 
         return false; 
        } 
    
        public String getServerResponse() { 
         // using HttpURLConnection 
        } 
    } 
    

    希望对您有所帮助或稍尖。我写了几个小部件,都有这个问题。非常感谢您的关注。

  • 回答

    0

    当AppWidget更新发生那么每个属性必须设置,像文本视图,所有点击听众,颜色等简单的一切都值。未设置的内容将使用布局XML中的默认值。

    在你的情况,你什么也不做else分支(“无网络”),因此您的应用程序插件使用默认的文本结束。因此,当您从服务器获取数据时,必须保存并在下次没有互联网连接时使用它。

    +0

    好吧,我在启动将属性设置为textview但仍不起作用的服务代码之前,粘贴了'updateWidget(Context context,int widgetID)'。每当系统更新没有互联网连接的小部件时,即使这些值是由前一次的服务设置的,它也会从xml中设置值。 –

    +0

    您正在服务中创建应用程序小部件的UI,即RemoteViews remoteViews = new RemoteViews(getApplicationContext()。getPackageName(), R.layout.initial_layout);'。这是“重置”UI的代码。所以,不要更新'updateWidget(Context context,int widgetID)'方法,而是用'// LOG:没有互联网连接'来更新'Service'中的ELSE分支。在那里,您将设置应用程序小部件的文本为您保存的值。 – shelll

    +0

    可能我只需要在一个服务中创建UI('RemoteViews remoteViews = new RemoteViews'),只能使用这个'remoteViews'(当我需要'remoteViews'时不要创建一个)? –