码迷,mamicode.com
首页 > 其他好文 > 详细

自己实现简单的天气预报应用(7)

时间:2015-03-05 23:30:26      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:

 

7.从文件中加载数据,取消Intent消息传递

 

解决上一篇最后提出问题的一种思路如下:

在程序每次fetch数据的时候同步将当天气象数据写入一个文件中供widget调用,每次更新Widget的时候都从文件中加载数据,

修改的数据如下:

在解析工具类中加入一个方法:

public static WeatherItem parseItem(String itemData) throws JSONException{
        JSONObject object = (JSONObject) new JSONTokener(itemData).nextValue();
        return new WeatherItem(object);
    }

在问价工具类中加入两个方法:

public static void saveWidgetData(Context context,WeatherItem weatherItem) throws JSONException,IOException{
        try{
            OutputStream out=context.openFileOutput(widgetDate,Context.MODE_PRIVATE);
            out.write(weatherItem.toJson().toString().getBytes());
            out.close();
        }catch (JSONException | IOException e){
            e.printStackTrace();
        }
    }

    public static WeatherItem loadWidgetData(Context context) throws IOException,JSONException{
        BufferedReader reader;
        try{
            InputStream in=context.openFileInput(widgetDate);
            reader =new BufferedReader(new InputStreamReader(in));
            StringBuilder builder = new StringBuilder();
            String line;
            while((line = reader.readLine())!=null){
                builder.append(line);
            }

            Log.i("TAG",builder.toString());

            return ParseTools.parseItem(builder.toString());
        }catch (IOException e){
            e.printStackTrace();
        }
        return null;
    }

 

更改fetch方法如下:

private void fetchData(){
        RequestQueue mQueue= Volley.newRequestQueue(getApplicationContext());
        mQueue.add(new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                //服务器有响应的时候才会调用此方法
                try {
                    mWeatherItems = ParseTools.getInstance(jsonObject.toString(),ParseTools.REQUEST_RAW);
                    //保存数据到文件
                    FileTools.saveData(getApplicationContext(), mWeatherItems);
                    Toast.makeText(getApplicationContext(),"更新完毕",Toast.LENGTH_SHORT).show();

                    mAdapter=new ItemAdapter(mWeatherItems);
                    listView.setAdapter(mAdapter);

                    FileTools.saveWidgetData(getApplicationContext(),mWeatherItems.get(0));

                } catch (JSONException | IOException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(getApplicationContext(), "获取失败", Toast.LENGTH_SHORT).show();
            }
        }));
        mQueue.start();
    }

 

在WidgetProvider中只要这样修改就可以了:

@Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        Log.i(TAG,"OnUpdate 更新");
        RemoteViews rv;
        try{
            mItem = FileTools.loadWidgetData(context);
        }catch (IOException | JSONException e){
            e.printStackTrace();
        }
        if(mItem != null){
            rv = new RemoteViews(context.getPackageName(),R.layout.widget_layout);
            rv.setTextViewText(R.id.date,mItem.getDate());
        }else{
            rv = new RemoteViews(context.getPackageName(),R.layout.widget_void_layout);
        }

        for(int appWidgetId : appWidgetIds){
            appWidgetManager.updateAppWidget(appWidgetId,rv);
        }
        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }

 

 

这里还是无法解决当程序安装但是未启动过的时候加入Widget之后,widget与程序启动之后加入的widget不同步的问题。

 

还有一个问题就是如何在程序中实现每天都 自动获取气象更新 然后 更新Widget,下一阶段考虑这个问题。

自己实现简单的天气预报应用(7)

标签:

原文地址:http://www.cnblogs.com/lhyz/p/4316904.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!