学习Android前后有快有1个月了,本着不耍流氓,谈恋爱就要结婚的信念(其实AD开发也挺有趣的),做了自己的第一个Android小应用。本来准备今天和大家分享的,考虑到在不同屏幕上的效果没测试和本着节约大家流量的前提下准备后天和大家分享我的APP,抽时间把一些大一点的数据缓冲到手机,不每次都去网络上下载。今天和大家分享我在开发这个app中的一些知识点。

1、请求webservices

  上次和大家说到的是请求wcf,以前有一个现成的.net网站里面提供了webservices服务,我就没有把webservices上功能拉出来部署为wcf服务了。先实现功能再说。今天先说webservices了,以后有机会再聊wcf。其实调用webservices服务挺简单的,下面把代码贴上来

    /**
     * 调用webservices服务
     *
     * @param url
     *            服务地址/方法名。 例如:http://www.wenyunge.com/webservices/XXX.asmx/
     *            QueryTopsNew
     * @param params方法参数
     *            例如:List params = new ArrayList(); params.add(new
     *            BasicNameValuePair("count", count));
     * @return
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public String Request(String url, List params) {
        HttpPost request = new HttpPost(url);
        String result = "";
        try {

            request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
            HttpResponse httpResponse = new DefaultHttpClient()
                    .execute(request);
            if (httpResponse.getStatusLine().getStatusCode() != 404) {
                result = EntityUtils.toString(httpResponse.getEntity());
            }
        } catch (Exception e) {
            try {
                throw e;
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
        return result;
    }

简单吧,我就不细说了。

 2、Service、BootBroadcastReceiver学习

  自己用文云阁来看小说,总是要进去后才知道最近有没有更新,这种体验实在是糟透了。作为屌丝的程序员,肯定总能想到办法啦。1、使用BootBroadcastReceiver设置为开机启动(这个好像只有在android4.0以上的系统中才能用上),然后在从写2、onReceive方法时候启动个Service,3、再然后再Service的onCreate里面写一个定时器。定时去检查有没有更新,4、有的话再通过NotificationManager在android通知里面告诉你。这样是不是很屌丝啊。嘿嘿

1、使用BootBroadcastReceiver设置为开机启动

AndroidManifest.xml
权限:
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
设置开机启动
<receiver android:name="BootBroadcastReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" >
                </action>

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </receiver>

2、onReceive方法时候启动个Service

public void onReceive(Context context, Intent intent) {
        // 后边的XXX.class就是要启动的服务
        Intent service = new Intent(context, BookUpdateService.class);
        context.startService(service);
        Log.v("TAG", "更新提醒服务已经启动");
        // 启动应用,参数为需要自动启动的应用的包名
        // Intent intent = getPackageManager().getLaunchIntentForPackage(
        // packageName);

        // context.startActivity(intent);
    }

3、再然后再Service的onCreate里面写一个定时器

public void onCreate() {
        // TODO Auto-generated method stub
        // Log.i("myService", "onCreate");

        Timer timer = new Timer();

        // 3. 启动定时器,
        timer.schedule(task, , );

        Log.e("myService", "onCreate");
        System.out.print("onCreate");
    }

特意设置1800000半小时检查一次,太快了流量还要钱呢。

4、有的话再通过NotificationManager在android通知里

@SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            // 要做的事情
            Bitmap btm = BitmapFactory.decodeResource(getResources(),
                    R.drawable.ic_launcher);
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                    BookUpdateService.this)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle("文云阁").setContentText("您的书架有新更新");
            mBuilder.setTicker("您的书架有新更新");// 第一次提示消息的时候显示在通知栏上
            mBuilder.setNumber(msg.arg2);
            mBuilder.setLargeIcon(btm);
            mBuilder.setAutoCancel(true);// 自己维护通知的消失

            // 构建一个Intent
            Intent resultIntent = new Intent(BookUpdateService.this,
                    MainActivity.class);
            // 封装一个Intent
            PendingIntent resultPendingIntent = PendingIntent.getActivity(
                    BookUpdateService., resultIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            // 设置通知主题的意图
            mBuilder.setContentIntent(resultPendingIntent);
            // 获取通知管理器对象
            NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            mNotificationManager.notify(, mBuilder.build());
        }
    };
 3、系统更新

屌丝的人生你不懂,做出来的东西也就是马马虎虎的,总有bug,总要升级

private void notNewVersionShow() {
        StringBuffer sb = new StringBuffer();
        sb.append("当前版本:");
        sb.append(BaseActivity.getVerName(ToolActivity.this));
        sb.append(" Code:");
        sb.append(BaseActivity.getVerCode(ToolActivity.this));
        sb.append(",/n已是最新版,无需更新!");
        Dialog dialog = new AlertDialog.Builder(ToolActivity.this)
                .setTitle("软件更新").setMessage(sb.toString())// 设置内容
                .setPositiveButton("确定",// 设置确定按钮
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                            }
                        }).create();// 创建
        // 显示对话框
        dialog.show();
    }

    private void doNewVersionUpdate() {
        StringBuffer sb = new StringBuffer();
        sb.append("当前版本:");
        sb.append(BaseActivity.getVerName(ToolActivity.this));
        sb.append(" Code:");
        sb.append(BaseActivity.getVerCode(ToolActivity.this));
        sb.append(", 发现新版本:");
        sb.append(name);
        sb.append(" Code:");
        sb.append(code);
        sb.append(", 是否更新?");
        Dialog dialog = new AlertDialog.Builder(ToolActivity.this)
                .setTitle("软件更新").setMessage(sb.toString())
                // 设置内容
                .setPositiveButton("更新",// 设置确定按钮
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                pBar = new ProgressDialog(ToolActivity.this);
                                pBar.setTitle("正在下载");
                                pBar.setMessage("请稍候...");
                                pBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                                downFile();
                            }
                        }).create();// 创建
        // .setNegativeButton("暂不更新",
        // new DialogInterface.OnClickListener() {
        // public void onClick(DialogInterface dialog,
        // int whichButton) {
        // // 点击"取消"按钮之后退出程序
        //
        // }
        // }).create();// 创建
        // 显示对话框
        dialog.show();
    }

    ProgressDialog pBar;

    void downFile() {
        pBar.show();
        new Thread() {
            public void run() {
                HttpClient client = new DefaultHttpClient();
                HttpGet get = new HttpGet(getResources().getString(
                        R.string.site_url)
                        + "wenyunge.apk");
                HttpResponse response;
                try {
                    response = client.execute(get);
                    HttpEntity entity = response.getEntity();
                    long length = entity.getContentLength();
                    InputStream is = entity.getContent();
                    FileOutputStream fileOutputStream = null;
                    if (is != null) {
                        File file = new File(
                                Environment.getExternalStorageDirectory(),
                                "wenyunge.apk");
                        fileOutputStream = new FileOutputStream(file);
                        ];
                        ;
                        ) {
                            fileOutputStream.write(buf, , ch);
                            ) {
                            }
                        }
                    }
                    fileOutputStream.flush();
                    if (fileOutputStream != null) {
                        fileOutputStream.close();
                    }
                    down();
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();

    }

    void down() {
        handler.post(new Runnable() {
            public void run() {
                pBar.cancel();
                update();
            }
        });
    }

    void update() {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        File f = new File(Environment.getExternalStorageDirectory(),
                "wenyunge.apk");
        if (f.exists()) {
            intent.setDataAndType(Uri.fromFile(f),
                    "application/vnd.android.package-archive");
            startActivity(intent);
        }
    }

其实这段代码是我看别人的,那兄弟的网址不记得了,不然给大家贴出来
我跟大家分享个错误,在升级app的时候提示,在下面

 4、an existing package by the same name with a conflicting signature is already installed

是因为新app签名和原来的不同

 5、最后了,app还是有问题,大家喜欢的下来玩玩,有问题,告诉我,我会尽快给解决。
 支持分享,有愿意一起学习的也可以Q我,我的源码放在svnChina上了。

扫描二维码下载

 觉得还看的过去的,一定要先赞下啊。呵呵
 仅为学习,和方便大家

Android宝典入门篇-进阶的更多相关文章

  1. Android宝典入门篇-基础知识

    今天跟大家分享的是我学android时的笔记.以前搞net很多年了,现在还在搞这.本着活到老学到老抽了点时间学习了下android.android网上有很多的视频教程,当时对于我这样以前不了解java ...

  2. Android从入门到进阶——布局

    一.组件 1.UI组件 (Android.view.View的子类或者间接子类) 2.容器组件(Android.view.ViewGroup子类或者间接子类) 二.UI组件:TextView,Spin ...

  3. Android测试入门篇

    Android本身是一套软件堆叠(Software Stack),或者成为软件叠层架构,叠层主要分成三层:操作系统.中间件和应用程序. Android构架 1. Application 应用程序层:用 ...

  4. 服务端技术进阶(八)GitHub入门篇

    服务端技术进阶(八)GitHub入门篇 前言 在投递简历的过程中,发现有的公司会要求填写自己的GitHub地址,而自己却还没有GitHub帐号,准确点说是自己还不太会使用GitHub.(貌似开源社区中 ...

  5. Android Studio2.0 教程从入门到精通Windows版 - 入门篇

    http://www.open-open.com/lib/view/open1468121363300.html 本文转自:深度开源(open-open.com)原文标题:Android Studio ...

  6. Scala进阶之路-并发编程模型Akka入门篇

    Scala进阶之路-并发编程模型Akka入门篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Akka Actor介绍 1>.Akka介绍 写并发程序很难.程序员不得不处 ...

  7. 【Android开发日记】之入门篇(十二)——Android组件间的数据传输

    组件我们有了,那么我们缺少一个组件之间传递信息的渠道.利用Intent做载体,这是一个王道的做法.还有呢,可以利用文件系统来做数据共享.也可以使用Application设置全局数据,利用组件来进行控制 ...

  8. 【Android开发日记】之入门篇(三)——Android目录结构

    本来的话,这一章想要介绍的是Android的系统架构,毕竟有了这些知识的储备,再去看实际的项目时才会更清楚地理解为什么要这样设计,同时在开发中遇到难题,也可以凭借着对Android的了解,尽快找出哪些 ...

  9. Android数据绑定DataBinding(二)入门篇

    前言 之前写了Android数据绑定DataBinding(一)入门篇,很简单的记录了如何使用DataBinding,其初衷是想要代码中的数据发生改变,不需要繁琐的setText等操作,在最后说到了只 ...

随机推荐

  1. loadrunner中controller 中scenario-> rendezvous灰色不可用的解决方法:

    1.首先确保lr_rendezvous("login");函数添加成功  Action() { web_set_max_html_param_len("2048" ...

  2. Scrollify – jQuery全屏滚动插件

    和 fullPage.js 一样,Scrollify 也是一款基于 jQuery 的全屏滚动插件.跟 fullPage.js 相比,Scrollify 更加小巧,压缩后不足 4KB.但功能上不如 fu ...

  3. CodeForces 811B Vladik and Complicated Book

    离线,树状数组. 数据范围好像有点小,直接暴力可以过的. 我直接上了$n,Q≤100000$的做法:只需要判断区间上比$x$小的数字有几个即可,可以对询问进行离线操作,从左到右一个一个数字插入到树状数 ...

  4. localstorage跟sessionstorage

    能查到证明有人指引,直接上代码 <script type="text/javascript"> function cunchu1(){ var arr = [ 1, 2 ...

  5. HDU 6199gems gems gems (DP)

    gems gems gems Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  6. 【十大经典数据挖掘算法】k

    [十大经典数据挖掘算法]系列 C4.5 K-Means SVM Apriori EM PageRank AdaBoost kNN Naïve Bayes CART 1. 引言 k-means与kNN虽 ...

  7. 【BZOJ 3229】 3229: [Sdoi2008]石子合并 (GarsiaWachs算法)

    3229: [Sdoi2008]石子合并 Description 在一个操场上摆放着一排N堆石子.现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆石子合并成新的一堆,并将新的一堆石子数记为该次合 ...

  8. Linux Shall命令入门

    Linux Shall命令入门 ifconfig                                            //查看ip信息 service network start   ...

  9. Codeforces 521 E cycling city

    cf的一道题,非常有意思,题目是问图中是否存在两个点,使得这两个点之间有三条路径,而且三条路径没有公共点. 其实就是判断一下是否为仙人掌就行了,如果不是仙人掌的话肯定就存在,题目难在输出路径上,改了半 ...

  10. JZYZOJ1525 HAOI2012道路 堆优化的dijkstra+pair

    From Tyvj Guest ☆[haoi2012]道路                 描述 Description     C国有n座城市,城市之间通过m条单向道路连接.一条路径被称为最短路,当 ...