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

基于百度定位及天气获取的DEMO +fragment+sharedpreference

时间:2015-02-13 14:30:51      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

此工程较BaiduLocationXML相比:
1.植入fragment,结合微信UI
2.在原本主界面的button  textview  textview 移植到Fragment1
3.增加网络判断,网络不通的情况下做另外处理
4.在网络通畅的情况下,将地址信息、天气信息存入xml(sharedpreferences),
    网络不通的情况下,将原先xml信息读出展现

fragment植入

public class Fragment1 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater,
            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment1,container, false);
        locBtn = (Button) rootView.findViewById(R.id.location);
        locBtn.setVisibility(View.INVISIBLE);//隐藏button
        locInfo = (TextView) rootView.findViewById(R.id.location_info);
        weatherInfo = (TextView) rootView.findViewById(R.id.weather_info);
        initializeListeners();
        initialize();//注册广播
        return rootView;
    }
     @Override  
        public void onDestroyView() {  
           getActivity().unregisterReceiver(broadcastReceiver);  //注销广播
           super.onDestroyView();  
        }
public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        // 初始化滑动条1/3
        initTabLine();
        // 初始化界面
        initView();//载入fragment
    }

private void initView() {
        // 实例化对象
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        tv1 = (TextView)findViewById(R.id.tv1);
        tv2 = (TextView) findViewById(R.id.tv2);
        tv3 = (TextView) findViewById(R.id.tv3);
        list = new ArrayList<Fragment>();

        // 设置数据源
        Fragment1 fragment1 = new Fragment1();
        Fragment2 fragment2 = new Fragment2();
        Fragment3 fragment3 = new Fragment3();

        list.add(fragment1);
        list.add(fragment2);
        list.add(fragment3);

        // 设置适配器
        FragmentPagerAdapter adapter = new FragmentPagerAdapter(
                getSupportFragmentManager()) {

            @Override
            public int getCount() {
                return list.size();
            }

            @Override
            public Fragment getItem(int arg0) {
                return list.get(arg0);
            }
        };

        // 绑定适配器
        viewPager.setAdapter(adapter);

        // 设置滑动监听
        viewPager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                // 当页面被选择时,先讲3个textview的字体颜色初始化成黑
                tv1.setTextColor(Color.BLACK);
                tv2.setTextColor(Color.BLACK);
                tv3.setTextColor(Color.BLACK);

                // 再改变当前选择页(position)对应的textview颜色
                switch (position) {
                case 0:
                    tv1.setTextColor(Color.rgb(51, 153, 0));
                    break;
                case 1:
                    tv2.setTextColor(Color.rgb(51, 153, 0));
                    break;
                case 2:
                    tv3.setTextColor(Color.rgb(51, 153, 0));
                    break;
                }
                currentPage = position;
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                Log.i("tuzi", arg0 + "," + arg1 + "," + arg2);

                // 取得该控件的实例
                LinearLayout.LayoutParams ll = (android.widget.LinearLayout.LayoutParams) tabline
                        .getLayoutParams();

                if (currentPage == 0 && arg0 == 0) { // 0->1移动(第一页到第二页)
                    ll.leftMargin = (int) (currentPage * tabLineLength + arg1
                            * tabLineLength);
                } else if (currentPage == 1 && arg0 == 1) { // 1->2移动(第二页到第三页)
                    ll.leftMargin = (int) (currentPage * tabLineLength + arg1
                            * tabLineLength);
                } else if (currentPage == 1 && arg0 == 0) { // 1->0移动(第二页到第一页)
                    ll.leftMargin = (int) (currentPage * tabLineLength - ((1 - arg1) * tabLineLength));
                } else if (currentPage == 2 && arg0 == 1) { // 2->1移动(第三页到第二页)
                    ll.leftMargin = (int) (currentPage * tabLineLength - (1 - arg1)
                            * tabLineLength);
                }

                tabline.setLayoutParams(ll);

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }
        });

    }

SharedPreferences:

//保存为xml部分
SharedPreferences perference =context.getSharedPreferences("result",context.MODE_WORLD_READABLE);//xml名为result
editor = perference.edit(); 
editor.putString("地址",addressStr);  
        editor.putString("天气信息",result);  
        //以上的put内容是保存在内容中,完成后利用commit提交  
        editor.commit();
//获取SharedPreferences  
            SharedPreferences perference = getActivity().getApplicationContext().getSharedPreferences("result",getActivity().getApplicationContext().MODE_PRIVATE);  
            //取得key为name的值,如果不存在,则返回空值  
            String addr = perference.getString("地址","");  
            //取得key为age的值,如果不存在,则返回0  
            String weather = perference.getString("天气信息","");

网络连通状态判断

//构造函数类型
public class NetworkStatus {
    public boolean netStatus = false;

    public NetworkStatus(Context context) {
        try {
            ConnectivityManager connectManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetworkInfo = connectManager
                    .getActiveNetworkInfo();
            if (activeNetworkInfo != null) {
                if (activeNetworkInfo.isAvailable()
                        && activeNetworkInfo.isConnected()) {
                    netStatus = true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
//boolean类型
public class NetworkStatus {
    public boolean netStatus = false;
    public NetworkStatus(Context context) {
   ConnectivityManager con=(ConnectivityManager)context.getSystemService   (Activity.CONNECTIVITY_SERVICE);  
    boolean wifi=con.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();  
    boolean internet=con.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();  
    if(internet){  
        //执行相关操作  
        netStatus=true;
        Toast.makeText(context,  
                "当前移动网络已连接!", Toast.LENGTH_LONG)  
                .show();  
    }else if(wifi){ 
        netStatus=true;
        Toast.makeText(context,  
                "当前WIFI已连接", Toast.LENGTH_LONG)  
                .show();  
    } else
    {
        Toast.makeText(context,  
                 "亲,网络连了么?", Toast.LENGTH_LONG)  
                .show(); 
    }
}
}

源码:http://download.csdn.net/detail/xiejun1026/8444569

基于百度定位及天气获取的DEMO +fragment+sharedpreference

标签:

原文地址:http://www.cnblogs.com/NeilLing/p/4290081.html

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