实例介绍
【实例截图】
【核心代码】
package com.example.packagemanagerdemo.activity; import java.io.File; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.List; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Intent; import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.os.AsyncTask; import android.os.Bundle; import android.preference.Preference; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.PopupWindow; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; import com.example.packagemanagerdemo.R; import com.example.packagemanagerdemo.adapter.AppListBaseAdapter; import com.example.packagemanagerdemo.adapter.AppListBaseAdapter2; import com.example.packagemanagerdemo.comparator.AppComparator; import com.example.packagemanagerdemo.domain.AppInfo; import com.example.packagemanagerdemo.util.LogUtil; import com.example.packagemanagerdemo.util.PreferencesUtil; import com.example.packagemanagerdemo.util.Util; /** * //BUG,点击卸载后,没法知道是否卸载成功 */ public class AppListActivity<popupWindowClickFlag> extends Activity implements OnItemClickListener, OnClickListener { private static final String TAG = "AppListActivity"; private int systemCount; private AppListBaseAdapter2 adapter; private ProgressBar progressBar1; private ListView lv; private TextView tv1; private TextView tv2; private TextView tv3; private ImageView iv_nodata; private LayoutInflater mLayoutInflater; private ImageView iv_applist_iv; private PopupWindow popupWindow; private TextView tv1_pw_sort_appname; private TextView tv1_pw_sort_firstInstallTime; private TextView tv1_pw_sort_length; private boolean sortAppNameClickFlag; private boolean sortFirstInstallTimeClickFlag; private boolean sortlengthClickFlag; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setTitle(R.string.MobileApplicationList); setContentView(R.layout.activity_applist); initializeVariables(); findViewById();//找到控件 setOnClickListener();//设置事件 initView();//初始控件状态 getData();//异步加载数据 // lv.setAdapter(adapter); /* 此方式在子线程中也可更新UI,但体验不好。 new Handler().post(new Runnable() { public void run() { List<AppInfo> list = getAppInfo(); progressBar1.setVisibility(View.GONE); adapter = new AppListBaseAdapter(MainActivity.this, list); lv.setAdapter(adapter); lv.setOnItemClickListener(MainActivity.this); tv1.setText("总应用数:" list.size()); tv2.setText("系统应用数:" systemCount); tv3.setText("用户应用数:" (list.size() - systemCount)); } });*/ } private void initializeVariables() { // TODO Auto-generated method stub mLayoutInflater =getLayoutInflater(); //popupWindow } private void findViewById() { // TODO Auto-generated method stub lv = (ListView) AppListActivity.this .findViewById(R.id.lv); tv1 = (TextView) AppListActivity.this .findViewById(R.id.tv1); tv2 = (TextView) AppListActivity.this .findViewById(R.id.tv2); tv3 = (TextView) AppListActivity.this .findViewById(R.id.tv3); iv_applist_iv = (ImageView) AppListActivity.this .findViewById(R.id.iv_applist_iv); progressBar1 = (ProgressBar) AppListActivity.this .findViewById(R.id.progressBar1); iv_nodata = (ImageView) AppListActivity.this .findViewById(R.id.iv_nodata); View contentView=mLayoutInflater.inflate(R.layout.popupwindow_view, null); popupWindow=new PopupWindow(contentView,LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, false);//false表是是否可关闭 false:可关闭 tv1_pw_sort_appname=(TextView)contentView.findViewById(R.id.tv1_pw_sort_appname); tv1_pw_sort_firstInstallTime=(TextView)contentView.findViewById(R.id.tv1_pw_sort_firstInstallTime); tv1_pw_sort_length=(TextView)contentView.findViewById(R.id.tv1_pw_sort_length); } private void setOnClickListener() { lv.setOnItemClickListener(AppListActivity.this); iv_applist_iv.setOnClickListener(this); tv1_pw_sort_appname.setOnClickListener(this); tv1_pw_sort_firstInstallTime.setOnClickListener(this); tv1_pw_sort_length.setOnClickListener(this); } public void onClick(View v) { int sortType; int id=v.getId(); switch (id) { case R.id.iv_applist_iv://弹出PopupWindow菜单 选择应用排序类型 if(popupWindow.isShowing()){ popupWindow.dismiss(); }else{ popupWindow.showAsDropDown(iv_applist_iv,5,-5); } break; case R.id.tv1_pw_sort_appname: List<AppInfo>list=adapter.getList(); sortType=1; PreferencesUtil.putInt(this,PreferencesUtil.sortType, sortType); if(sortAppNameClickFlag){ sortAppNameClickFlag=false; }else{ sortAppNameClickFlag=true; } Collections.sort(list, new AppComparator(sortType,sortAppNameClickFlag)); adapter.setList(list); adapter.notifyDataSetChanged(); break; case R.id.tv1_pw_sort_firstInstallTime: List<AppInfo>list2=adapter.getList(); sortType=2; PreferencesUtil.putInt(this,PreferencesUtil.sortType, sortType); if(sortFirstInstallTimeClickFlag){ sortFirstInstallTimeClickFlag=false; }else{ sortFirstInstallTimeClickFlag=true; } Collections.sort(list2, new AppComparator(sortType,sortFirstInstallTimeClickFlag)); adapter.setList(list2); adapter.notifyDataSetChanged(); break; case R.id.tv1_pw_sort_length: List<AppInfo>list3=adapter.getList(); sortType=3; PreferencesUtil.putInt(this,PreferencesUtil.sortType, sortType); if(sortlengthClickFlag){ sortlengthClickFlag=false; }else{ sortlengthClickFlag=true; } Collections.sort(list3, new AppComparator(sortType,sortlengthClickFlag)); adapter.setList(list3); adapter.notifyDataSetChanged(); break; } } private void initView() { lv.setEmptyView(iv_nodata); lv.addHeaderView(mLayoutInflater.inflate(R.layout.activity_applist_listview_headerview, null)); tv1.setText("总应用:0"); tv2.setText("系统:0"); tv3.setText("用户:0"); } private void getData() { // TODO Auto-generated method stub new MainAsyncTask().execute();//获取数据 } /** * 异步加载类 */ private class MainAsyncTask extends AsyncTask<Void, Void, List<AppInfo>>{ // List<AppInfo>l; protected void onPreExecute() { progressBar1.setVisibility(View.VISIBLE); } protected List<AppInfo> doInBackground(Void... params) { // l=getAppInfo(); // return null; return getAppInfo(); } protected void onPostExecute(List<AppInfo> result) { // if(l!=null){ // //排序类型 // int sortType=PreferencesUtil.getInt(AppListActivity.this, PreferencesUtil.sortType, 0); // switch (sortType) { // case 1: // Collections.sort(l, new AppComparator(sortType,true)); // break; // case 2: // Collections.sort(l, new AppComparator(sortType,true)); // break; // case 3: // Collections.sort(l, new AppComparator(sortType,true)); // break; // } // adapter = new AppListBaseAdapter2(AppListActivity.this, l); //// adapter = new AppListBaseAdapter(AppListActivity.this, result); // lv.setAdapter(adapter); // lv.setOnItemClickListener(AppListActivity.this); // // tv1.setText("总应用:" l.size()); // tv2.setText("系统:" systemCount); // tv3.setText("用户:" (l.size() - systemCount)); // progressBar1.setVisibility(View.GONE); // }else{ // View emptyView = null; // lv.setEmptyView(emptyView); // } if(result!=null){ //排序类型 int sortType=PreferencesUtil.getInt(AppListActivity.this, PreferencesUtil.sortType, 0); switch (sortType) { case 1: Collections.sort(result, new AppComparator(sortType,true)); break; case 2: Collections.sort(result, new AppComparator(sortType,true)); break; case 3: Collections.sort(result, new AppComparator(sortType,true)); break; } adapter = new AppListBaseAdapter2(AppListActivity.this, result); // adapter = new AppListBaseAdapter(AppListActivity.this, result); lv.setAdapter(adapter); lv.setOnItemClickListener(AppListActivity.this); tv1.setText("总应用:" result.size()); tv2.setText("系统:" systemCount); tv3.setText("用户:" (result.size() - systemCount)); progressBar1.setVisibility(View.GONE); }else{ View emptyView = null; lv.setEmptyView(emptyView); } } protected void onProgressUpdate(Void... values) {//更新进度值 } } protected void onResume() { // TODO Auto-generated method stub super.onResume(); } /** * 获取手机应用信息 */ @SuppressLint("NewApi") private List<AppInfo> getAppInfo() { // TODO Auto-generated method stub List<AppInfo> list = new ArrayList<AppInfo>(); PackageManager packageManager = this.getPackageManager();// 1111111111111111111111111 List<PackageInfo> packageInfo_list = null; //packageManager.getInstalledPackages(PackageManager.GET_ACTIVITIES);会报此错误 //03-21 10:57:09.039: E/JavaBinder(8041): !!! FAILED BINDER TRANSACTION !!! // packageManager.getInstalledPackages(PackageManager.GET_ACTIVITIES);// 222222222222222222222 //03-21 10:35:47.759: E/AndroidRuntime(4883): Caused by: java.lang.RuntimeException: Package manager has died //03-21 10:35:47.759: E/AndroidRuntime(4883): Caused by: android.os.TransactionTooLargeException // packageInfo_list = packageManager.getInstalledPackages(PackageManager.GET_ACTIVITIES);// 222222222222222222222 // LogUtil.d(TAG, "GET_ACTIVITIES==>" packageInfo_list.size()); // packageInfo_list = packageManager.getInstalledPackages(PackageManager.GET_CONFIGURATIONS);// 222222222222222222222 LogUtil.d(TAG, "GET_CONFIGURATIONS==>" packageInfo_list.size()); // // // packageInfo_list = packageManager.getInstalledPackages(PackageManager.GET_DISABLED_COMPONENTS);// 222222222222222222222 // LogUtil.d(TAG, "GET_DISABLED_COMPONENTS==>" packageInfo_list.size()); // // // packageInfo_list = packageManager.getInstalledPackages(PackageManager.GET_GIDS);// 222222222222222222222 // LogUtil.d(TAG, "GET_GIDS==>" packageInfo_list.size()); // // // packageInfo_list = packageManager.getInstalledPackages(PackageManager.GET_INSTRUMENTATION);// 222222222222222222222 // LogUtil.d(TAG, "GET_INSTRUMENTATION==>" packageInfo_list.size()); // // // packageInfo_list = packageManager.getInstalledPackages(PackageManager.GET_INTENT_FILTERS);// 222222222222222222222 // LogUtil.d(TAG, "GET_INTENT_FILTERS==>" packageInfo_list.size()); // // // packageInfo_list = packageManager.getInstalledPackages(PackageManager.GET_META_DATA);// 222222222222222222222 // LogUtil.d(TAG, "GET_META_DATA==>" packageInfo_list.size()); // // // packageInfo_list = packageManager.getInstalledPackages(PackageManager.GET_PERMISSIONS);// 222222222222222222222 // LogUtil.d(TAG, "GET_PERMISSIONS==>" packageInfo_list.size()); // // // packageInfo_list = packageManager.getInstalledPackages(PackageManager.GET_PROVIDERS);// 222222222222222222222 // LogUtil.d(TAG, "GET_PROVIDERS==>" packageInfo_list.size()); // // // packageInfo_list = packageManager.getInstalledPackages(PackageManager.GET_RECEIVERS);// 222222222222222222222 // LogUtil.d(TAG, "GET_RECEIVERS==>" packageInfo_list.size()); // // // packageInfo_list = packageManager.getInstalledPackages(PackageManager.GET_RESOLVED_FILTER);// 222222222222222222222 // LogUtil.d(TAG, "GET_RESOLVED_FILTER==>" packageInfo_list.size()); // // // packageInfo_list = packageManager.getInstalledPackages(PackageManager.GET_SERVICES);// 222222222222222222222 // LogUtil.d(TAG, "GET_SERVICES==>" packageInfo_list.size()); // // // packageInfo_list = packageManager.getInstalledPackages(PackageManager.GET_SHARED_LIBRARY_FILES);// 222222222222222222222 // LogUtil.d(TAG, "GET_SHARED_LIBRARY_FILES==>" packageInfo_list.size()); // // // packageInfo_list = packageManager.getInstalledPackages(PackageManager.GET_SIGNATURES);// 222222222222222222222 // LogUtil.d(TAG, "GET_SIGNATURES==>" packageInfo_list.size()); // //0x00002000 // packageInfo_list = packageManager.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);// 222222222222222222222 // LogUtil.d(TAG, "GET_UNINSTALLED_PACKAGES==>" packageInfo_list.size()); // // // packageInfo_list = packageManager.getInstalledPackages(PackageManager.GET_URI_PERMISSION_PATTERNS);// 222222222222222222222 // LogUtil.d(TAG, "GET_URI_PERMISSION_PATTERNS==>" packageInfo_list.size()); // // // packageInfo_list = packageManager.getInstalledPackages(0);// 222222222222222222222 // LogUtil.d(TAG, "0==>" packageInfo_list.size()); // list.contains(mAppInfo); int t = packageInfo_list.size(); systemCount=0; for (int i = 0; i < t; i ) { PackageInfo packageInfo = packageInfo_list.get(i);// 3333333333333333333333 String packageName = packageInfo.packageName;// 应用包名////////////////////////////////////////////////////////// // String[] requestedPermissions = null; // try { // requestedPermissions = packageManager.getPackageInfo( // packageName, packageManager.GET_PERMISSIONS).requestedPermissions; // } catch (NameNotFoundException e) { // e.printStackTrace(); // } // String[] requestedPermissions // =packageInfo.requestedPermissions;//应用权限,可以为空 (此方式获取不到应用的权限) ApplicationInfo applicationInfo = packageInfo.applicationInfo;// 4444444444444444444444 String appName = applicationInfo.loadLabel(packageManager) .toString();// 应用名///////////////////////////////////////////////////////////////////////////////////// boolean isUserApp = false;// 应用是系统应用还是用户应用 if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) > 0) {// 系统程序 // isUserApp = false; systemCount ;// 系统应用计数 } else {// 用户程序 isUserApp = true; } // String versionCode = String.valueOf(packageInfo.versionCode);// 版本号 String versionName = packageInfo.versionName;// 版本名 String sourceDir = applicationInfo.sourceDir;// apk安装包所在路径 long length = new File(sourceDir).length();// 安装包大小////////////////////////////////////////////////////////// // String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") // .format(new Date(file.lastModified()));// 安装日期 // Drawable drawable = applicationInfo.loadIcon(packageManager); // byte[] icon; // if (drawable instanceof StateListDrawable) {// 是StateListDrawable就不能转为byte // icon = ImageTool.drawableToByte(getResources().getDrawable( // R.drawable.ic_launcher));// 使用默认 // } else { // icon = ImageTool.drawableToByte(drawable);// 应用图标byte类型 // } // String firstInstallTime = new SimpleDateFormat( // "yyyy-MM-dd HH:mm:ss").format(new Date( // packageInfo.firstInstallTime)); long firstInstallTime=packageInfo.firstInstallTime;//安装时间 // String lastUpdateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") // .format(new Date(packageInfo.lastUpdateTime)); // String publicSourceDir = applicationInfo.publicSourceDir; // String processName = applicationInfo.processName;// 应用运行的进程的名称 // String largestWidthLimitDp = String // .valueOf(applicationInfo.largestWidthLimitDp);// 最大最小的屏幕宽度 // String compatibleWidthLimitDp = String // .valueOf(applicationInfo.compatibleWidthLimitDp);// 最大最小的屏幕宽度 // String dataDir = applicationInfo.dataDir; // Util.Log(TAG, "appName" appName);//one // Util.Log(TAG, "firstInstallTime" firstInstallTime); // Util.Log(TAG, "lastUpdateTime" lastUpdateTime); // // Util.Log(TAG, "publicSourceDir" publicSourceDir); // Util.Log(TAG, "processName" processName); // Util.Log(TAG, "largestWidthLimitDp" largestWidthLimitDp); // Util.Log(TAG, "compatibleWidthLimitDp" compatibleWidthLimitDp); // Util.Log(TAG, "dataDir" dataDir); // Util.Log(TAG, "packageName" packageName);//two // Util.Log(TAG, "isSystem" isSystem); // Util.Log(TAG, "sourceDir" sourceDir); // Util.Log(TAG, "size" size);//three // Util.Log(TAG, "versionCode" versionCode); // Util.Log(TAG, "versionName" versionName); // Util.Log(TAG, "date" date); // Util.Log(TAG, "drawable" drawable); // Util.Log(TAG, "icon" icon); // if (requestedPermissions != null) { // Util.Log(TAG, "requestedPermissions.toString()" // requestedPermissions.toString()); // for (int j = 0; j < requestedPermissions.length; j ) { // Util.Log(TAG, "requestedPermissions[j]()" // requestedPermissions[j]); // } // } else { // Util.Log(TAG, "requestedPermissions" requestedPermissions); // } // new AppInfo(appName, packageName, isSystem, sourceDir, size, // versionCode, versionName, date, icon); // list.add(new AppInfo(appName, packageName, isSystem, sourceDir, // size, versionCode, versionName, date, icon)); // list.add(new AppInfo(appName, packageName, isSystem, sourceDir, // size, versionCode, versionName, date, // icon,requestedPermissions)); if(appName.equals("设置")){ Log.i("wujiaming", packageName);//com.android.settings } list.add(new AppInfo(appName, packageName, versionName, length, firstInstallTime, isUserApp)); } return list; } public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub AppInfo appInfo = this.adapter.getItem(arg2); Util.Log(TAG, "appInfo.toString()==>" appInfo.toString()); // Util.Log(TAG, "appInfo.toString()==>" appInfo.getIcon().getClass().getName().toString()); Intent intent = new Intent(); intent.setClass(this, AppInfoActivity.class); Bundle extras = new Bundle(); extras.putSerializable("appInfo", appInfo); intent.putExtras(extras); int requestCode=0x01; this.startActivityForResult(intent, requestCode); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if(requestCode==0x01){ // if(resultCode==0){ getData(); // this.adapter.setAppInfos(getData()); // //通知ListView数据发生了变化 // this.adapter.notifyDataSetChanged();//BUG,点击卸载后,没法知道是否卸载成功 // } } } }
标签: Android
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论