首先看一下界面:

AppManagerActivity .java
//软件管理
public class AppManagerActivity extends Activity implements View.OnClickListener{List<AppInfo> appinfos;ListView lv;private List<AppInfo> userAppInfos;private List<AppInfo> systemAppInfos;private TextView tv_app;private PopupWindow popupWindow;private AppInfo clickAppInfo;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);        initUI();initData();                }@Overridepublic void onClick(View v) {switch (v.getId()) {//分享case R.id.ll_share:Intent share_localIntent = new Intent("android.intent.action.SEND");share_localIntent.setType("text/plain");share_localIntent.putExtra("android.intent.extra.SUBJECT", "f分享");share_localIntent.putExtra("android.intent.extra.TEXT","Hi!推荐您使用软件:" + clickAppInfo.getApkname()+"下载地址:"+"https://play.google.com/store/apps/details?id="+clickAppInfo.getApkPackageName());this.startActivity(Intent.createChooser(share_localIntent, "分享"));popupWindowDismiss();break;//运行case R.id.ll_start:Intent start_localIntent = this.getPackageManager().getLaunchIntentForPackage(clickAppInfo.getApkPackageName());this.startActivity(start_localIntent);popupWindowDismiss();break;//卸载case R.id.ll_uninstall:Intent uninstall_localIntent = new Intent("android.intent.action.DELETE", Uri.parse("package:" + clickAppInfo.getApkPackageName()));startActivity(uninstall_localIntent);popupWindowDismiss();break;//详情case R.id.ll_detail:Intent detail_intent = new Intent();detail_intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");detail_intent.addCategory(Intent.CATEGORY_DEFAULT);detail_intent.setData(Uri.parse("package:" + clickAppInfo.getApkPackageName()));startActivity(detail_intent);break;}}private class AppManagerAdapter extends BaseAdapter{@Overridepublic int getCount() {// TODO Auto-generated method stubreturn userAppInfos.size() + 1 + systemAppInfos.size() + 1;}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubif (position == 0) {return null;} else if (position == userAppInfos.size() + 1) {return null;}AppInfo appInfo;if (position < userAppInfos.size() + 1) {//把多出来的特殊的条目减掉appInfo = userAppInfos.get(position - 1);} else {int location = userAppInfos.size() + 2;appInfo = systemAppInfos.get(position - location);}return appInfo;}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {                 //特殊条目的处理//如果当前的position等于0 表示应用程序if (position == 0) {TextView textView = new TextView(AppManagerActivity.this);textView.setTextColor(Color.WHITE);textView.setBackgroundColor(Color.GRAY);textView.setText("用户程序(" + userAppInfos.size() + ")");return textView;//表示系统程序} else if (position == userAppInfos.size() + 1) {TextView textView = new TextView(AppManagerActivity.this);textView.setTextColor(Color.WHITE);textView.setBackgroundColor(Color.GRAY);textView.setText("系统程序(" + systemAppInfos.size() + ")");return textView;}AppInfo appInfo;if (position < userAppInfos.size() + 1) {//把多出来的特殊的条目减掉appInfo = userAppInfos.get(position - 1);} else {int location = userAppInfos.size() + 2;appInfo = systemAppInfos.get(position - location);}View view = null;ViewHolder holder;if (convertView != null && convertView instanceof LinearLayout) {view = convertView;holder = (ViewHolder) view.getTag();} else {view = View.inflate(AppManagerActivity.this, R.layout.item_app_manager, null);tv_app = (TextView) findViewById(R.id.tv_app);holder = new ViewHolder();holder.iv_icon = (ImageView) view.findViewById(R.id.iv_icon);holder.tv_apk_size = (TextView) view.findViewById(R.id.tv_size);holder.tv_location = (TextView) view.findViewById(R.id.tv_rom);holder.tv_name = (TextView) view.findViewById(R.id.tv_name);view.setTag(holder);}holder.iv_icon.setImageDrawable(appInfo.getIcon());holder.tv_apk_size.setText(Formatter.formatFileSize(AppManagerActivity.this, appInfo.getApksize()));holder.tv_name.setText(appInfo.getApkname());if (appInfo.isRom()) {holder.tv_location.setText("手机内存");} else {holder.tv_location.setText("外部存储");}return view;}}static class ViewHolder{ImageView iv_icon;TextView tv_location;TextView tv_name ;TextView tv_apk_size;}private Handler handler = new Handler(){public void handleMessage(android.os.Message msg) {AppManagerAdapter adapter = new AppManagerAdapter();lv.setAdapter(adapter);};};private void initData() {// TODO Auto-generated method stubnew Thread(){public void run(){//获取到所有安装到手机上面的应用程序appinfos = AppInfos.getAppInfos(AppManagerActivity.this);//AppInfos是一个可以复用的类//appInfos拆成 用户程序的集合 + 系统程序的集合//用户程序的集合userAppInfos = new ArrayList<AppInfo>();//系统程序的集合systemAppInfos = new ArrayList<AppInfo>();for (AppInfo appInfo : appinfos) {//用户程序if (appInfo.isUserApp()) {userAppInfos.add(appInfo);} else {systemAppInfos.add(appInfo);}}handler.sendEmptyMessage(0);//这样更方便//也可以这样发消息 //Message obtain = Message.Obtain();                //handler.sendMessage(obtain);}}.start();}private void initUI() {// TODO Auto-generated method stub
        setContentView(R.layout.activity_app_manager);ViewUtils.inject(this);//ViewUtils下文说明lv = (ListView) findViewById(R.id.list_view);TextView tv_rom = (TextView) findViewById(R.id.tv_rom);TextView tv_sd = (TextView) findViewById(R.id.tv_sd); //得到ROM内存剩余空间,运行的大小long rom_freeSpace = Environment.getDataDirectory().getFreeSpace();//得到SD卡剩余空间,运行的大小long sd_freeSpace = Environment.getExternalStorageDirectory().getFreeSpace();//格式化大小tv_rom.setText("内存可用:"+Formatter.formatFileSize(this, rom_freeSpace));tv_sd.setText("SD卡可用:"+Formatter.formatFileSize(this, sd_freeSpace)); //卸载的广播UninstallReceiver receiver = new UninstallReceiver();IntentFilter intentFilter = new IntentFilter(Intent.ACTION_PACKAGE_REMOVED);intentFilter.addDataScheme("package");registerReceiver(receiver, intentFilter);//设置listview的滚动监听lv.setOnScrollListener(new AbsListView.OnScrollListener() {@Overridepublic void onScrollStateChanged(AbsListView view, int scrollState) {}/**** @param view* @param firstVisibleItem 第一个可见的条的位置* @param visibleItemCount 一页可以展示多少个条目* @param totalItemCount   总共的item的个数*/@Overridepublic void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {popupWindowDismiss();if (userAppInfos != null && systemAppInfos != null) {if (firstVisibleItem > (userAppInfos.size() + 1)) {//系统应用程序tv_app.setText("系统程序(" + systemAppInfos.size() + ")个");} else {//用户应用程序tv_app.setText("用户程序(" + userAppInfos.size() + ")个");}}}});//listview的点击监听lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id) {//获取到当前点击的item对象Object obj = lv.getItemAtPosition(position);if (obj != null && obj instanceof AppInfo) {clickAppInfo = (AppInfo) obj;View contentView = View.inflate(AppManagerActivity.this, R.layout.item_popup, null);LinearLayout ll_uninstall = (LinearLayout) contentView.findViewById(R.id.ll_uninstall);LinearLayout ll_share = (LinearLayout) contentView.findViewById(R.id.ll_share);LinearLayout ll_start = (LinearLayout) contentView.findViewById(R.id.ll_start);LinearLayout ll_detail = (LinearLayout) contentView.findViewById(R.id.ll_detail);ll_uninstall.setOnClickListener(AppManagerActivity.this);ll_share.setOnClickListener(AppManagerActivity.this);ll_start.setOnClickListener(AppManagerActivity.this);ll_detail.setOnClickListener(AppManagerActivity.this);  popupWindowDismiss();// -2表示包裹内容popupWindow = new PopupWindow(contentView, -2, -2);//需要注意:使用PopupWindow 必须设置背景。不然没有动画popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));int[] location = new int[2];//获取view展示到窗体上面的位置
                        view.getLocationInWindow(location);popupWindow.showAtLocation(parent, Gravity.LEFT + Gravity.TOP, 70, location[1]);//添加一个由小变大的动画ScaleAnimation sa = new ScaleAnimation(0.5f, 1.0f, 0.5f, 1.0f,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);sa.setDuration(3000);contentView.startAnimation(sa);}}});}private class UninstallReceiver extends BroadcastReceiver{@Overridepublic void onReceive(Context context, Intent intent) {System.out.println("接收到卸载的广播");}}private void popupWindowDismiss() {if (popupWindow != null && popupWindow.isShowing()) {popupWindow.dismiss();popupWindow = null;}}@Overrideprotected void onDestroy() {popupWindowDismiss();super.onDestroy();}
}

AppInfo.java  这是一个javabean

public class AppInfo {private Drawable icon;//图片的icon。Drawable适用的范围更广一点,可以是图片,也可以是xml等private String apkname;//程序的名字private long apksize;//程序的大小private boolean userApp;//表示是用户APP还是系统APP,true,用户APPprivate boolean isRom;//放置的位置private String apkPackageName;//包名

public Drawable getIcon() {return icon;}public void setIcon(Drawable icon) {this.icon = icon;}public String getApkname() {return apkname;}public void setApkname(String apkname) {this.apkname = apkname;}public long getApksize() {return apksize;}public void setApksize(long apksize) {this.apksize = apksize;}public boolean isUserApp() {return userApp;}public void setUserApp(boolean userApp) {this.userApp = userApp;}public boolean isRom() {return isRom;}public void setRom(boolean isRom) {this.isRom = isRom;}public String getApkPackageName() {return apkPackageName;}public void setApkPackageName(String apkPackageName) {this.apkPackageName = apkPackageName;}@Overridepublic String toString() {return "AppInfo [icon=" + icon + ", apkname=" + apkname + ", apksize=" + apksize + ", userApp=" + userApp+ ", isRom=" + isRom + ", apkPackageName=" + apkPackageName + "]";}}

AppInfos .java  获取当前手机上边所有的应用程序的详细信息
public class AppInfos {public static List<AppInfo> getAppInfos(Context context){List<AppInfo> packageAppinfos = new ArrayList<AppInfo>();PackageManager pm=context.getPackageManager();//获取到包的管理者,即清单文件中的东西List<PackageInfo> installPackages = pm.getInstalledPackages(0);//获取安装到手机上边的安装包for(PackageInfo installPackage:installPackages){AppInfo appinfo = new AppInfo();//javabean//获取到应用程序的图标/名字/包名/资源路径Drawable drawable = installPackage.applicationInfo.loadIcon(pm);appinfo.setIcon(drawable);String apkName = installPackage.applicationInfo.loadLabel(pm).toString();appinfo.setApkname(apkName);String packageName = installPackage.packageName;appinfo.setApkPackageName(packageName);String sourceDir = installPackage.applicationInfo.sourceDir;File file = new File(sourceDir);//apk的长度long apksize = file.length();appinfo.setApksize(apksize);        System.out.println(apkName+";"+packageName+";"+apksize);//第三方应用放在data/data/app   系统应用放在system/app//获取到安装应用程序的标记,都是二进制  int flags = installPackage.applicationInfo.flags;if((flags&ApplicationInfo.FLAG_SYSTEM)!=0){//系统应用appinfo.setUserApp(false);}else{//用户appappinfo.setUserApp(true);}if((flags&ApplicationInfo.FLAG_EXTERNAL_STORAGE)!=0){//sd卡appinfo.setRom(false);}else{//表示内存appinfo.setRom(true);}packageAppinfos.add(appinfo);}return packageAppinfos;    }}



activity_app_manager.xml
<?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="match_parent"android:orientation="vertical" ><TextViewstyle="@style/TitleStyle"android:text="我的软件" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal" ><TextViewandroid:id="@+id/tv_rom"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="内存可用:XXX" /><TextViewandroid:id="@+id/tv_sd"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="SD卡可用:XXX" /></LinearLayout><FrameLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"><includeandroid:id="@+id/list_view"layout="@layout/list_view"></include><TextViewandroid:id="@+id/tv_app"android:layout_width="match_parent"android:layout_height="wrap_content"android:textColor="#fff"android:background="#ff888888"android:text="用户程序(5)个" /></FrameLayout></LinearLayout>

item_popup.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/local_popup_bg"><LinearLayoutandroid:id="@+id/ll_uninstall"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:layout_marginRight="5dp"android:orientation="vertical"><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/img1" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="卸载" /></LinearLayout><LinearLayoutandroid:id="@+id/ll_start"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:layout_marginRight="5dp"android:orientation="vertical"><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/img2" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="运行" /></LinearLayout><LinearLayoutandroid:id="@+id/ll_share"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:layout_marginRight="5dp"android:orientation="vertical"><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/img3" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="分享" /></LinearLayout><LinearLayoutandroid:id="@+id/ll_detail"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:layout_marginRight="5dp"android:orientation="vertical"><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/img3" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="详情" /></LinearLayout></LinearLayout>

ViewUtils完全注解的方式进行UI绑定和事件绑定,无需findViewById()和 setClickListener().下面一篇博客来说明xUtils的使用。

转载于:https://www.cnblogs.com/mengxiao/p/6377547.html

手机安全卫士——软件管理-用户程序和系统程序相关推荐

