根据url下载apk
来源:互联网 发布:遗传算法解决线路优化 编辑:程序博客网 时间:2024/05/16 01:14
首先我们开看一下效果图:
首先我来说一下,实现这个效果的整体思路:
自定义一个dialog,当有新版的时候,弹出这个dialog,当点击立即更新时,去下载apk。
一、我们首先需要自定义一个dialog
public class VersionDialog extends Dialog { private int layoutRes;//布局文件 private Context mCtx; private TextView versNameTv; private TextView contentTv; public Button positiveBtn; public Button negativeBtn; public VersionDialog(Context context) { super(context); // TODO Auto-generated constructor stub this.mCtx = context; } public VersionDialog(Context context, int resLayout) { super(context); this.mCtx = context; this.layoutRes = resLayout; } public VersionDialog(Context context, int theme,int resLayout) { super(context, theme); // TODO Auto-generated constructor stub this.mCtx = context; this.layoutRes = resLayout; } @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.setContentView(layoutRes); versNameTv = (TextView) findViewById(R.id.umeng_update_vers_name); contentTv = (TextView) findViewById(R.id.umeng_update_content); positiveBtn = (Button) findViewById(R.id.umeng_update_id_ok); negativeBtn = (Button) findViewById(R.id.umeng_update_id_cancel); } public void setValue(String name,String content,int flag){ versNameTv.setText("最新版本:"+name); contentTv.setText(content); if (flag == 1) { negativeBtn.setVisibility(View.GONE); } } }
二、在代码里引用布局,当点击更新时,就下载apk
private void versDialog(String name, String content) { versDialg = new VersionDialog(mCtx, R.style.version_dialog, R.layout.umeng_update_dialog); versDialg.setCancelable(false); versDialg.show(); versDialg.setValue(name, content, updateFlag); versDialg.positiveBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if (!"".equals(versUrl)) { Intent intent = new Intent(mCtx, DownloadService.class); intent.putExtra("apkUrl", versUrl); startService(intent); versDialg.dismiss(); } else { Tools.toast(mCtx, "下载地址有误,请重新登录"); finish(); } } }); versDialg.negativeBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { versDialg.dismiss(); } }); }
R.style.version_dialog的样式
<!-- 版本更新dialog --> <style name="version_dialog" parent="@android:style/Theme.Dialog"> <item name="android:windowFrame">@null</item> <item name="android:windowIsFloating">true</item> <item name="android:windowIsTranslucent">false</item> <item name="android:windowNoTitle">true</item> <item name="android:background">@null</item> <item name="android:backgroundDimEnabled">false</item> <item name="android:windowBackground"> @android:color/transparent </item> </style>
<color name="transparent">#00000000</color>
三、我们来看一下DownloadService
public class DownloadService extends Service { private DownloadManager dm; private long enqueue; private BroadcastReceiver receiver; private String apkName = ""; private String apkUrl; private File apkFile = null; public DownloadService() { } @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. throw new UnsupportedOperationException("Not yet implemented"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Intent intent1 = new Intent(Intent.ACTION_VIEW); //判断是否是AndroidN以及更高的版本 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){ intent1.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); Uri contentUri = FileProvider.getUriForFile(context, "com.sanji.sjmallapp.fileprovider",new File(apkFile.getPath() + File.separator + apkName)); intent1.setDataAndType(contentUri,"application/vnd.android.package-archive"); }else{ intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent1.setDataAndType(Uri.fromFile(new File(apkFile.getPath() + File.separator + apkName)), "application/vnd.android.package-archive"); } startActivity(intent1); stopSelf(); } }; apkUrl = intent.getStringExtra("apkUrl"); registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); startDownload(); return Service.START_STICKY; } private void startDownload(){ dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); DownloadManager.Request request = new DownloadManager.Request(Uri.parse(apkUrl)); request.setMimeType("application/vnd.android.package-archive"); apkName = "商城 " + Tools.getCurrentTime() + ".apk"; apkFile = new File(SDCardUtil.getApkPath()); if (apkFile != null && apkFile.exists() && apkFile.isDirectory()){ for (File item : apkFile.listFiles()){ item.delete(); } }else{ apkFile.mkdirs(); } if(SDCardUtil.checkSdCard()){ //下载到SD卡 request.setDestinationInExternalPublicDir(SDCardUtil.FILEAPK,apkName); }else{ //下载到内置目录 request.setDestinationInExternalFilesDir(MallApplication.getApplication(),SDCardUtil.FILEAPK,apkName); } enqueue = dm.enqueue(request); } @Override public void onDestroy() { unregisterReceiver(receiver); super.onDestroy(); }}
Tools.getApkPath(getApplicationContext()))方法是根据有没有SD卡,来确定存储路径
/** * 检测Sdcard是否存在 * * @return */ public static boolean isExitsSdcard() { if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) return true; else return false; }
/** * 获取APK路径 * * @param context * @return */ public static String getApkPath(Context context) { String apkPath = ""; if (isExitsSdcard()) { // SD卡 apkPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + Constants.apkPath; } else { // data apkPath = context.getFilesDir().getAbsolutePath() + File.separator + Constants.apkPath; } return apkPath; }
public static String rootPath = "ExampleOne"; // 存放apk public static String apkPath = rootPath + File.separator + "apk";
这是调用安卓系统自带的DownLoadManager,下载速度一般自己封装的快。
0 0
- 根据url下载apk
- 根据url下载音乐
- 根据URL下载文件
- 根据URL下载文件
- 根据URL链接下载文件
- Java根据Url下载图片
- android根据url下载图片
- HttpURLConnection根据URL下载图片
- 根据url下载网络图片
- HttpURLConnection根据URL下载图片
- 根据 url地址 下载图片
- java根据url下载图片
- 根据服务器url地址下载文件
- IOS如何根据URL下载内容
- java根据URL地址下载文件
- java根据图片的URL下载图片
- 根据url自动下载图片的demo
- 根据URL将文件下载本地
- 【Day50】PHP单一文件入口框架简析
- 如果你也身在小团队,该选择哪款协作工具?
- 在 Ubuntu 虚拟机中安装 VMware Tools
- 写给Android开发者的混淆使用手册
- Session应用:验证码技术
- 根据url下载apk
- JVM相关学习记录与总结(内存&GC&类加载&工具)
- 带导航的控制器上面再加个带导航的控制器,但第二个导航只从屏幕左边隔一定距离显示到右边的框
- Windows 服务移植到Mono
- Struts2(四)-知识清单
- 使用VideoCapture类调用摄像头读入视频并显示
- nginx+tomcat+redis做集群负载均衡
- 调用了opencv的C++程序调试运行没有问题,但一运行exe就报错debug error R6010
- 基于python3的k-means代码实现