码迷,mamicode.com
首页 > 移动开发 > 详细

Android通过读取网上的配置显示和隐藏广告

时间:2014-10-17 03:47:33      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:android   http   io   os   使用   ar   java   sp   数据   

使用HttpClient读取GitHub上一个地址来判断是否显示或隐藏广告。
request.setHeader(“Cache-Control”, “no-cache”);不使用缓存。DefaultHttpClient默认缓存网页的数据。
在onResume()方法中调用getOnlineConfig()

private Handler mHttpHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            Log.d(TAG, "msg = " + msg);
            if (msg.what == 1) {
                mCanShowAd = true;
                showAds();
            } else {
                mCanShowAd = false;
                hideAds();
            }
        }

    };

    void getOnlineConfig() {
        new Thread(new Runnable() {

            @Override
            public void run() {
                Looper.prepare();
                HttpGet request = new HttpGet("https://raw.githubusercontent.com/skyhacker2/FilpBlocks/master/ad.txt");
                request.setHeader("Cache-Control", "no-cache");
                HttpClient client = new DefaultHttpClient();
                try {
                    HttpResponse response = client.execute(request);
                    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                        HttpEntity entity = response.getEntity();
                        String content = new String(EntityUtils.toString(entity));
                        Log.d(TAG, "配置内容: " + content);
                        if (content.equals("true")){
                            mHttpHandler.sendEmptyMessage(1);
                        } else {
                            mHttpHandler.sendEmptyMessage(0);
                        }
                    }
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }).start();
    }

Android通过读取网上的配置显示和隐藏广告

标签:android   http   io   os   使用   ar   java   sp   数据   

原文地址:http://my.oschina.net/skyhacker2/blog/333058

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