软件管理器类似于Windows下控制面板中的程序卸载功能,它可以实现对本机中的软件进行集中管理。通过软件管理器,我们可以实现查看已安装软件的详细信息、启动和卸载软件等功能。
从用户实际使用需求出发,基于Android平台的软件管理器,主要实现的功能有:
1) 以列表视图或网格视图显示用户程序或者所有程序;
2) 启动程序
3) 查看程序详细信息
4) 卸载程序
/**
* ItemClick的处理
*/
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
final PackageInfo tempPkInfo = showPackageInfo.get(position);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("选项");
builder.setItems(R.array.choice, new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
switch(which){
case 0:
//start an app
String packageName = tempPkInfo.packageName;
ActivityInfo activityInfo = tempPkInfo.activities[0];
if(activityInfo == null){
Toast.makeText(MainActivity.this, "程序出错!", Toast.LENGTH_SHORT).show();
return;
}
String activityName = activityInfo.name;
Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName,activityName));
startActivity(intent);
break;
case 1:
showAppDetail(tempPkInfo);
break;
case 2:
Uri packagUri = Uri.parse("package:" tempPkInfo.packageName);
Intent uninstallIntent = new Intent();
uninstallIntent.setAction(Intent.ACTION_DELETE);
uninstallIntent.setData(packagUri);
//StartActivityForResult
startActivityForResult(uninstallIntent , 0);
//Should OVERRIDE onActivityResult()
break;
}
}
});
builder.setNegativeButton("取消", null);
builder.create();
builder.show();
}
/**
* Activity回传处理
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
packageInfo = getPackageManager().getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES | PackageManager.GET_ACTIVITIES);
userPackageInfo = new ArrayList<PackageInfo>();
for (int i =0;i<packageInfo.size();i ){
PackageInfo temp = packageInfo.get(i);
ApplicationInfo appInfo = temp.applicationInfo;
boolean flag = false;
//用户安装的程序
if((appInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0){
flag = true;
}
else if((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) ==0){
flag = true;
}
if(flag){
userPackageInfo.add(temp);
}
}
if(allApplication){
showPackageInfo = packageInfo;
}
else{
showPackageInfo = userPackageInfo;
}
gv.setAdapter(new GridViewAdapter(MainActivity.this,showPackageInfo));
lv.setAdapter(new ListViewAdapter(MainActivity.this,showPackageInfo));
}
/**
*显示程序的详细详细
*/
private void showAppDetail(PackageInfo packageInfo) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("程序详细详细");
StringBuffer messageDetail = new StringBuffer();
messageDetail.append("程序名称: " packageInfo.applicationInfo.loadLabel(getPackageManager()));
messageDetail.append("\n包名: " packageInfo.packageName);
messageDetail.append("\n版本: " packageInfo.versionName);
builder.setMessage(messageDetail);
builder.setIcon(packageInfo.applicationInfo.loadIcon(getPackageManager()));
builder.setPositiveButton("确定", null);
builder.create().show();
}
评论