版本更新

//banbengengxin

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="330dp"
        android:background="@drawable/gengxinbg">


        <android.support.v7.widget.RecyclerView
            android:id="@+id/remarks"
            android:layout_width="match_parent"
            android:layout_height="130dp"
            android:layout_marginRight="23dp"
            android:layout_marginLeft="23dp"
            android:layout_marginTop="115dp"
            ></android.support.v7.widget.RecyclerView>

       <LinearLayout
           android:layout_marginTop="20dp"
           android:layout_below="@+id/remarks"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:orientation="horizontal">
           <Button
               android:background="@drawable/button_home1"
               android:id="@+id/quxiao"
               android:layout_marginLeft="32dp"
               android:layout_width="0px"
               android:layout_height="35dp"
               android:layout_weight="1"
               android:text="残忍拒绝"
               android:layout_gravity="center_horizontal"/>

           <Button
               android:layout_marginLeft="15dp"
               android:textColor="#ffff"
               android:background="@drawable/button_home"
               android:id="@+id/gengxin"
               android:layout_marginRight="32dp"
               android:layout_width="0px"
               android:layout_height="35dp"
               android:layout_weight="1"
               android:text="立即更新"
               android:layout_gravity="center_horizontal"/>

       </LinearLayout>

    </RelativeLayout>
</LinearLayout>

 

item_updataversion

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:padding="3dp"
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:textColor="#333"
        android:text="更新了"
        />

</LinearLayout>

 

  style样式

<style name="dialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:background">@android:color/transparent</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:backgroundDimAmount">0.6</item>

    </style>

 

  

button_home

 

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="40dp" />
    <gradient
        android:endColor="#fd4a01"
        android:startColor="#fd7701" />
</shape>

button_home1

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="20dp" />
    <gradient
        android:endColor="#EBEBEB"
        android:startColor="#EBEBEB" />
</shape>

 

MainActivity

public class MainActivity extends AppCompatActivity {

    //版本更新内容
    private List<String> versionList;
    public static final int SHOW_ERROR = 1;
  //  private String fileUrl=" http://www.czbx360.com/static/car-insurance-1.0.apk";
       String fileUrl="http://www.apk.anzhi.com/data3/apk/201703/14/4636d7fce23c9460587d602b9dc20714_88002100.apk";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //模拟服务器返回版本号为2大于更新
        int code=2;