  1. Android毕业设计——基于Android+Eclipse的手机安全卫士设计与实现(毕业论文+程序源码)——手机安全卫士

    基于Android+Eclipse的手机安全卫士设计与实现(毕业论文+程序源码) 大家好,今天给大家介绍基于Android+Eclipse的手机安全卫士设计与实现,文章末尾附有本毕业设计的论文和源码下 ...

  2. Android项目实战_手机安全卫士软件管家

    ###1.应用程序信息的flags 1. int flags = packageInfo.applicationInfo.flags 2. 0000 0000 0000 0000 0000 0000 ...

  3. 计算机毕业设计ssm教师授课信息综合管理平台451gu系统+程序+源码+lw+远程部署

    计算机毕业设计ssm教师授课信息综合管理平台451gu系统+程序+源码+lw+远程部署 计算机毕业设计ssm教师授课信息综合管理平台451gu系统+程序+源码+lw+远程部署 本源码技术栈: 项目架构 ...

  4. 计算机毕业设计ssm基于客户时间窗变化的物流配送管理系统设计ro75j系统+程序+源码+lw+远程部署

    计算机毕业设计ssm基于客户时间窗变化的物流配送管理系统设计ro75j系统+程序+源码+lw+远程部署 计算机毕业设计ssm基于客户时间窗变化的物流配送管理系统设计ro75j系统+程序+源码+lw+远 ...

