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

    xiaoxiao2021-12-14  18

     版本更新: 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();
    转载请注明原文地址: https://ju.6miu.com/read-963548.html

    最新回复(0)