## 經常使用號碼查詢 ##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));