  5. 计算机毕业设计ssm校外实习管理平台6tu82系统+程序+源码+lw+远程部署

    计算机毕业设计ssm校外实习管理平台6tu82系统+程序+源码+lw+远程部署 计算机毕业设计ssm校外实习管理平台6tu82系统+程序+源码+lw+远程部署 源码地址:https://pan.bai ...

  6. 计算机毕业设计ssm基于疫情防控下社区管理平台my3tu系统+程序+源码+lw+远程部署

    计算机毕业设计ssm基于疫情防控下社区管理平台my3tu系统+程序+源码+lw+远程部署 计算机毕业设计ssm基于疫情防控下社区管理平台my3tu系统+程序+源码+lw+远程部署 本源码技术栈: 项目 ...

  7. 计算机毕业设计ssm公交电子站牌管理系统软件9430l系统+程序+源码+lw+远程部署

    计算机毕业设计ssm公交电子站牌管理系统软件9430l系统+程序+源码+lw+远程部署 计算机毕业设计ssm公交电子站牌管理系统软件9430l系统+程序+源码+lw+远程部署 本源码技术栈: 项目架构 ...

  8. 计算机毕业设计ssm泸定中学宿舍管理系统设计g93gd系统+程序+源码+lw+远程部署

    计算机毕业设计ssm泸定中学宿舍管理系统设计g93gd系统+程序+源码+lw+远程部署 计算机毕业设计ssm泸定中学宿舍管理系统设计g93gd系统+程序+源码+lw+远程部署 本源码技术栈: 项目架构 ...

