## 经常使用号码查询 ##android
- ExpandableListView 是一个能够展开的listview 分组显示数据库
- 填充数据要 用ExpandableListAdapter 缓存
- 在splash页面拷贝经常使用号码数据库
-
/**
* 拷贝经常使用号码数据库
*/
private void copyCommNum() {
// 获取AssetManager对象
AssetManager assets = getAssets();
InputStream addrIs = null;
FileOutputStream fos = null;
try {
// 获取assets文件夹里的文件流
addrIs = assets.open("commonnum.db");
File targetFile = new File(getFilesDir(), "commonnum.db");
fos = new FileOutputStream(targetFile);
int len = 0;
byte[] buffer = new byte[1024];
while ((len = addrIs.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
GZipUtils.close(addrIs);
GZipUtils.close(fos);
}
}ide
- 拷贝数据库到工程 在启动页面 拷贝到 files缓存文件夹里面 而后写dao
> 填充数据布局
private class NumAdapter extends BaseExpandableListAdapter {this
// 组的数量
@Override
public int getGroupCount() {3d
return CommonNumDao.getGroupCount(getApplicationContext());
}orm
// 每组里孩子的数量
@Override
public int getChildrenCount(int groupPosition) {
return CommonNumDao.getChildCount(getApplicationContext(), groupPosition);
}xml
// 供外部使用 不用就不写
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return 0;
}对象
// 是否拥有一个稳定的id 用不到 默认
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
TextView tvGroup = new TextView(getApplicationContext());
String groupName = CommonNumDao.getGroupName(getApplicationContext(),
groupPosition);
tvGroup.setText(groupName);
tvGroup.setTextSize(18);
tvGroup.setTextColor(Color.BLACK);
tvGroup.setBackgroundColor(Color.parseColor("#66000000"));
tvGroup.setPadding(8, 8, 8, 8);
return tvGroup;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
TextView tvChild = new TextView(getApplicationContext());
String[] child = CommonNumDao.getChildName(getApplicationContext(),
groupPosition, childPosition);
tvChild.setText(child[0] + "\n" + child[1]);
tvChild.setTextSize(16);
tvChild.setTextColor(Color.BLACK);
tvChild.setPadding(8, 8, 8, 8);
return tvChild;
}
//孩子是否可点击
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
// 供外部使用 不用就去写
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return null;
}
// 供外部使用 不用就去写
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return null;
}
// 供外部使用 不用就去写
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return 0;
}
}
- 设置组的点击事件
// 组的点击事件
ElvNum.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// 判断是否展开
if (ElvNum.isGroupExpanded(groupPosition)) {
// 已经展开去关闭
ElvNum.collapseGroup(groupPosition);
} else {
// 已经关闭去 展开
ElvNum.collapseGroup(mOpenGroup);// 把以前展开的关闭掉
ElvNum.expandGroup(groupPosition, true);// 展开一个组
ElvNum.setSelection(groupPosition);// 把当前的展开的组置顶
mOpenGroup = groupPosition;// 记住每次展开的position
}
return true;
}
});
- 孩子的点击事件 去拨号
// 孩子的点击事件
ElvNum.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
String[] child = CommonNumDao.getChildName(getApplicationContext(),
groupPosition, childPosition);
String num = child[1];
Intent intent = new Intent();
intent.setAction(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:"+num));
startActivity(intent);
return true;
}
});
## 软件管理 ##
- 软件管理页面顶部实现=
- 这个内存信息, 这个指的是内部存储, 不是运行时内存.
- 写界面, 怎样设置进度条为水平样式? 不用记, 拖过来就行.
- 如何更改横向progressbar的 默认背景和进度样式 去系统样式文件里找到默认ProgressBar样式
发现须要修改android:progressDrawable这个属性
- 给 ProgressBar 设置 android:progressDrawable="@drawable/progress_horizontal" 属性便可,对应的 drawable文件:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 背景 -->
<item android:id="@android:id/background">
<shape>
<solid android:color="#22000000" />
</shape>
</item>
<!-- 副进度条 -->
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<solid android:color="#22ff0000" />
</shape>
</clip>
</item>
<!-- 主进度条 -->
<item android:id="@android:id/progress">
<clip>
<shape>
<solid android:color="#55ff0000" />
</shape>
</clip>
</item>
</layer-list>
- 针对页面上面这块, 写一个自定义组合控件, 后面进程管理里还要用到.这里不须要自定义属性 由于数据须要在代码里动态设置,不能在
布局文件里写死.
public class ProgressDescView extends LinearLayout {
private TextView mTvTitle;
private TextView mTvLeft;
private TextView mTvRight;
private ProgressBar mPbProgress;
public ProgressDescView(Context context) {
this(context, null);
}
public ProgressDescView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ProgressDescView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// 把填充出来的View添加到本身里面
View.inflate(context, R.layout.view_progress_des, this);
// 初始化view
mTvTitle = (TextView) findViewById(R.id.tv_pdv_title);
mTvLeft = (TextView) findViewById(R.id.tv_pdv_left);
mTvRight = (TextView) findViewById(R.id.tv_pdv_right);
mPbProgress = (ProgressBar) findViewById(R.id.pb_pdv);
mPbProgress.setMax(100);
}
public void setTitle(String text) {
mTvTitle.setText(text);
}
public void setTextLeft(String text) {
mTvLeft.setText(text);
}
public void setTextRight(String text) {
mTvRight.setText(text);
}
/**
* 设置进度, 最大值为100
* @param progress
*/
public void setProgress(int progress) {
mPbProgress.setProgress(progress);
}
}
- ProgressDescView总体的布局文件:
<?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"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_pdv_title"
style="@style/TextContentStyle"
android:layout_width="50dp"
android:layout_marginTop="0dp"
android:text="标题:"
android:textSize="15sp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ProgressBar
android:id="@+id/pb_pdv"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="24dp"
android:layout_centerVertical="true"
android:progressDrawable="@drawable/progress_horizontal"/>
<TextView
android:id="@+id/tv_pdv_left"
style="@style/TextContentStyle"
android:layout_centerVertical="true"
android:layout_marginLeft="0dp"
android:text="xxx已用"
android:textSize="13sp"/>
<TextView
android:id="@+id/tv_pdv_right"
style="@style/TextContentStyle"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="0dp"
android:layout_marginRight="3dp"
android:text="xxx可用"
android:textSize="13sp"/>
</RelativeLayout>
</LinearLayout>
- 文件大小的计算
android.text.Formatter 类能够格式化文件大小
// 内部存储, 其实就是data目录的容量
File dataDirectory = Environment.getDataDirectory(); // 所有 long totalSpace = dataFile.getTotalSpace(); // 可用 long usableSpace = dataFile.getUsableSpace(); // 已用 long usedSpace = totalSpace - usableSpace; mPdvRom.setTitle("内存: "); mPdvRom.setTextLeft(Formatter.formatFileSize(getApplicationContext(), usedSpace) + "已用"); mPdvRom.setTextRight(Formatter.formatFileSize(getApplicationContext(), freeSpace) + "可用"); mPdvRom.setProgress((int) (usedSpace * 100f / totalSpace + 0.5f)); // 四舍五入 // SD卡 File sdDirectory = Environment.getExternalStorageDirectory(); // 总空间 long sdTotalSpace = sdDirectory.getTotalSpace(); // 剩余空间 long sdFreeSpace = sdDirectory.getFreeSpace(); long sdUsedSpace = totalSpace - freeSpace; mPdvSD.setTitle("SD卡: "); mPdvSD.setTextLeft(Formatter.formatFileSize(getApplicationContext(), sdUsedSpace) + "已用"); mPdvSD.setTextRight(Formatter.formatFileSize(getApplicationContext(), sdFreeSpace) + "可用"); mPdvSD.setProgress((int) (sdUsedSpace * 100f / sdTotalSpace+ 0.5f));