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

首先看一下界面:

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;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        initUI();
        initData();
    }
     @Override
        public 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{

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
             return userAppInfos.size() + 1 + systemAppInfos.size() + 1;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
             if (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;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

         @Override
            public 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 stub
        new 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() {
                @Override
                public void onScrollStateChanged(AbsListView view, int scrollState) {

                }

                /**
                 *
                 * @param view
                 * @param firstVisibleItem 第一个可见的条的位置
                 * @param visibleItemCount 一页可以展示多少个条目
                 * @param totalItemCount   总共的item的个数
                 */
                @Override
                public 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() + ")个");
                        }
                    }

                }
            });

            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public 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{

         @Override
         public void onReceive(Context context, Intent intent) {
             System.out.println("接收到卸载的广播");
         }
     }

     private void popupWindowDismiss() {
         if (popupWindow != null && popupWindow.isShowing()) {
             popupWindow.dismiss();
             popupWindow = null;
         }
     }

     @Override
     protected 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,用户APP
    private 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;
    }
    @Override
    public 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{//用户app
                appinfo.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" >

    <TextView
        style="@style/TitleStyle"
        android:text="我的软件" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/tv_rom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="内存可用:XXX" />

        <TextView
            android:id="@+id/tv_sd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="SD卡可用:XXX" />
    </LinearLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include
            android:id="@+id/list_view"
            layout="@layout/list_view"></include>

        <TextView
            android: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>
ViewUtils完全注解的方式进行UI绑定和事件绑定,无需findViewById()和 setClickListener().下面一篇博客来说明xUtils的使用。
时间: 2024-08-07 21:19:32

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

手机安全卫士——进程管理

首先看一下界面: TaskManagerActivity .java //進程管理 public class TaskManagerActivity extends Activity { @ViewInject(R.id.tv_task_process_count) private TextView tv_task_process_count; @ViewInject(R.id.tv_task_memory) private TextView tv_task_memory; @ViewInjec

向Windows 日志管理器写入系统程序日志信息

标准样例代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Diagnostics; using System.Diagnostics.Eventin

android141 360 安装软件管理java代码

AppManagerActivity package com.itheima52.mobilesafe.activity; import android.app.Activity; import android.appwidget.AppWidgetProvider; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import and

【边做项目边学Android】手机安全卫士05_1:程序主界面

主界面布局(知识点:GridView) mainscreen.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=

CentOS系统程序包管理之---rpm、yum和编译

一.程序包管理器 1.软件的运行 2.程序包管理 将编译好的文件打包成一个或有限的几个文件,可用于实现便捷的安装.卸载.升级.查询,校验等程序管理. centos常用的程序管理器有rpm和yum 二.程序包管理器之RPM  1.rpm是什么    RPM 是RPM Package Manager(RPM软件包管理器)的缩写,这一文件格式名称虽然打上了RedHat的标志,但是其原始设计理念是开放式的.RPM包管理器(RPM)是一个强大的命令行驱动的包管理系统能够安装.卸载.验证.查询和更新计算机软

Linux系统程序包管理工具 RPM

什么是RPM: RPM全名是"RedHat Package Manager",简称为RPM,这套软件管理机制是由RedHat这家公司发展而来的.RPM是以一种数据库记录的方式来将你所需要的软件安装到你的Linux系统的一套管理机制.其最大的特点就是将你要安装的软件先编译过,并且打包成为RPM机制的安装包,通过包装好的软件里面默认的数据库记录这个软件安装时必须具备的依赖属性软件,具备就安装.不具备就不予安装. 程序的组成部分: 编译之前:源代码 编译文件 二进制程序:/bin, /sbi

Linux系统程序包的管理功能相关命令rpm与yum的使用

一.软件包管理核心功能 1.软件包制作 2.包管理器:打包,安装.升级.卸载.查询及校验 3.工具:rpm .deb 4.程序包的组成部分: 二进制程序:/bin, /sbin,/ /usr/bin, /usr/sbin, 库文件:/lib64, /usr/lib64 配置文件:/etc 帮助文件:manual, info 5.rpm包管理 rpm:数据库  /var/lib/rpm rpmbuild:建立软件管理数据库 rpm包默认为二进制格式,有rpm包作者下载源码程序,编译完成后,制作成r

Linux的系统程序包管理

RPM 我们知道在操作系统之上使用的程序是由程序员通过开发工具开发出来的,而程序员编写的纯文本我们 称为源代码.由于计算机只认识二进制,程序员写好的源代码要经过一定的编译成计算机认识的二进制程序.而编译就是将源代码转成二进制,再通过一定的步骤来安装到时操作系统之上被我们使用. 虽然原始码进行软件编译, 毕竟不是每个人都会进行原始码编译的.如果我位的 Linux系统与发行厂商一模一样,那么在厂商系统上编译出来的程序,自然也可以在我们的系统上来运行.由于我们本来就是使用厂商的发行版,那么使用厂商系统

Linux系统程序包管理

Linux程序包管理 API:Application Program Interface ABI:Application Binary INnterface Unix-like,系统上的二级制格式的应用程序文件格式为 ELF Windows系统的二级制格式的应用程序文件格式为: exe,msi 库级别的虚拟化: Linux:WinE,可以实现在Linux系统上运行Windows的二进制可执行程序 Windows:Cywin,可以实现在Windows系统上运行Linux的位二进制可执行程序 各种编