了解应用管理器的项目概况和功能需求
(从数据开始做)
掌握PackageManager的用法
掌握PackageInfo的常用属性
掌握Application的常用属性和方法
学会实体类的编写(工具类)
完成项目的创建和数据测试
版本名为字符串,版本号为数字.
开发实体类:1.在原包下New一个新包entity
2.在entity下New一个Java Class -- AppInfo
/**
* Created by Administrator on 2018/8/14.
* 实体类:应用的信息
*/
public class AppInfo {
/** 包名 */
public String packageName;
/** 版本名 */
public String versionName;
/** 版本号 */
public int versionCode;
/** 第一次安装时间 */
public long insTime;
/** 更新时间 */
public long updTime;
/** 程序名 */
public String appName;
/** 图标 */
public Drawable icon;
/** 字节大小 */
public long byteSize;
/** 大小 */
public String size;
@Override //Alt+Insert选择toString()
public String toString() {
return "\nAppInfo{" +
"packageName='" + packageName + '\'' +
", versionName='" + versionName + '\'' +
", versionCode=" + versionCode +
", insTime=" + insTime +
", updTime=" + updTime +
", appName='" + appName + '\'' +
", icon=" + icon +
", byteSize=" + byteSize +
", size='" + size + '\'' +
'}';
}
}
建工具包(放工具类:跟整个项目,视图,前台没有太多联系可以独立出来的方法进行整合) : 1.在原包下New一个新包util
2.在util下New一个Java Class -- Utils
/**
* Created by Administrator on 2018/8/14.
* 工具类
*/
public class Utils {
public static List getAppList(Context context){
// 返回值集合
List list = new ArrayList();
// 实例化包管理器
PackageManager pm = context.getPackageManager();
// 获取所有已经安装的应用信息
List pList = pm.getInstalledPackages(0); //0为不加任何过滤条件.放回全部的安装信息
// 遍历集合
for(int i = 0; i
Java代码
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//用集合接收传回的返回值.获取数据
List list = Utils.getAppList(this);
Log.i("srq","list="+list.toString());
}
}
转换时间
Utils类中加
public static String getTime(long millis){ //将毫秒数转化为日期格式的工具方法
Date date = new Date(millis); //将毫秒数转化为日期对象
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(date);
}
改动AppInfo中
@Override
public String toString() {
return "\nAppInfo{" +
"packageName='" + packageName + '\'' +
", versionName='" + versionName + '\'' +
", versionCode=" + versionCode +
", insTime=" + Utils.getTime(insTime) +
", updTime=" + Utils.getTime(updTime) +
", appName='" + appName + '\'' +
", icon=" + icon +
", byteSize=" + byteSize +
", size='" + size + '\'' +
'}';
}
1.在原包下New一个新包adapter
2.在adapter下New一个Java Class -- MyAdapter
public class MyAdapter extends BaseAdapter {
List list;
LayoutInflater inflater;
public MyAdapter(Context context) {
this.inflater = LayoutInflater.from(context);
}
public void setList(List list) {
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null) {
convertView = inflater.inflate(R.layout.item, null);
holder = new ViewHolder();
holder.logo = (ImageView) convertView.findViewById(R.id.logo);
holder.title = (TextView) convertView.findViewById(R.id.title);
holder.version = (TextView) convertView.findViewById(R.id.version);
holder.size = (TextView) convertView.findViewById(R.id.size);
holder.btn = (Button) convertView.findViewById(R.id.btn);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
AppInfo app = list.get(position); //list得到的每一个项就是实体类对象
holder.logo.setImageDrawable(app.icon);
holder.title.setText(app.appName);
holder.version.setText(app.versionName);
holder.size.setText(app.size + "M");
return convertView;
}
public class ViewHolder{
ImageView logo;
TextView title;
TextView version;
TextView size;
Button btn;
}
}
colors.xml
#00000000
#ff0febff
drawable -- item_selector.xml
drawable -- btn_selector.xml
item.xml
main.xml
Java代码
public class MainActivity extends Activity {
ListView lv;
List list;
MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//拿到ListView
lv = (ListView) findViewById(R.id.lv_main);
//数据源
list = Utils.getAppList(this);
//适配器
adapter = new MyAdapter(this);
adapter.setList(list);
//关联
lv.setAdapter(adapter);
Log.i("srq","list="+list.toString());
}
}
改动MyAdapter(测试用)
AppInfo app = list.get(position);
holder.logo.setImageDrawable(app.icon);
holder.title.setText(app.appName);
holder.version.setText(app.versionName + " "+ Utils.getTime(app.insTime));
holder.size.setText(app.size + "M" + " "+Utils.getTime(app.updTime));