        if (code>getVersionCode()) {

            //版本更新内容
            initversion();
            //版本更新弹框
            initdata();
        }


    }

    /**
     * 版本更新弹框
     * */
    private void initdata() {
        View view = View.inflate(this, R.layout.banbengengxin, null);
        final MyDialog dialog = new MyDialog(MainActivity.this, 0, 0, view, R.style.dialog);
        dialog.setCancelable(false);   //返回键不可关闭
        dialog.show();

        Button gengxin = view.findViewById(R.id.gengxin);
        Button quxiao = view.findViewById(R.id.quxiao);
        //版本更新内容
        RecyclerView tv_remarks = view.findViewById(R.id.remarks);
        tv_remarks.setAdapter(new UpdataversionAdapter(MainActivity.this, versionList));
        tv_remarks.setLayoutManager(new LinearLayoutManager(this));

        gengxin.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View view) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    boolean hasInstallPermission =
                            getApplicationContext().getPackageManager().canRequestPackageInstalls();
                    if (!hasInstallPermission) {
                        startInstallPermissionSettingActivity();
                        return;
                    }
                }
                //下载apk
                downloadApk();
            }
        });

        //取消退出app
        quxiao.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View view) {
                dialog.dismiss();
                finish();
            }
        });

    }


    /**
     * 版本更新内容
     * */
    private void initversion() {
        versionList = new ArrayList<>();
        versionList.add("1.新增注册登录功能");
        versionList.add("2.新增钱包功能");
        versionList.add("3.新增名片功能");
        versionList.add("4.新增团员功能");
        versionList.add("5.修复部分bug");
    }

    /**
     * 下载apk
     */
    private void downloadApk() {
        //显示下载进度
        ProgressDialog dialog = new ProgressDialog(this);
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.setCancelable(false);
        dialog.show();

        //访问网络下载apk
        new Thread(new DownloadApk(dialog)).start();
    }


    /**
     * 访问网络下载apk
     */
    private class DownloadApk implements Runnable {
        private ProgressDialog dialog;
        InputStream is;
        FileOutputStream fos;

        public DownloadApk(ProgressDialog dialog) {
            this.dialog = dialog;
        }

        @Override
        public void run() {
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder().get().url("http://www.apk.anzhi.com/data3/apk/201703/14/4636d7fce23c9460587d602b9dc20714_88002100.apk").build();
            try {
                Response response = client.newCall(request).execute();
                if (response.isSuccessful()) {
                    //获取内容总长度
                    long contentLength = response.body().contentLength();
                    //设置最大值
                    dialog.setMax((int) contentLength);
                    //保存到sd卡
                    File apkFile = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".apk");
                    fos = new FileOutputStream(apkFile);
                    //获得输入流
                    is = response.body().byteStream();
                    //定义缓冲区大小
                    byte[] bys = new byte[1024];
                    int progress = 0;
                    int len = -1;
                    while ((len = is.read(bys)) != -1) {
                        try {
                            Thread.sleep(1);
                            fos.write(bys, 0, len);
                            fos.flush();
                            progress += len;
                            //设置进度
                            dialog.setProgress(progress);
                        } catch (InterruptedException e) {
                            Message msg = Message.obtain();
                            msg.what = SHOW_ERROR;
                            msg.obj = "ERROR:10002";
                        }
                    }
                    //下载完成,提示用户安装
                    installApk(apkFile);
                }
            } catch (IOException e) {
                Message msg = Message.obtain();
                msg.what = SHOW_ERROR;
                msg.obj = "ERROR:10003";
            } finally {
                //关闭io流
                if (is != null) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    is = null;
                }
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    fos = null;
                }
            }
            dialog.dismiss();
        }
    }


    /**
     * 下载完成,提示用户安装
     */
    private void installApk(File file) {
        //调用系统安装程序
        Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
        startActivityForResult(intent, 1);
    }

    /**
     * 跳转到设置-允许安装未知来源-页面
     */
    @RequiresApi(api = Build.VERSION_CODES.O) private void startInstallPermissionSettingActivity() {
        //注意这个是8.0新API
        Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        getApplication().startActivity(intent);
    }

    /*
     * 获取当前程序的版本号
     */
    private int getVersionCode() {
        try {

            //获取packagemanager的实例
            PackageManager packageManager = getPackageManager();
            //getPackageName()是你当前类的包名,0代表是获取版本信息
            PackageInfo packInfo = packageManager.getPackageInfo(getPackageName(), 0);
            return packInfo.versionCode;

        } catch (Exception e) {
            e.printStackTrace();

        }

        return  1;
    }

}
 

 

MyDialog

/**
 * 弹出框
 */
public class MyDialog extends Dialog {

    private static int default_width = 200; //默认宽度
    private static int default_height = 120;//默认高度

    public MyDialog(Context context, View layout, int style) {
        this(context, default_width, default_height, layout, style);
    }

    public MyDialog(Context context, int width, int height, View layout, int style) {
        super(context, style);
        setContentView(layout);
        setwidthhegih(width,height);
    }

    public void setwidthhegih(int top,int botton){

        /**
         * 设置宽度全屏,要设置在show的后面
         * */

        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.gravity = Gravity.CENTER;
        layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT;
        layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;

        getWindow().getDecorView().setPadding(40, top, 40, botton);

        getWindow().setAttributes(layoutParams);
    }

}

 

 

UpdataversionAdapter

public class UpdataversionAdapter extends RecyclerView.Adapter<UpdataversionAdapter.Myhodler> {

    private Context context;
   private List<String> list;

    public UpdataversionAdapter(Context context, List<String> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public Myhodler onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_updataversion, parent, false);
        Myhodler myhodler=new Myhodler(inflate);
        return myhodler;
    }

    @Override
    public void onBindViewHolder(Myhodler holder, int position) {
       holder.tv.setText(list.get(position));
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class Myhodler extends RecyclerView.ViewHolder{
        public TextView tv;
        public Myhodler(View itemView) {
            super(itemView);
            this.tv=itemView.findViewById(R.id.tv);
        }
    }
}
 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值