  9. 手机安全卫士开发系列(6)——程序主界面

    主界面的布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:and ...

最新文章

  1. 手把手快速实现 Resnet 残差模型实战
  2. Android MVP模式就是这么回事儿
  3. 学习Promise之前你必须理解的知识点:异步处理的通用模型
  4. 拾趣——ios::sync_with_stdio(false)详解(提高代码运算速度)
  5. 如何摆脱「自我否定」状态
  6. 直接使用汇编编写 .NET Standard 库
  7. redis配置主从没效果_跟我一起学Redis之加个哨兵让主从复制更加高可用
  8. 【英语学习】【Daily English】U06 Shopping L01 We are out of pasta.
  9. CoderForces999D-Equalize the Remainders
  10. 比尔·盖茨退出微软董事会,回顾盖茨与微软的传奇故事
  11. linux cron 服务,Linux定时任务Crontab详解(推荐)
  12. zz推荐的软件测试英文网站
  13. java基础源码 (2)--StringBuilder类
  14. 袖珍计算器c语言设计源码,VB程序题:编一模拟袖珍计算器的完整程序,界面如下图所示。要求:输入两个操作数和一个操作符,根据操作符决定所做的运算。 VB源码 龚沛曾...
  15. win10计算机ser,win10 ch341ser.inf安装失败如何处理_win10无法安装ch341ser.inf修复方法...
  16. teraterm 执行sql_teraterm自动或定时执行远程命令
  17. 类抽屉问题的C++解决
  18. linux ftw()函数使用方法 实例
  19. python爬虫脚本 初级入门爬虫英雄联盟所有皮肤_用Python爬取英雄联盟(lol)全部皮肤...
  20. SpringBoot整合Shiro搭建登录注册认证授权权限项目模板

热门文章

  1. 《中国顶尖技术团队访谈录·第二季》发布
  2. 《Effective C++》第8章 定制new和delete-读书笔记
  3. Android APK反编译详解(附图)
  4. 在数据库组件中用业务规则剖析挑选数据
  5. Word 2013无法发布文章到博客园
  6. ClassFlow推出全新课堂活动轨迹功能
  7. JDBC,JdbcTemplate,JPA傻傻分不清楚
  8. 页面与ViewModel(上)
  9. 【java】staitc
  10. win7的IE缓存,临时文件,cookies和历史记录