安卓版本更新后进入引导界面
来源:互联网 发布:php curl 参数详解 编辑:程序博客网 时间:2024/05/21 06:16
安卓版本更新后重新进入引导界面
安卓在MainActivity通过getVersionCode获取app版本,与服务器后台的VersionCode判断版本号, 如果有更新,在service中下载,当下载完成之后调用
Intent installAPKIntent = new Intent(Intent.ACTION_VIEW); installAPKIntent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive"); installAPKIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(installAPKIntent);
来安装新的apk,当完成之后,会直接到登录或者首页,而不重新进入引导页面,这是因为 在SplashActivity中判断是否之前到过GuideActivity的值
String is_first=SharedPrefsUtil.getValue(context,getVersionCode(getApplicationContext()), "0");
其中sp中的key是版本号,这样,每次有新的版本更新后,
在sp中的is_first都是不存在的,设置默认值”0”,这样,判断is_first的值就可以了
if (is_first.equals("0")) { Intent intent = new Intent(getApplicationContext(), GuideActivity.class); startActivity(intent); SharedPrefsUtil.putValue(context, MyUtils.getVersionName(context), "1"); finish(); }
public class DownloadService extends IntentService { private static final int BUFFER_SIZE = 10 * 1024; private static final String TAG = "DownloadService"; private NotificationManager mNotifyManager; private Builder mBuilder; public DownloadService() { super("DownloadService"); } @Override protected void onHandleIntent(Intent intent) { mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mBuilder = new Builder(this); String appName = getString(getApplicationInfo().labelRes); int icon = getApplicationInfo().icon; mBuilder.setContentTitle(appName).setSmallIcon(icon); String urlStr = intent.getStringExtra("url"); LogUtils.e(urlStr); InputStream in = null; FileOutputStream out = null; try { URL url = new URL(urlStr); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.setDoOutput(false); urlConnection.setConnectTimeout(10 * 1000); urlConnection.setReadTimeout(10 * 1000); urlConnection.setRequestProperty("Connection", "Keep-Alive"); urlConnection.setRequestProperty("Charset", "UTF-8"); urlConnection.setRequestProperty("Accept-Encoding", "gzip, deflate"); urlConnection.connect(); long bytetotal = 0; if (urlConnection.getResponseCode() == 200) { bytetotal = urlConnection.getContentLength(); } LogUtils.e(bytetotal); int bytesum = 0; int byteread = 0; in = urlConnection.getInputStream(); File dir = StorageUtils.getCacheDirectory(this); String apkName = urlStr.substring(urlStr.lastIndexOf("/") + 1, urlStr.length()); File apkFile = new File(dir, apkName); out = new FileOutputStream(apkFile); byte[] buffer = new byte[BUFFER_SIZE]; int oldProgress = 0; while ((byteread = in.read(buffer)) != -1) { bytesum += byteread; out.write(buffer, 0, byteread); int progress = (int) (bytesum * 100L / bytetotal); if (progress != oldProgress) { if (bytetotal != -1) { updateProgress(progress, false); } else { updateProgress(progress, true); }// LogUtils.e(progress); } oldProgress = progress; } // 下载完成 mBuilder.setContentText(getString(R.string.download_success)).setProgress(0, 0, false); //直接清除通知栏状态 mNotifyManager.cancelAll(); //下载完成后直接弹出安装界面 Intent installAPKIntent = new Intent(Intent.ACTION_VIEW); installAPKIntent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive"); installAPKIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(installAPKIntent); } catch (Exception e) { e.printStackTrace(); } finally { if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } } private void updateProgress(int progress, boolean i) { if (i == false) { //如果获取不到文件大小,则不使用百分比进度条 mBuilder.setContentText(this.getString(R.string.download_progress, progress)).setProgress(100, progress, i); } else { mBuilder.setContentText("正在下载:未知大小").setProgress(100, progress, i); } PendingIntent pendingintent = PendingIntent.getActivity(this, 0, new Intent(), PendingIntent.FLAG_CANCEL_CURRENT); mBuilder.setContentIntent(pendingintent); mNotifyManager.notify(0, mBuilder.build()); }}
/** * 获取版本号 * * @return 当前应用的版本号 */ public static String getVersionCode(Context context) { String version = ""; try { PackageManager manager = context.getPackageManager(); PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0); version = info.versionCode + ""; return version; } catch (Exception e) { e.printStackTrace(); } return version; }
/** * 获取版本号 * * @return 当前应用的版本名 */public static String getVersionName(Context ctx) { PackageManager packageManager = ctx.getPackageManager(); PackageInfo packInfo; try { packInfo = packageManager.getPackageInfo(ctx.getPackageName(), 0); String version = packInfo.versionName; return version; } catch (NameNotFoundException e) { e.printStackTrace(); } return ""; }
0 0
- 安卓版本更新后进入引导界面
- App版本更新后的引导界面
- Fedora 18启动后进入命令行界面
- 安卓版本更新
- 安卓版本更新
- 安卓版本更新
- 安卓 版本更新
- 如何让linux启动后进入字符界面
- 定制android设备启动后进入的初始界面
- [Extjs6] 页面电话号码点击后进入打电话界面
- win7 域环境客户端启动后进入登录界面黑屏
- 安卓更新界面问题
- 安卓技术版本更新
- 关于打开应用白屏或黑屏后进入引导页的问题
- Ubuntu开机后进入登录界面输入密码后又跳回到登录界面
- 安卓 Android 线程更新界面
- 安卓应用的版本更新
- 安卓版本更新的简单Demo
- Swift 图片轮播图
- iOS - NSMutableAttributedString 实现富文本(不同颜色字体、下划线等)
- 奇异值和特征值
- 【九度】题目1065:输出梯形
- 【svn】svn的diff命令用法详解
- 安卓版本更新后进入引导界面
- leetcode 40. Combination Sum II 解题报告
- Hexo+Github pages折腾记
- bzoj3576 [Hnoi2014]江南乐
- 第三方库头文件找不到
- Python开发环境快速搭建
- MyEclipse10破解时出现:integrity check error
- JDeveloper使用Toplink DB Provider发布Webservice
- Oracle transfer parent child tree to json