安卓版本更新后进入引导界面

安卓版本更新后重新进入引导界面

   安卓在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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值