判断网络,wifi 4g,
new AlertDialog.Builder(MainActivity.this)
.setIcon(R.mipmap.ic_launcher)
.setTitle("网络选择")
.setSingleChoiceItems(new String[] {"wife","手机流量"},2,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch (which){
case 0:
Toast.makeText(MainActivity.this, "wife", Toast.LENGTH_SHORT).show();
dialog.dismiss();
new AlertDialog.Builder(MainActivity.this)
.setTitle("版本更新")
.setMessage("现在检测到新版本,是否更新?")
.setPositiveButton("下载", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
downloadApk();
dialog.dismiss();
}
})
.setNegativeButton("取消", null)
.create()
.show();
break;
case 1:
Toast.makeText(MainActivity.this, "跳到wife界面", Toast.LENGTH_SHORT).show();
//跳转网络设置界面 隐士意图
Intent intent = new Intent();
intent.setAction("android.settings.WIRELESS_SETTINGS");
startActivity(intent);
dialog.dismiss();
break;
}
}
}
)
.setNegativeButton("取消", null)
.show();
Xutils请求数据
private void getData() {
String path = "http://v.juhe.cn/toutiao/index";
RequestParams params = new RequestParams(path);
params.addQueryStringParameter("key","5b6258c74f4346147b12fe38490a12b2");
params.addQueryStringParameter("type","top");
x.http().get(params, new Callback.CommonCallback<String>() {
private MyBaseAdapter adapter;
@Override
public void onSuccess(String result) {
System.out.println("-----------------"+result);
Gson gson = new Gson();
Yuekao2Info yuekao2Info = gson.fromJson(result, Yuekao2Info.class);
List<Yuekao2Info.ResultBean.DataBean> data = yuekao2Info.getResult().getData();
if (adapter == null) {
adapter = new MyBaseAdapter(data,MainActivity.this);
xListView.setAdapter(adapter);
}else {
adapter.loadMore(data,flag);
adapter.notifyDataSetChanged();
}
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
});
}
progressBar的一系列操作
private void downloadApk() {
String url = "http://down11.zol.com.cn/suyan/lulutong3.6.5g.apk";
String path = Environment.getExternalStorageDirectory().getPath() + "/teme1/myapk.apk";
File file = new File(path);
File parentFile = file.getParentFile();
if (!parentFile.exists()) {
parentFile.mkdir();
}
RequestParams params = new RequestParams(url);
params.setAutoRename(false);
params.setAutoResume(true);
//设置保存路径
params.setSaveFilePath(path);
x.http().get(params, new Callback.ProgressCallback<File>() {
@Override
public void onSuccess(File result) {
Toast.makeText(MainActivity.this, "下载成功", Toast.LENGTH_SHORT).show();
installDownloadApk(result);
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
Toast.makeText(MainActivity.this, "下载失败", Toast.LENGTH_SHORT).show();
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
cancleProgressDialog();
}
@Override
public void onWaiting() {
}
@Override
public void onStarted() {
showProgressDialog();
}
@Override
public void onLoading(long total, long current, boolean isDownloading) {
int progress = (int) (current * 100/ total );
if (progress >= 0 && progress <= 100) {
updataProgressDialog(progress);
}
}
});
}
//安卓开发 apk安装
private void installDownloadApk(File result) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.fromFile(result), "application/vnd.android.package-archive");
startActivity(intent);
}
private void cancleProgressDialog() {
if (progressDialog == null) {
return;
}
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
private void updataProgressDialog(int progress) {
if (progressDialog == null) {
return;
}
progressDialog.setProgress(progress);
}
private void showProgressDialog() {
progressDialog = new ProgressDialog(this);
//设置progressDialog显示样式
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("我正在下载东西");
progressDialog.setTitle("请等待");
progressDialog.setProgress(0);
progressDialog.show();
}
baseadapter适配器 要灵活运用
public class MyBaseAdapter extends BaseAdapter {
private List<Yuekao2Info.ResultBean.DataBean> data;
private Context context;
MyBaseAdapter(List<Yuekao2Info.ResultBean.DataBean> data, Context context){
this.data = data;
this.context =context;
}
public void loadMore(List<Yuekao2Info.ResultBean.DataBean> datas,boolean flag){
for (Yuekao2Info.ResultBean.DataBean bean:datas) {
if (flag){
data.add(0,bean);
}else {
data.add(bean);
}
}
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null){
convertView = View.inflate(context,R.layout.item,null);
holder = new ViewHolder();
holder.tv = (TextView) convertView.findViewById(R.id.tv);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
ImageView iv = (ImageView) convertView.findViewById(R.id.iv);
holder.tv.setText(data.get(position).getTitle());
loderImage(data.get(position).getThumbnail_pic_s(),iv);
return convertView;
}
class ViewHolder{
TextView tv;
ImageView iv;
}
public void loderImage(String url,ImageView imageView){
ImageLoaderConfiguration configuration = ImageLoaderConfiguration
.createDefault(context);
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.mipmap.ic_launcher)
.showImageOnFail(R.mipmap.ic_launcher)
.cacheInMemory(true)
.cacheOnDisk(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
ImageLoader instance = ImageLoader.getInstance();
instance.init(configuration);
instance.displayImage(url, imageView ,options);
//compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
}
}
建一个application,在Androidmanifest清单文件中引用
application里面
x.Ext.init(this);
String path = Environment.getExternalStorageDirectory().getPath()+"/"+"Pictrues";
File file = new File(path);
权限 网络权限 读写权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>