XUtil3.0下载

首先创建一个工程

导入XUtils3.0的依赖

compile 'org.xutils:xutils:3.3.36'
然后创建一个MyApp类在AndroidManifest.xml文件中注册

public class MyApp extends Application{
    @Override
    public void onCreate() {
        super.onCreate();
        //初始化xutils3
        x.Ext.init(this);
        //false 不影响性能
        x.Ext.setDebug(true);
    }
}
导入权限

android:name="android.permission.INTERNET">
android:name="android.permission.ACCESS_NETWORK_STATE">
android:name="android.permission.WRITE_EXTERNAL_STORAGE">
再写一个网络判断的Utils

public class NetWorkUtils {
    //判断网络连接状态
    public static boolean isNetWorkIsAvailable(Context context) {
        //网络连接管理器
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        //获取网络信息
        NetworkInfo info = connectivityManager.getActiveNetworkInfo();
        if (info != null) {
            return true;
        }
       /* if (info.isConnected()) {
            return true;
        }*/


        return false;
    }

    //判断是否是wifi
    public static boolean isWifi(Context context) {
        //网络连接管理器
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        //获取网络信息
        NetworkInfo info = connectivityManager.getActiveNetworkInfo();
        if (info != null && info.getType() == connectivityManager.TYPE_WIFI) {
            return true;
        }

        return false;
    }

    //判断是否是手机流量
    public static boolean isMobile(Context context) {
        //网络连接管理器
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        //获取网络信息
        NetworkInfo info = connectivityManager.getActiveNetworkInfo();
        if (info != null && info.getType() == connectivityManager.TYPE_MOBILE) {
            return true;
        }

        return false;
    }
}
在MainActivity

boolean workIsAvailable = NetWorkUtils.isNetWorkIsAvailable(MainActivity.this);
if (!workIsAvailable) {
    Toast.makeText(MainActivity.this, "网络未连接,请设置网络", Toast.LENGTH_SHORT).show();
    Intent intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
    startActivity(intent);
} else {
    //连接成功下载
    Toast.makeText(MainActivity.this, "网络连接成功", Toast.LENGTH_SHORT).show();

    download();
}
//下载
private void download() {
    final String[] items = {"wifi", "手机流量"};
    new AlertDialog.Builder(this).setTitle("网络选择").setIcon(R.mipmap.ic_launcher).setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

            switch (which) {
                case 0:
                    //wifi下 下载apk
                    downloadApk();
                    break;
                case 1:
                    //手机流量下提醒用户
                    boolean mobile = NetWorkUtils.isMobile(MainActivity.this);
                    if (mobile) {
                        Toast.makeText(MainActivity.this, "现在未使用wifi,将耗用手机流量", Toast.LENGTH_SHORT).show();
                        Intent wifiSettingsIntent = new Intent("android.settings.WIFI_SETTINGS");
                        startActivity(wifiSettingsIntent);

                    }
                    dialog.dismiss();
                    break;
            }
            dialog.dismiss();
        }
    }).show();
}
 
//下载apk
private void downloadApk() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("版本更新");
    builder.setMessage("现在检测到新版本,是否更新?");
    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            updateApk();
        }
    });
    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    builder.create();
    builder.show();




}


//版本更新
private void updateApk() {
    String url = "http://169.254.249.24:8080/08web/app.apk";
    RequestParams params = new RequestParams(url);
    //保存到sdcard
    params.setSaveFilePath(Environment.getExternalStorageDirectory() + "/bawei/");
    //自动文件命令
    params.setAutoRename(true);
    //下载
    x.http().post(params, new Callback.ProgressCallback() {
        @Override
        public void onWaiting() {


        }


        @Override
        public void onStarted() {


        }


        @Override
        public void onLoading(long total, long current, boolean isDownloading) {


        }


        @Override
        public void onSuccess(File result) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(result), "application/vnd.android.package-archive");
            startActivity(intent);
        }


        @Override
        public void onError(Throwable ex, boolean isOnCallback) {


        }


        @Override
        public void onCancelled(CancelledException cex) {


        }


        @Override
        public void onFinished() {


        }
    });




你可能感兴趣的:(XUtil3.0下载)