记录配合服务器实现的项目内更新

该更新也可用于Android6.0以上需要获取权限时,无法打开安装包的情况
首先,你需要一个让后台给你传一个类似<http:// www . xxx . com/123.apk>这样的接口,也就是下载文件的接口。
其次附上检测更新时的弹窗布局文件,名称是dialog_app_download

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/common_dialog_bg"
    >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dip"
        android:gravity="center"
        android:text="提示"
        android:textColor="#000000"
        android:textSize="16sp"
        />
    <TextView
        android:id="@+id/tv_progress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginTop="15dp"
        android:visibility="gone"
        android:textSize="12sp"
        android:textColor="#4b4b4b"
        />
    <ProgressBar
        android:id="@+id/progress_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:max="100"
        android:progressDrawable="@drawable/progress_bar_bg"
        android:visibility="gone"
        style="@android:style/Widget.ProgressBar.Horizontal"
        />
    <TextView
        android:id="@+id/tv_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="@dimen/px_28"
        android:textColor="@color/tv_most_black"
        android:padding="17dp"
        android:text="@string/default_string"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="45dip"
        android:orientation="horizontal"
        >
        <TextView
            android:id="@+id/tv_cancel"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:textColor="#4b4b4b"
            android:textSize="16sp"
            android:text="取消"
            />
        <TextView
            android:id="@+id/tv_sure"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:textColor="@color/app_color_theme"
            android:textSize="16sp"
            android:text="确定"
            />
    </LinearLayout>

</LinearLayout>

ps:字体颜色为你的APP的主题颜色,你可以具体情况具体运用

common_dialog_bg

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    >

    <corners
        android:radius="4dp"
        ></corners>
    <solid
        android:color="@color/white"
        ></solid>
</shape>

progress_bar_bg

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!--  设置背景色(蓝色)  -->
    <item  android:id="@android:id/background" >
        <shape>
            <corners android:radius="1dp" />
            <gradient android:startColor="#f3f3f3"
                android:endColor="#f3f3f3" />
        </shape>
    </item>

    <!--  设置进度条颜色(红色)  -->
    <item  android:id="@android:id/progress" >
        <clip>
            <shape>
                <corners android:radius="1dp" />
                <gradient  android:startColor="@color/app_color_theme"
                    android:endColor="@color/app_color_theme" />
            </shape>
        </clip>
    </item>

</layer-list>

ps:字体颜色为你的APP的主题颜色,你可以具体情况具体运用

在构造函数中初始化

 private  Context mContext;
    private ProgressBar mProgressBar;
    private TextView tv_cancel,tv_sure,tv_content,tv_progress;
    private String down_url;
    private File mFile;
    private String channelName;
    private String banben;
    public AppDownloadDialog(@NonNull Context context) {
        super(context, R.style.mask_dialog);
        mContext = context;
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_app_download,null,false);
        setContentView(view);
        mProgressBar = (ProgressBar) view.findViewById(R.id.progress_bar);
        tv_cancel = (TextView) view.findViewById(R.id.tv_cancel);
        tv_sure = (TextView) view.findViewById(R.id.tv_sure);
        tv_content = (TextView) view.findViewById(R.id.tv_content);
        tv_progress = (TextView) view.findViewById(R.id.tv_progress);

        tv_sure.setOnClickListener(this);
        tv_cancel.setOnClickListener(this);


        WindowManager.LayoutParams lp = getWindow()
                .getAttributes();
        lp.width = (ConfigYibaisong.window_x * 3) / 4;
        lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes(lp);

        try {
            channelName = AnalyticsConfig.getChannel(context);
        } catch (Exception e) {
            e.printStackTrace();
            channelName="";
        }

    }

核心逻辑

  if(mFile!=null){
                    installApk(mFile);
                }else {
                  //此处为核心代码,以OkGo为基础实现
                    showProgress();
                    OkGo.<File>get(down_url).execute(new FileCallback(Environment.getExternalStorageDirectory().getPath(),"YiLinCollege"+banben+".apk") {
                        @Override
                        public void onSuccess(Response<File> response) {
                            mFile = response.body();
                            installApk(mFile);
                            tv_cancel.setEnabled(true);
                            tv_sure.setEnabled(true);
                        }

                        @Override
                        public void downloadProgress(Progress progress) {
                            super.downloadProgress(progress);
                            mProgressBar.setProgress((int) (progress.fraction*100));
                            tv_progress.setText(((int) (progress.fraction*100))+"%");
                        }

                        @Override
                        public void onError(Response<File> response) {
                            super.onError(response);
                            dismiss();
                            AppToastMgr.showToast(response.getException().toString());
                        }
                    });
}

以下为上述代码中的showProgress()方法

    private void showProgress(){
        tv_content.setVisibility(View.GONE);
        mProgressBar.setVisibility(View.VISIBLE);
        tv_progress.setVisibility(View.VISIBLE);
        tv_cancel.setEnabled(false);
        tv_sure.setEnabled(false);
        setCanceledOnTouchOutside(false);
    }

以下为跳转至安装apk界面代码

    private void installApk(File file){
        if(file==null)
            return;
        String fileName = file.getName();
        if (fileName.endsWith(".apk")) {
            Intent install = new Intent(Intent.ACTION_VIEW);
            if(Build.VERSION.SDK_INT>=24) {//判读版本是否在7.0以上
                install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//添加这一句表示对目标应用临时授权该Uri所代表的文件
            } else{
                install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            }
            install.setDataAndType(FileProviderUtils.getUriForFile(mContext,file),
                    "application/vnd.android.package-archive");
            mContext.startActivity(install);
        }

    }

最后附上设置数据及取消弹框的代码

    public void setValue(String download, String content,String banben) {
        this.down_url = download;
        if(tv_content!=null)
        tv_content.setText(content);
        this.banben = banben;
    }

    public void hiddenCancel(){
        if(tv_cancel!=null){
            tv_cancel.setVisibility(View.GONE);
        }
    }

在activity中的应用

 PackageManager pm = MainActivity.this.getPackageManager();
                    PackageInfo pi = pm.getPackageInfo(MainActivity.this.getPackageName(), 0);
if(yourcode>pi.versionCode){

//在这可以加个判断版本号的逻辑

}
AppDownloadDialog dialog_down = new AppDownloadDialog(MainActivity.this);
                        dialog_down.setValue("your down_url","your content",pi.versionName+"");
                        if(must==1){
                            dialog_down.hiddenCancel();
                            dialog_down.setCanceledOnTouchOutside(false);
                        }else{
                            dialog_down.setCanceledOnTouchOutside(true);
                        }
                        dialog_down.show();

完美收工
ps:主要用于检测新版本之后,运用此弹窗工具类

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 170,566评论 25 707
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,200评论 0 17
  • 今天给大家带来暑假的第二道菜,“烤新奥尔良鸡翅”!一听名字是不是就觉得口水3三千丈呀?其实我现在烤的就是平...
    王铭锐阅读 374评论 1 0
  • 昨晚梦到你 梦里下着雪 你牵着我一直走 一步一个脚印 醒来后才发现 原来我和你 早就过完了我们的一生
    提灯小可爱阅读 217评论 2 3