package bw.com.bw_day01_xutils;
import android.app.Application;
import org.xutils.x;
/**
* 3, 定义Application 的子类, 初始化xUtils
*/
public class MyApp extends Application{
@Override
public void onCreate() {
super.onCreate();
//初始化
x.Ext.init(this);
x.Ext.setDebug(true);//设置显示日志
}
}
//基本注解使用
package bw.com.bw_day01_xutils;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;
import bw.com.bw_day01_xutils.demo02.GetDataActivity;
import bw.com.bw_day01_xutils.demo03.DownLoadApkActivity;
import bw.com.bw_day01_xutils.demo04.DbActivity;
//TODO 2, 利用注解 加载页面
@ContentView(value = R.layout.activity_main)
public class MainActivity extends AppCompatActivity {
//TODO 3, 控件的初始化
@ViewInject(value = R.id.tv_id)
private TextView mTv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);//加载页面布局
//TODO ViewUtils 的使用
//TODO 1, 需要引入注解
x.view().inject(this);
}
//TODO 4, 事件的注解
/**注意:
* 1, 需要@Event 实现事件的注解
* 2, value , 必须是一个int[] , 可以设置多个控件的ID
* 3, type , 类型-- 要处理的是哪个事件 -- 事件类.class
* 4, 方法的修饰符必须是私有的
* 5, 方法的名称, 必须要和type 指定的事件监听器中, 回调的方法名一致
*/
@Event(value = {R.id.tv_id,R.id.but_01,R.id.but_02,R.id.but_03,R.id.but_04},type = View.OnClickListener.class)
private void onClick(View view)
{
switch (view.getId())
{
case R.id.tv_id:
mTv.setText("xUtils 的注解功能就写完了........");
break;
case R.id.but_01:
Toast.makeText(this, "你点击了按钮", Toast.LENGTH_SHORT).show();
break;
case R.id.but_02:
startActivity(new Intent(this, GetDataActivity.class));
break;
case R.id.but_03:
startActivity(new Intent(this, DownLoadApkActivity.class));
break;
case R.id.but_04:
startActivity(new Intent(this, DbActivity.class));
break;
}
}
}
//断电续传
package bw.com.bw_day01_xutils.demo03;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ProgressBar;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;
import java.io.File;
import bw.com.bw_day01_xutils.R;
@ContentView(value = R.layout.activity_down_load_apk)
public class DownLoadApkActivity extends AppCompatActivity {
//初始化控件
@ViewInject(value = R.id.pb_id)
private ProgressBar mProgressBar;
//下载的地址:
private String apkUrl = "http://218.244.149.129:9010/download.php?apkid=12";
//sd卡中存储的名称
private String apkName = "hello.apk";
//TODO 2, 断点续传中, 设置可以取消的任务
private Callback.Cancelable cancelable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//引入注解
x.view().inject(this);
}
//点击按钮, 下载apk, 存入sd中 -- 路径, 最后实现自动安装的功能
@Event(value = {R.id.down_load_id,R.id.stop_load_id})
private void onClick(View view)
{
switch (view.getId())
{
case R.id.down_load_id: // 点击下载
//请求参数
RequestParams params = new RequestParams(apkUrl);
//TODO 1 , 设置支持断点续传
params.setAutoResume(true);//设置支持断点续传
//存入sd中 -- 指定路径 getExternalCacheDir() 混存目录
String filePath = getExternalCacheDir() + File.separator + apkName;
//指定路径, 到网络请求参数中
params.setSaveFilePath(filePath);
//TODO 3, 断点续传中, 获取可以取消的任务
cancelable = x.http().get(params, new Callback.ProgressCallback() {
@Override
public void onLoading(long total, long current, boolean isDownLoading) {
// todo 正在加载的方法 -- 实时更新进度
int progress = (int)(current *100/total);//得到当前加载的进度
mProgressBar.setProgress(progress);
}
@Override
public void onSuccess(File file) {
// todo 加载成功 -- 最后的操作 -- 一定是下载成功了, 所有只需要安装即可
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
DownLoadApkActivity.this.startActivity(intent);
}
@Override
public void onError(Throwable throwable, boolean b) {
// todo 加载失败
}
@Override
public void onCancelled(CancelledException e) {
// todo 取消方法
}
@Override
public void onFinished() {
// todo 完成的方法
}
@Override
public void onWaiting() {
// todo 等待的方法
}
@Override
public void onStarted() {
// todo 开始加载的方法
}
});
break;
case R.id.stop_load_id: //点击暂停
//TODO 4, 暂停下载
cancelable.cancel();
break;
}
}
}
//数据库
package bw.com.bw_day01_xutils.demo04;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.view.annotation.ContentView;
import org.xutils.x;
import java.util.List;
import bw.com.bw_day01_xutils.R;
@ContentView(value = R.layout.activity_db)
public class DbActivity extends AppCompatActivity {
private DbManager dbManager;//数据库的管理器对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//引入注解
x.view().inject(this);
//1, 创建数据库 -- 实例化数据库的配置信息
DbManager.DaoConfig daoConfig = new DbManager.DaoConfig();
daoConfig.setDbName("bw_1511C.db");//数据库的名称
daoConfig.setDbVersion(1);//设置版本号
daoConfig.setDbUpgradeListener(new DbManager.DbUpgradeListener() {
@Override
public void onUpgrade(DbManager dbManager, int i, int i1) {
}
});//数据库版本更新的回调方法
dbManager = x.getDb(daoConfig);
//2, 表 -- 实体类 (改良) --TODO 切记: 必须要包含一个无参的构造方法
//3, 数据--内容 -- 添加数据
try {
dbManager.save(new User("张三",20));
dbManager.save(new User("李四",25));
dbManager.save(new User("王五",30));
} catch (DbException e) {
e.printStackTrace();
}
//5, 修改数据
//根据id 得到要修改的类
try {
User user = dbManager.findById(User.class,1);
user.setName("宋小宝");
user.setAge(40);
dbManager.update(user,"name","age");
} catch (DbException e) {
e.printStackTrace();
}
//6,删除数据
try {
//根据id 得到要删除的类
User user = dbManager.findById(User.class,2);
dbManager.delete(user);
} catch (DbException e) {
e.printStackTrace();
}
//4, 查看数据
try {
//查询所有数据
List data = dbManager.findAll(User.class);
//查询指定id的数据 dbManager.findById(User.class,2);
for(User user:data)
{
Log.e("TAG","user -- " + user);
}
} catch (DbException e) {
e.printStackTrace();
}
}
}
package bw.com.bw_day01_xutils.demo04;
import org.xutils.db.annotation.Column;
import org.xutils.db.annotation.Table;
/**
* Created by Administrator on 2018/1/23.
*/
//指定表
@Table(name = "user")
public class User {
//指定字段
//name 字段名称,isId = true 是否为主键 ,autoGen = true 是否自动增长
@Column(name = "_id",isId = true,autoGen = true)
private int id;
@Column(name = "name")
private String name;
@Column(name = "age")
private int age;
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
//图片
package bw.com.bw_day01_xutils.demo02;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import org.xutils.image.ImageOptions;
import org.xutils.x;
import java.util.List;
import bw.com.bw_day01_xutils.R;
/**
* Created by Administrator on 2018/1/23.
*/
public class MyAdapter extends BaseAdapter {
private Context context;
private List data;
public MyAdapter(Context context,List data)
{
this.context = context;
this.data = data;
}
@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 viewHolder;
if(convertView==null)
{
convertView = LayoutInflater.from(context).inflate(R.layout.item_lv,parent,false);
viewHolder = new ViewHolder();
viewHolder.tv = (TextView) convertView.findViewById(R.id.tv_id);
viewHolder.iv = (ImageView) convertView.findViewById(R.id.iv_id);
convertView.setTag(viewHolder);
}else
{
viewHolder = (ViewHolder) convertView.getTag();
}
//赋值 -- 设置标题
viewHolder.tv.setText(data.get(position).getTitle());
// 加载图片
//图片的选项
ImageOptions options = new ImageOptions.Builder()
.setFailureDrawableId(R.mipmap.ic_launcher)//加载失败显示的图片
.setLoadingDrawableId(R.mipmap.ic_launcher)//正在加载时显示的图片
.setUseMemCache(true)//设置缓存
.setCircular(true)//设置圆形图片
.setIgnoreGif(false)//设置是否忽略gif动画
.build();
x.image().bind(viewHolder.iv,data.get(position).getImage(),options);
return convertView;
}
class ViewHolder
{
ImageView iv;
TextView tv;
}
}