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

来源:互联网 发布: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
原创粉丝点击