app版本更新,安装,卸载,启动,分享

 版本更新: Android网络编程 ,解析json,Handler,线程。

  注: 耗时操作

  代码:
            Get请求
                    URL url = new URL("http://ip:8080/update.json"); //URL
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("GET");                            // 请求方法
                    conn.setConnectTimeout(5000) ;                           // 连接超时
                    conn.setReadTimeout(5000);                                 // 响应超时, 虽连接上了,但服务器迟迟不给响应
                    conn.connect();                                                           // 连接服务器

                    int responseCode = conn.getResponseCode();               // 获取响应码,返回码为200表示正常,
                                                                                                                         //当下载资源的某一部分时返回的是206
                    InputStream inputStream = conn.getInputStream();         //获取服务端返回的结果

    
            解析json
                        JSONObject jo = new JSONObject(result);                     //根据result得到一个Json对象,result为服务端返回的.json文件
                        mVersionName = jo.getString("versionName");            //获取Json中具体值
                        mVersionCode = jo.getInt("versionCode");
                        mDesc = jo.getString("description");
                        mDownloadUrl = jo.getString("downloadUrl");
            判断是否有更新
                        if (mVersionCode > getVersionCode()) {
                          //更新软件并下载

                          //xUtils包的使用

                        }

            app安装:主要是跳转到系统安装界面进行安装

                 action:  Intent.ACTION_VIEW = "android.intent.action.VIEW"--------Display the data to the user 启动activity用的
                 data:       URI.fromFile(new File(path))            ----------- app文件
                 type:"application/vnd.android.package-archive"   ----------  是文件类型,具体为APK的互联网媒体类型
                 
                 代码:
                    // 跳转到系统安装页面
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.addCategory(Intent.CATEGORY_DEFAULT);
                    intent.setDataAndType(Uri.fromFile(path),"application/vnd.android.package-archive");
                    startActivityForResult(intent, 0);

            app卸载:
                删除一个app所对应的intent-filter 
                <intent-filter> 
                <action android:name="android.intent.action.VIEW" /> 
                <action android:name="android.intent.action.DELETE" /> 
                <category android:name="android.intent.category.DEFAULT" /> 
                <data android:scheme="package" /> 
                </intent-filter>
            
            app启动
                PackageManager pm = getPackageManager();
                // 获取手机中所有具有启动能力的activities,做桌面app时运用
                // Intent intent = new Intent();
                // intent.setAction(Intent.ACTION_MAIN);
                // intent.addCategory(Intent.CATEGORY_LAUNCHER);
                // List<ResolveInfo> activities = pm.queryIntentActivities(intent,
                // PackageManager.GET_INTENT_FILTERS);
                Intent intent = pm.getLaunchIntentForPackage(mAppInfo.getPackName());

                if (intent != null) {
                    startActivity(intent);
                    dismissPopupWindow();
                }
            
            app分享
                代码:
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_SEND);
                    intent.addCategory(Intent.CATEGORY_DEFAULT);
                    intent.setType("text/plain");
                    intent.putExtra(Intent.EXTRA_TEXT, 推荐具体内容);
                    startActivity(intent);
 注:
    1、ByteArrayOutputStream在网络通信的使用:
      代码:
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int len = 0;
        byte[] buffer = new byte[1024];

        while ((len = in.read(buffer)) != -1) {
            out.write(buffer, 0, len);
        }

        String result = out.toString();
        in.close();
        out.close();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值