手机卫士第七天
1. 软件管理的开发
a) 第一知识点如何得到SD卡、手机内存的大小和可用空间,所用到的API:
i. StatFs文件系统状态的类
//path 文件系统的目录地址
public long getAvailableSpace(String path) {
StatFs statFs = new StatFs(path);
//得到单个设备块的大小
int blockSize = statFs.getBlockSize();
//得到可用数据块的总数
int blockCount = statFs.getAvailableBlocks();
return blockSize * blockCount;
}
ii. 使用Formatter格式化文本和得到内存和外存的绝对路径
tv_rom_aviliable_space.setText("手机:"
+ Formatter.formatFileSize(this, getAvailableSpace(Environment
.getDataDirectory().getAbsolutePath())));
tv_sdcard_aviliable_space.setText("手机:"
+ Formatter.formatFileSize(this, getAvailableSpace(Environment
.getExternalStorageDirectory().getAbsolutePath())));
iii. 得到系统中所用应用的信息
public static List<AppInfo> listAppINfos(Context context) {
PackageManager pm = context.getPackageManager();
List<PackageInfo> infos = pm.getInstalledPackages(0);
AppInfo appInfo = null;
List<AppInfo> appInfos = new ArrayList<AppInfo>();
for (PackageInfo info : infos) {
appInfo = new AppInfo();
appInfo.setIcon(info.applicationInfo.loadIcon(pm));
int flags = info.applicationInfo.flags;
// 进行与操作,若果存在该权限则为1 否则为0
appInfo.setInRom((flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) == 0);
appInfo.setUserApp((flags & ApplicationInfo.FLAG_SYSTEM) == 0);
appInfo.setName(info.applicationInfo.loadLabel(pm).toString());
appInfo.setPackageName(info.packageName);
appInfos.add(appInfo);
}
return appInfos;
}
iv. 知识点:线程在完成从数据库、本地文件或者网络中获取数据的操作后需要通知主线程更新界面显示就用到了Handler对象,但是如果只是更新很小的界面的时候显得代码有点松散。所以子线程中可以使用runOnUiThread()方法代替Handler直接通知主线程更新UI
private void fillData() {
new Thread(new Runnable() {
@Override
public void run() {
appInfos = AppInfoManager.listAppINfos(AppManagerActivity.this);
// 对以上集合进行分组
userApp = new ArrayList<AppInfo>();
sysApp = new ArrayList<AppInfo>();
for (AppInfo info : appInfos) {
if (info.isUserApp()) {
userApp.add(info);
} else {
sysApp.add(info);
}
}
// 数据完毕后再主线程中填充数据
runOnUiThread(new Runnable() {
@Override
public void run() {
appListAdapter = new AppListAdapter();
lv_list_allapp.setAdapter(appListAdapter);
ll_loading_in_appmanager.setVisibility(View.INVISIBLE);
}
});
}
}).start();
}
v. 安卓2.3之后在清单文件的Manife节点可以添加属性installLocation建议程序的安装位置
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.phoneguard"
android:versionCode="2"
android:installLocation="[auto]|[internalOnly]|[preferExternal]”
android:versionName="1.1" >
vi. LIstView的复杂界面显示。例如根据对象集合对象的某个属性进行分组显示。(适配器如下)
class AppListAdapter extends BaseAdapter {
@Override
public int getCount() {
// 新增加两个分组的item
return appInfos.size() + 2;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (position == 0) {
TextView tv = new TextView(AppManagerActivity.this);
tv.setBackgroundColor(Color.BLACK);
tv.setTextColor(Color.WHITE);
tv.setTextSize(16);
tv.setText("用户应用:共" + userApp.size() + "个");
tv.setClickable(false);
return tv;
}
if (position == userApp.size() + 1) {
TextView tv = new TextView(AppManagerActivity.this);
tv.setBackgroundColor(Color.BLACK);
tv.setTextColor(Color.WHITE);
tv.setTextSize(16);
tv.setText("系统应用:共" + sysApp.size() + "个");
tv.setClickable(false);
return tv;
}
ViewHolder holder = null;
if (convertView != null && convertView instanceof RelativeLayout) {
holder = (ViewHolder) convertView.getTag();
} else {
holder = new ViewHolder();
convertView = inflater
.inflate(R.layout.appinfo_item_show, null);
holder.app_icon = (ImageView) convertView
.findViewById(R.id.iv_app_icon_item_appinfo);
holder.app_location = (TextView) convertView
.findViewById(R.id.tv_app_location_item_appinfo);
holder.app_remove = (ImageView) convertView
.findViewById(R.id.iv_app_remove_item_appinfo);
holder.app_space = (TextView) convertView
.findViewById(R.id.tv_app_space_item_appinfo);
holder.app_name = (TextView) convertView
.findViewById(R.id.tv_app_name_item_appinfo);
convertView.setTag(holder);
}
AppInfo info = null;
if (position > 0 && position < userApp.size() + 1) {
info = userApp.get(position - 1);
} else {
info = sysApp.get(position - userApp.size() - 2);
}
holder.app_icon.setImageDrawable(info.getIcon());
holder.app_location.setText(info.isInRom() == true ? "手机内存"
: "扩展内存");
holder.app_remove.setVisibility(View.INVISIBLE);
holder.app_name.setText(info.getName());
holder.app_space.setText("所占内存");
return convertView;
}
class ViewHolder {
private ImageView app_icon;
private ImageView app_remove;
private TextView app_location;
private TextView app_space;
private TextView app_name;
}
}
vii. PopupWindow泡泡弹出窗和动画的应用
// 如果页面上存在气泡窗口,就关闭它
dismissPopup();
View popupView = inflater.inflate(R.layout.popup_app_manager,
null);
int[] location = new int[2];
view.getLocationOnScreen(location);
popupWindow = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
false);
// 为了应用动画,须为泡泡窗口设置背景(透明也可)
popupWindow.setBackgroundDrawable(new ColorDrawable(
Color.TRANSPARENT));
// 需要将dp转换成px显示
popupWindow.showAtLocation(view, Gravity.TOP | Gravity.LEFT,
location[0], location[1]);
if (position < userApp.size() + 1) {
appInfo = userApp.get(position - 1);
} else {
appInfo = sysApp.get(position - userApp.size() - 2);
}
AlphaAnimation alphaAnimation = new AlphaAnimation(0.2f, 1.0f);
alphaAnimation.setDuration(300);
ScaleAnimation scaleAnimation = new ScaleAnimation(0.3f, 1.0f,
0.3f, 1.0f, Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(300);
AnimationSet set = new AnimationSet(false);
set.addAnimation(scaleAnimation);
set.addAnimation(alphaAnimation);
popupView.startAnimation(set);
viii. .9patch图形的用途
可以在安卓或者IOS开发中制定图片拉伸时拉伸制定的部分横切的线条
ix. 启动一个指定报名的应用程序(APP)
/**
* 运行指定包名的应用程序
*
* @param packageName
*/
private void launchApplictation(String packageName) {
PackageManager pm = getPackageManager();
Intent intent = pm.getLaunchIntentForPackage(packageName);
if (intent == null) {
Toast.makeText(this, "无法启动,该应用是一个服务进程...", 0).show();
return;
}
startActivity(intent);
}
x. 卸载一个制定包名的应用程序(APP)
/**
* 卸载一个制定报名的应用程序
*
* @param packageName
*/
private void uninstallApplictation(String packageName) {
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.setAction("android.intent.action.DELETE");
intent.addCategory("android.intent.category.DEFAULT");
intent.setData(Uri.parse("package:" + packageName));
startActivityForResult(intent, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
fillData();
super.onActivityResult(requestCode, resultCode, data);
}
xi. 分享一段数据
/**
* 分享一个应用程序名称文本
*/
private void shareApplication() {
// Intent { act=android.intent.action.SEND typ=text/plain flg=0x3000000
// cmp=com.android.mms/.ui.ComposeMessageActivity (has extras) } from
// pid 256
Intent intent = new Intent();
intent.setAction("android.intent.action.SEND");
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "推荐您使用一款软件,名称叫:" + appInfo.getName());
startActivity(intent);
}