如何在安卓设备上获取服务器上的APK文件?
1、获取本地APK版本信息
获取当前程序的版本号
public static int getVersionCode(Context mContext) { int versionCode = 0; try { versionCode = mContext.getPackageManager(). getPackageInfo(mContext.getPackageName(), 0).versionCode; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return versionCode; }
获取版本号名称(对应versionName)
public static String getVerName(Context context) { String verName = ""; try { verName = context.getPackageManager(). getPackageInfo(context.getPackageName(), 0).versionName; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return verName; }
2、获取服务器APK版本信息
添加网络访问权限
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <!--下载文件读写权限--> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
服务器上txt文件形式
{ "code": "2.0", "update": "http://hnzldzkj.cn/static/apks/version.txt" }
访问服务器txt文件并解析字段代码(需在线程中调用)
private String getVersion() throws IOException, JSONException { URL url = new URL("http://hnzldzkj.cn/static/apks/version.txt"); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setDoInput(true); httpURLConnection.setDoOutput(true); httpURLConnection.setReadTimeout(8 * 1000); InputStream inputStream = httpURLConnection.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String string; string = bufferedReader.readLine(); JSONObject jsonObject = new JSONObject(string); String strings = jsonObject.getString("code"); return strings; }
3、比较本地APK和服务器上APK的版本号(versionCode)
if(new_apkcode> getVersionCode(getContext())){ //如果新版本的versionCode大于旧版本的 showUpdataDialog(); }else { Toast.makeText(getActivity(),"当前为最新版本!",Toast.LENGTH_SHORT).show(); }
弹出对话框
protected void showUpdataDialog() { AlertDialog dialog = new AlertDialog.Builder(getContext()) .setTitle("发现新版本") .setMessage("是否更新到最新版本?") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 执行下载操作 } }) .setNegativeButton("取消", null) .create(); dialog.show(); }
4、下载安装APK
从服务器下载APK文件
private void downloadAPK(final String apkUrl) { new Thread(new Runnable() { @Override public void run() { try { URL url = new URL(apkUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(10 * 1000); //超时时间 connection.connect(); //连接 if (connection.getResponseCode() == 200) { //返回的响应码200是成功. File file = new File("/mnt/sdcard/yang/dujinyang.apk"); //这里我是手写了。 file.createNewFile(); InputStream inputStream = connection.getInputStream(); ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream(); //缓存 byte[] buffer = new byte[1024 * 10]; while (true) { int len = inputStream.read(buffer); publishProgress(len); if (len == -1) { break; //读取完 } arrayOutputStream.write(buffer, 0, len); //写入 } arrayOutputStream.close(); inputStream.close(); byte[] data = arrayOutputStream.toByteArray(); FileOutputStream fileOutputStream = new FileOutputStream(file); fileOutputStream.write(data); //记得关闭输入流 fileOutputStream.close(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }).start(); }
安装APK文件
private void installAPK(String path) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(new File(path)), "application/vnd.android.package-archive"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); }
卸载APK文件
private void uninstallAPK(String packageURI) { Uri packageUri = Uri.parse("package:" + packageURI); Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageUri); startActivity(uninstallIntent); }
相关问题与解答的栏目:
Q1: 如何确保用户设备上的Android系统支持从SD卡安装APK?
A1: 确保应用的android:minSdkVersion
至少为8,这样应用程序才能支持从SD卡安装APK,还需要在AndroidManifest.xml
中设置android:installLocation="preferExternal"
来指定应用首选安装在外部存储上。
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myapp"> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <application android:installLocation="preferExternal" ... > ... </application> </manifest>
到此,以上就是小编对于“安卓获取服务器apk文件”的问题就介绍到这了,希望介绍的几点解答对大家有用,有任何问题和不懂的,欢迎各位朋友在评论区讨论,给我留言。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权请联系我们,一经查实立即删除!