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

news/2024/7/7 18:39:13

首先看一下界面:

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


http://lihuaxi.xjx100.cn/news/253195.html

相关文章

如何利用pyecharts绘制炫酷的关系网络图?

如何利用pyecharts绘制炫酷的关系网络图 这是本学期在大数据哲学与社会科学实验室做的第六次分享了。 第一次分享的是&#xff1a; 如何利用“wordcloudjieba”制作中文词云&#xff1f; 第二次分享的是&#xff1a; 如何爬取知乎中问题的回答以及评论的数据&#xff1f; …

HDU 2102 A计划

该题是一道典型的搜索题&#xff0c; #include<stdio.h> #include<stdlib.h> #include<string.h> struct Node {int x, y;int time;int flag; }q[100024]; int d[4][2]{ 0,1,1,0,0,-1,-1,0 }; int N,M; char map[2][13][13]; void getxy( int &X,int &a…

判断链表是否存在环(及其延伸)

有一个单链表&#xff0c;其中可能有一个环&#xff0c;也就是某个节点的next指向的是链表中在它之前的节点&#xff0c;这样在链表的尾部形成一环。问题&#xff1a;1、如何判断一个链表是不是这类链表&#xff1f;2、如果链表为存在环&#xff0c;如果找到环的入口点&#xf…

NCEPU:线下组队学习周报(007)

线下组队学习 经过一段时间的准备&#xff0c;我们组织的线下组队学习逐步进入正轨。欢迎华北电力大学保定校区的伙伴加入进来大家一起学习一起成长。 我们开展组队学习的内容为&#xff1a; &#xff08;1&#xff09;周志华的《机器学习》&#xff08;西瓜书&#xff09; …

谁说 Python 写 GUI 程序丑?那是你不会美化!

作者 | 派森酱来源 | Python技术在平时工作学习当中&#xff0c;我们经常会编写一些简单的 Python GUI 工具&#xff0c;以此来完成各种各样的自动化任务&#xff0c;比如批量处理文件&#xff0c;批量处理图片等等。当我们进行这些工具的编写之时&#xff0c;往往只关注了功能…

Java实现二维码

Java实现二维码 最近突然想写一些博客&#xff0c;所以就陆陆续续的编写一些自我感觉有用的并且大家也可能用到的一些技术代码。方便互相学习交流。 今天这篇博客&#xff0c;主要是利用Java实现二维码&#xff1a; 在写代码之前先讲一下整体思路&#xff0c;以方便更好更快捷的…

【青少年编程】【二级】寻找宝石

「青少年编程竞赛交流群」已成立&#xff08;适合6至18周岁的青少年&#xff09;&#xff0c;公众号后台回复【Scratch】或【Python】&#xff0c;即可进入。如果加入了之前的社群不需要重复加入。 微信后台回复“资料下载”可获取以往学习的材料&#xff08;视频、代码、文档&…

zabbix4.0构建实录

【Nginx】 #wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo [rootcentos ~]# yum -y install zlib pcre pcre-devel openssl openssl-devel[rootcentos ~]# useradd -s /sbin/nologin nginx [rootzabbix-server ~]# yum install -y nginx 【M…