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

时间:2024-01-29 11:56:05

###1.应用程序信息的flags

1. int flags = packageInfo.applicationInfo.flags
2. 0000 0000 0000 0000 0000 0000 0000 0000 //int的32位每一位的0或者1表示一个boolean值
3. 适用于需要大量boolean变量的场景,提高效率
4. 具体代码

int flags = packInfo.applicationInfo.flags;
if ((flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
//系统应用
} else {
//用户应用
}
if ((flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) {
//安装在sd卡
} else {
//安装在手机内存
}

###2.应用的安装位置
<manifest
... SD卡 手机内存 手机内存
android:installLocation="preferExternal | internalOnly | auto"
/>

###3.如何区分显示系统应用和用户应用
1. 将“应用程序集合”拆分成“用户程序集合”和“系统程序集合”两个集合
2. 在getView方法中,根据position判断,该位置应该是用户程序还是系统程序,在从对应的集合中取出数据
3. 注意,当显示第二个集合的数据时,需要对position进行修正

###4.将标签加入到ListView的item中显示
1. 在getCount中计算出正确的个数:用户程序个数+系统程序个数+标签个数
2. 在getView中,根据position判断需要显示的四种类型:用户程序标签、用户程序、系统程序标签、系统程序,根据不同的类型返回不同的view
3. 注意,标签是插在列表的前面和中间,需要对position进行修正

###5.ListView的item具有多种布局
当我们在Adapter中调用方法getView的时候,如果整个列表中的Item View如果有多种类型布局,如:

![](http://i.imgur.com/ug4MqzT.jpg)

我们继续使用convertView来将数据从新填充貌似不可行了,因为每次返回的convertView类型都不一样,无法重用。

Android在设计上的时候,也想到了这点。所以,在adapter中预留的两个方法。

public int getViewTypeCount(); //有多少种布局类型
public int getItemViewType(int position); //获取某个位置是哪种类型类型

只需要重写这两个方法,设置一下ItemViewType的个数和判断方法,Recycler就能有选择性的给出不同的convertView了。

private static final int TYPE_LABEL = 0;
private static final int TYPE_CONTENT = 1;

private class AppManagerAdapter extends BaseAdapter{

@Override
public int getViewTypeCount() {
return 2;
}

@Override
public int getItemViewType(int position) {
if(position == 0 || position == userAappInfos.size()+1){
return TYPE_LABEL;
}else{
return TYPE_CONTENT;
}
}

/**
* 返回listview里面有多少个条目
*/
@Override
public int getCount() {
//为什么要加两个1 , 增加了两个textview的标签。整个listview条目的个数增加了。
return 1+userAappInfos.size()+1+systemAppInfos.size();
}

/**
* 显示每个条目的view对象
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LabelViewHolder labelViewHolder = null;
ContentViewHolder contentViewHolder = null;

int type = getItemViewType(position);
if(convertView == null){
switch (type) {
case TYPE_LABEL:
convertView = new TextView(AppManagerActivity.this);
labelViewHolder = new LabelViewHolder();
labelViewHolder.tv_label = (TextView) convertView;
labelViewHolder.tv_label.setBackgroundColor(Color.GRAY);
convertView.setTag(labelViewHolder);
break;
case TYPE_CONTENT:
convertView = View.inflate(getApplicationContext(), R.layout.item_appinfo, null);
contentViewHolder = new ContentViewHolder();
contentViewHolder.iv_appIcon = (ImageView) convertView.findViewById(R.id.iv_appicon);
contentViewHolder.tv_appName = (TextView) convertView.findViewById(R.id.tv_appName);
contentViewHolder.tv_apkSize = (TextView) convertView.findViewById(R.id.tv_apkSize);
contentViewHolder.iv_install_location = (ImageView) convertView.findViewById(R.id.iv_install_location);
convertView.setTag(contentViewHolder);
break;
}
}else{
switch (type) {
case TYPE_LABEL:
labelViewHolder = (LabelViewHolder) convertView.getTag();
break;
case TYPE_CONTENT:
contentViewHolder = (ContentViewHolder) convertView.getTag();
break;
}
}

switch (type) {
case TYPE_LABEL:
if(position == 0){
labelViewHolder.tv_label.setText("用户程序:"+userAappInfos.size());
}else{
labelViewHolder.tv_label.setText("系统程序:"+systemAppInfos.size());
}
break;
case TYPE_CONTENT:
AppInfo appInfo;
if(position<=userAappInfos.size()){//用户程序
int newPosition = position - 1;//减去用户的标签textview占据的位置
appInfo = userAappInfos.get(newPosition);
}else {//系统程序
int newPosition = position - 1 - userAappInfos.size() - 1;
appInfo = systemAppInfos.get(newPosition);
}

contentViewHolder.iv_appIcon.setImageDrawable(appInfo.getAppIcon());
contentViewHolder.tv_appName.setText(appInfo.getAppName());
contentViewHolder.tv_apkSize.setText("程序大小:"+Formatter.formatFileSize(getApplicationContext(), appInfo.getAppSize()));
if(appInfo.isInRom()){
contentViewHolder.iv_install_location.setImageResource(R.drawable.memory);
}else{
contentViewHolder.iv_install_location.setImageResource(R.drawable.sd);
}
break;
}

return convertView;
}

@Override
public Object getItem(int position) {
return null;
}

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

/**
* 存放内容孩子对象的引用
*/
static class ContentViewHolder{
ImageView iv_appIcon;
TextView tv_appName;
TextView tv_apkSize;
ImageView iv_install_location;
}

/**
* 存放标签孩子对象的引用
*/
static class LabelViewHolder{
TextView tv_label;
}

###6.ListView的OnScrollListener--滑动监听器
public static int SCROLL_STATE_IDLE = 0; //停止
public static int SCROLL_STATE_TOUCH_SCROLL = 1; //手指拖动
public static int SCROLL_STATE_FLING = 2; //惯性滑动

listView.setonScrollListener(new OnScrollListener() {

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
//滚动状态发生变化。0停止,1手指拖动,2惯性滑动
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) {
//当listview正在滚动的时候调用的方法
//firstVisibleItem:第一个显示的Item的位置
//visibleItemCount:可显示的Item的总数
//totalItemCount:Item的总数
}
});

###7.PopupWindow的使用
//创建,指定显示的View和宽高
PopupWindow ppw = new PopupWindow(View contentView, int width, int height);
//显示,parent并不是指要把contentView 添加到parent上,而是要获取token
ppw. showAtLocation(View parent, int gravity, int x, int y);
//关闭
ppw. dismiss();

###8.PopupWindow使用注意事项
1.在show之前dismiss掉之前创建的,避免重复显示
2.在Activity的onDestory()的时候dismiss,避免窗体泄漏

###9.获取View在Window中的位置
int[] location = new int[2];
view.getLocationInWindow(location);
Location[0]: x坐标
Location[1]: y坐标

获取View在父View中的位置
view.getX();
view.getY();

###10.PopupWindow不显示动画的原因
1.手机中设置了动画关闭
2.PopupWindow没有设置背景

注:PopupWindow要设置背景和获取焦点,才能有点击弹框外消失的效果

 

 

###1.卸载应用的Intent
卸载请求

Intent intent = new Intent();
intent.setAction("android.intent.action.DELETE");
intent.addCategory("android.intent.category.DEFAULT");
intent.setData(Uri.parse("package:"+clickedAppInfo.getPackName()));
startActivity(intent);
安装请求

Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.setDataAndType(
Uri.fromFile(new File(apk_path)),
"application/vnd.android.package-archive");
startActivity(intent);

###2.接收卸载应用程序的广播
AppUninstallReceiver receiver = new AppUninstallReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
filter.addDataScheme("package");
registerReceiver(receiver, filter);

* **相关广播**
* ACTION\_PACKAGE\_ADDED 一个新应用包已经安装在设备上,数据包括包名(最新安装的包程序不能接收到这个广播,(当前正在安装的程序不能接收这个广播))
* ACTION\_PACKAGE\_REPLACED 一个新版本的应用安装到设备,替换之前已经存在的版本
* ACTION\_PACKAGE\_REMOVED 一个已存在的应用程序包已经从设备上移除,包括包名(正在被卸载的包程序不能接收到这个广播)
###3.遍历集合时如何删除数据
方法一:高级for循环,记录要删除的数据,遍历后再删除
AppInfo deleteAppInfo = null;
//更新ui界面
for(AppInfo appinfo: userAppInfos){
if(appinfo.getPackName().equals(packname)){
deleteAppInfo = appinfo;
}
}
if(deleteAppInfo!=null){
userAppInfos.remove(deleteAppInfo);
}

方法二:使用迭代器进行遍历,可在遍历中删除
Iterator<AppInfo> iterator = userAppInfos.iterator();
while(iterator.hasNext()){
AppInfo appinfo = iterator.next();
if (appinfo.getPackName().equals(packname)) {
iterator.remove();
}
}

方法三:使用普通for循环倒叙,可在遍历中删除。注意:如不采用倒叙,会遍历不全
for (int i = userAppInfos.size() - 1; i >= 0; i--) {
AppInfo appinfo = userAppInfos.get(i);
if (appinfo.getPackName().equals(packname)) {
userAppInfos.remove(i);
break;
}
}

###4.启动一个应用程序
PackageManager pm = getPackageManager();
Intent intent = pm.getLaunchIntentForPackage(clickedAppInfo.getPackName());
if(intent!=null){
startActivity(intent);
}else{
Toast.makeText(this, "对不起,该应用无法被开启", 0).show();
}

###5.分享
Intent intent = new Intent();
intent.setAction("android.intent.action.SEND");
intent.addCategory("android.intent.category.DEFAULT");
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "推荐你使用一款软件:"+clickedAppInfo.getAppName()+",真的很好用哦");
startActivity(intent);

###6.详细信息
Intent intent = new Intent("android.settings.APPLICATION_DETAILS_SETTINGS");
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setData(Uri.parse("package:"+clickedAppInfo.getPackName()));
startActivity(intent);

 主要代码:

  1 package com.hb.mobilesafe.activities;
  2 
  3 import java.util.ArrayList;
  4 import java.util.List;
  5 
  6 import android.annotation.SuppressLint;
  7 import android.app.Activity;
  8 import android.content.BroadcastReceiver;
  9 import android.content.Context;
 10 import android.content.Intent;
 11 import android.content.IntentFilter;
 12 import android.content.pm.PackageManager;
 13 import android.graphics.Color;
 14 import android.graphics.drawable.ColorDrawable;
 15 import android.net.Uri;
 16 import android.os.Bundle;
 17 import android.text.format.Formatter;
 18 import android.view.Gravity;
 19 import android.view.View;
 20 import android.view.View.OnClickListener;
 21 import android.view.ViewGroup;
 22 import android.view.Window;
 23 import android.view.animation.Animation;
 24 import android.view.animation.ScaleAnimation;
 25 import android.widget.AbsListView;
 26 import android.widget.AbsListView.OnScrollListener;
 27 import android.widget.AdapterView;
 28 import android.widget.AdapterView.OnItemClickListener;
 29 import android.widget.BaseAdapter;
 30 import android.widget.ImageView;
 31 import android.widget.LinearLayout;
 32 import android.widget.ListView;
 33 import android.widget.PopupWindow;
 34 import android.widget.TextView;
 35 import android.widget.Toast;
 36 
 37 import com.hb.demo_mobilesafe.R;
 38 import com.hb.mobilesafe.bean.mPackageInfo;
 39 import com.hb.mobilesafe.utils.MermorySize;
 40 import com.hb.mobilesafe.utils.PackageInfoUtil;
 41 
 42 public class SoftwareManagerActivity extends Activity implements OnClickListener{
 43     private TextView tv_software_count;
 44     private TextView tv_rom_available;
 45     private TextView tv_sdcard_available;
 46     private List<mPackageInfo> appInfo;
 47     private List<mPackageInfo> userInfo;
 48     private List<mPackageInfo> sysInfo;
 49     private ListView lv_show_allapp;
 50     private LinearLayout ll_progressbar;
 51     private MyAdapter adapter;
 52     private PopupWindow pw;
 53     private mPackageInfo mInfo;
 54     private LinearLayout ll_uninstall,ll_start,ll_share,ll_information;
 55     private MyReceiver receiver;
 56     
 57     @Override
 58     protected void onCreate(Bundle savedInstanceState) {
 59         super.onCreate(savedInstanceState);
 60         requestWindowFeature(Window.FEATURE_NO_TITLE);
 61         setContentView(R.layout.activity_softmanager);
 62         initView();
 63         initDate();
 64 
 65 
 66     }
 67     private void initView() {
 68         tv_rom_available=(TextView) findViewById(R.id.tv_rom_available);
 69         tv_sdcard_available=(TextView) findViewById(R.id.tv_sdcard_available);
 70         lv_show_allapp=(ListView) findViewById(R.id.lv_show_allapp);
 71         ll_progressbar=(LinearLayout) findViewById(R.id.ll_progressbar);
 72         tv_software_count=(TextView) findViewById(R.id.tv_software_count);
 73     }
 74     private void initDate() {
 75 //        mInfo=new mPackageInfo();
 76         //rom可用
 77         tv_rom_available.setText("机身内存可用:"+Formatter.formatFileSize(this, MermorySize.getMermory()));
 78         //sdcard可用
 79         tv_sdcard_available.setText("SD卡内存可用:"+Formatter.formatFileSize(this, MermorySize.getSd()));
 80 
 81         ll_progressbar.setVisibility(View.VISIBLE);
 82         new Thread(){
 83             public void run() {
 84                 appInfo = PackageInfoUtil.getAppInfo(SoftwareManagerActivity.this);
 85                 isSysUserApp();
 86                 adapter = new MyAdapter();
 87                 runOnUiThread(new Runnable() {
 88                     public void run() {
 89                         lv_show_allapp.setAdapter(adapter);
 90                         ll_progressbar.setVisibility(View.INVISIBLE);
 91 
 92                     }
 93                 });
 94 
 95             };
 96         }.start();
 97         lv_show_allapp.setOnScrollListener(new OnScrollListener() {
 98             //滑动后发生改变
 99             @Override
100             public void onScrollStateChanged(AbsListView view, int scrollState) {
101                 
102             }
103             //正在滑动
104             @Override
105             public void onScroll(AbsListView view, int firstVisibleItem,
106                     int visibleItemCount, int totalItemCount) {
107                 if(pw !=null){
108                     pw.dismiss();
109                     pw=null;
110                 }
111                 if(userInfo !=null && sysInfo !=null){
112 
113                     if(firstVisibleItem == 0){
114                         tv_software_count.setVisibility(View.GONE);
115                     }
116                     //else if(firstVisibleItem == userInfo.size()+1){
117                     //tv_software_count.setVisibility(View.GONE);
118                     //}
119                     else{
120                         tv_software_count.setVisibility(View.VISIBLE);
121                     }
122                     if(firstVisibleItem <=userInfo.size()){
123                         tv_software_count.setText("用户程序:"+userInfo.size());
124                     }else{
125                         tv_software_count.setText("系统程序:"+sysInfo.size());
126                     }
127                 }
128             }
129         });
130         lv_show_allapp.setOnItemClickListener(new OnItemClickListener() {
131 
132 
133 
134             @Override
135             public void onItemClick(AdapterView<?> parent, View view,
136                     int position, long id) {
137                 //判断如果点击的是两个TextView就让他什么都不执行
138                 if(position == 0 || position ==userInfo.size()+1){
139                     return;
140                 }
141                 if(pw != null){
142                     pw.dismiss();
143                     pw=null;
144                     
145                 }//判断是系统应用还是用户应用
146                 else if(position<userInfo.size()+1){
147                     //用户应用位置
148                     mInfo=userInfo.get(position-1);
149                 }else{
150                     //系统应用位置
151                     mInfo=sysInfo.get(position-2-userInfo.size());
152                 }
153                 
154                 View conver = View.inflate(SoftwareManagerActivity.this, R.layout.popuopwindow_item,null);
155                 pw = new PopupWindow(conver, -2, -2);
156                 
157                 //获取点击位置
158                 int [] location=new int [2];
159                 view.getLocationInWindow(location);
160                 //设置窗体的一个缩放动画
161                 ScaleAnimation sa = new ScaleAnimation(0.3f, 1f, 0.3f, 1f, Animation.RELATIVE_TO_SELF, Animation.RELATIVE_TO_SELF);
162                 //设置缩放时间
163                 sa.setDuration(500);
164                 
165                 //设置窗体的背景(透明的)
166                 pw.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
167                 //设置显示的位置
168                 pw.showAtLocation(parent, Gravity.RIGHT+Gravity.TOP, 30, location[1]-20);
169                 //pw.setContentView(conver);
170                 //开启动画
171                 conver.startAnimation(sa);
172                 
173                 
174                 ll_uninstall=(LinearLayout) conver.findViewById(R.id.ll_uninstall);
175                 ll_start=(LinearLayout) conver.findViewById(R.id.ll_start);
176                 ll_share=(LinearLayout) conver.findViewById(R.id.ll_share);
177                 ll_information=(LinearLayout) conver.findViewById(R.id.ll_information);
178                 ll_uninstall.setOnClickListener(SoftwareManagerActivity.this);
179                 ll_start.setOnClickListener(SoftwareManagerActivity.this);
180                 ll_information.setOnClickListener(SoftwareManagerActivity.this);
181                 ll_information.setOnClickListener(SoftwareManagerActivity.this);
182 
183             }
184 
185         });
186 
187     }
188     
189     @Override
190     public void onClick(View v) {
191         Intent intent;
192         switch (v.getId()) {
193         //卸载
194         case R.id.ll_uninstall:
195             String packageName = mInfo.getPackageName();
196             System.out.println("packageName:"+packageName);
197             intent= new Intent();
198             if(packageName.equals("com.hb.demo_mobilesafe")){
199                 Toast.makeText(SoftwareManagerActivity.this, "您不能卸载当前App", 0).show();
200                 pw.dismiss();
201                 return;
202             }
203             intent.setAction("android.intent.action.DELETE");
204             intent.addCategory("android.intent.category.DEFAULT");
205             intent.setData(Uri.parse("package:"+packageName));
206             startActivity(intent);
207             
208             IntentFilter filter=new IntentFilter();
209             filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
210             filter.addDataScheme("package");
211             receiver = new MyReceiver();
212             registerReceiver(receiver, filter);
213             
214             
215             break;
216             //启动
217         case R.id.ll_start:
218             PackageManager pm=getPackageManager();
219             intent = pm.getLaunchIntentForPackage(mInfo.getPackageName());
220             if(intent !=null){
221                 startActivity(intent);
222                 pw.dismiss();
223             }
224             
225             break;
226             //分享
227         case R.id.ll_share:
228             intent = new Intent();
229             intent.setAction("android.intent.action.SEND");
230             intent.addCategory("android.intent.category.DEFAULT");
231             intent.setType("text/plain");
232             intent.putExtra(Intent.EXTRA_TEXT, "推荐你使用一款软件:"+mInfo.getAppName()+",真的很好用哦");
233             startActivity(intent);
234             break;
235             //信息
236         case R.id.ll_information:
237             intent = new Intent("android.settings.APPLICATION_DETAILS_SETTINGS");
238             intent.addCategory(Intent.CATEGORY_DEFAULT);
239             intent.setData(Uri.parse("package:"+mInfo.getPackageName()));
240             startActivity(intent);
241             break;
242         }
243 
244     }
245     private class MyAdapter extends BaseAdapter{
246         @SuppressLint("ViewHolder") @Override
247         public View getView(int position, View convertView, ViewGroup parent) {
248             ViewHolder holder;
249             if(convertView !=null && convertView instanceof LinearLayout){
250                 holder=(ViewHolder) convertView.getTag();
251 
252             }else{
253                 holder=new ViewHolder();
254                 convertView=View.inflate(SoftwareManagerActivity.this, R.layout.appinfo_item, null);
255                 holder.appIcon=(ImageView) convertView.findViewById(R.id.iv_app_icon);
256                 holder.appName=(TextView) convertView.findViewById(R.id.tv_pro_name);
257                 holder.appSize=(TextView) convertView.findViewById(R.id.tv_app_size);
258                 holder.appLocation=(ImageView) convertView.findViewById(R.id.iv_location);
259                 convertView.setTag(holder);
260             }
261             if(position == 0){
262                 TextView tv0= new TextView(SoftwareManagerActivity.this);
263                 if(userInfo.size()==0){
264                     tv0.setHeight(0);
265                     return tv0;
266                 }
267                 tv0.setClickable(false);
268                 tv0.setTextColor(Color.WHITE);
269                 tv0.setBackgroundColor(Color.GRAY);
270                 tv0.setText("用户程序:"+userInfo.size());
271                 return tv0;
272             }else if(position ==userInfo.size()+1){
273                 TextView tv1= new TextView(SoftwareManagerActivity.this);
274                 tv1.setClickable(false);
275                 tv1.setTextColor(Color.WHITE);
276                 tv1.setBackgroundColor(Color.GRAY);
277                 tv1.setText("系统程序:"+sysInfo.size());
278                 return tv1;
279             }else if(position <= userInfo.size()){
280                 int newPosition = position-1;
281                 boolean sd = userInfo.get(newPosition).isSd();
282                 if(sd){
283                     holder.appLocation.setBackgroundResource(R.drawable.sd);
284                 }else{
285                     holder.appLocation.setBackgroundResource(R.drawable.memory);
286                 }
287                 holder.appIcon.setImageDrawable(userInfo.get(newPosition).getIcon());
288                 holder.appName.setText(userInfo.get(newPosition).getAppName());
289                 holder.appSize.setText("程序大小:"+Formatter.formatFileSize(SoftwareManagerActivity.this, userInfo.get(newPosition).getAppSize()));
290             }else{
291                 int newPosition=position -userInfo.size()-2;
292                 boolean sd = sysInfo.get(newPosition).isSd();
293                 if(sd){
294                     holder.appLocation.setBackgroundResource(R.drawable.sd);
295                 }else{
296                     holder.appLocation.setBackgroundResource(R.drawable.memory);
297                 }
298                 holder.appIcon.setImageDrawable(sysInfo.get(newPosition).getIcon());
299                 holder.appName.setText(sysInfo.get(newPosition).getAppName());
300                 holder.appSize.setText("程序大小:"+Formatter.formatFileSize(SoftwareManagerActivity.this, sysInfo.get(newPosition).getAppSize()));
301             }
302             return convertView;
303         }
304 
305         @Override
306         public int getCount() {
307             /**
308              * 返回系统应用与用户应用的数量
309              */
310             return sysInfo.size()+1+userInfo.size()+1;
311         }
312 
313         @Override
314         public Object getItem(int position) {
315             return null;
316         }
317 
318         @Override
319         public long getItemId(int position) {
320             return 0;
321         }
322 
323 
324 
325     }
326     class ViewHolder{
327         ImageView appIcon;
328         ImageView appLocation;
329         TextView appName;
330         TextView appSize;
331 
332 
333     }
334     /**
335      * 判断是系统应用还是用户应用
336      */
337     private void isSysUserApp(){
338         userInfo = new ArrayList<mPackageInfo>();
339         sysInfo = new ArrayList<mPackageInfo>();
340         for (mPackageInfo Info : appInfo) {
341             boolean men = Info.isMen();
342             if(men){
343                 sysInfo.add(Info);
344             }else{
345                 userInfo.add(Info);
346             }
347         }
348     }
349     class MyReceiver extends BroadcastReceiver{
350 
351         @Override
352         public void onReceive(Context context, Intent intent) {
353             String packcageName=null;
354             mPackageInfo tag=null;
355             String data = intent.getData().toString();
356             
357             unregisterReceiver(this);
358             
359             if(data != null){
360                 
361                 packcageName = data.replace("package:", "");
362             
363             }
364             for (mPackageInfo info : appInfo) {
365                 
366                 if(info.getPackageName().equals(packcageName)){
367                     tag=info;
368                 }
369             }
370             if(appInfo != null){
371                 System.out.println("程序卸载已经更新"+tag);
372                 appInfo.remove(tag);
373                 isSysUserApp();
374                 adapter.notifyDataSetChanged();
375             }
376             
377         }
378         
379     }
380 
381 }
 1 package com.hb.mobilesafe.utils;
 2 
 3 import java.io.File;
 4 import java.util.ArrayList;
 5 import java.util.List;
 6 
 7 import android.content.Context;
 8 import android.content.pm.ApplicationInfo;
 9 import android.content.pm.PackageInfo;
10 import android.content.pm.PackageManager;
11 import android.graphics.drawable.Drawable;
12 import android.os.SystemClock;
13 
14 import com.hb.mobilesafe.bean.mPackageInfo;
15 
16 
17 public class PackageInfoUtil {
18     public static List<mPackageInfo> getAppInfo(Context context){
19         List<mPackageInfo> list=new ArrayList<mPackageInfo>();
20 
21         PackageManager pm = context.getPackageManager();
22         List<PackageInfo> packages = pm.getInstalledPackages(0);
23         for (PackageInfo info1 : packages) {
24             mPackageInfo info = new mPackageInfo();
25             Drawable appIcon = info1.applicationInfo.loadIcon(pm);
26             String appName = info1.applicationInfo.loadLabel(pm).toString();
27             String appPackageName = info1.packageName.toString();
28             String path = info1.applicationInfo.sourceDir.toString();
29             File file=new File(path);
30             long appSize = file.length();
31 
32             int flags = info1.applicationInfo.flags;
33             /**
34              * 判断是否是系统应用
35              */
36             if((flags & ApplicationInfo.FLAG_SYSTEM)!=0){
37                 info.setMen(true);
38             }else{
39                 info.setMen(false);
40             }
41             /**
42              * 判断是否是安装在sd卡上的
43              */
44             if((flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE)!=0){
45                 info.setSd(false);
46             }else {
47                 info.setSd(true);
48             }
49             info.setAppName(appName);
50             info.setAppSize(appSize);
51             info.setIcon(appIcon);
52             info.setPackageName(appPackageName);
53             list.add(info);
54 
55         }
56         SystemClock.sleep(1200);
57         return list;
58 
59     }
60 }

 

xml:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:gravity="center_horizontal" >
 6 
 7     <TextView
 8         android:id="@+id/tv_title"
 9         android:layout_width="match_parent"
10         android:layout_height="46dp"
11         android:background="#A1FF80"
12         android:gravity="center_vertical"
13         android:text="程序管理"
14         android:textSize="20sp" />
15 
16     <RelativeLayout
17         android:id="@+id/rl_size"
18         android:layout_width="match_parent"
19         android:layout_height="wrap_content"
20         android:layout_below="@id/tv_title" >
21 
22         <TextView
23             android:id="@+id/tv_rom_available"
24             android:layout_width="wrap_content"
25             android:layout_height="wrap_content"
26             android:drawableLeft="@drawable/memory"
27             android:text="机身内存可用:181MB"
28             android:textSize="10sp" />
29 
30         <TextView
31             android:id="@+id/tv_sdcard_available"
32             android:layout_width="wrap_content"
33             android:layout_height="wrap_content"
34             android:layout_alignParentRight="true"
35             android:drawableLeft="@drawable/sd"
36             android:text="SD卡内存可用:181MB"
37             android:textSize="10sp" />
38     </RelativeLayout>
39 
40     <TextView
41         android:visibility="gone"
42         android:textColor="#ffffff"
43         android:clickable="false"
44         android:id="@+id/tv_software_count"
45         android:layout_width="match_parent"
46         android:layout_height="wrap_content"
47         android:layout_below="@id/rl_size"
48         android:background="#888888"
49         android:text="用户程序:5"
50          />
51 
52     <LinearLayout
53         android:id="@+id/ll_progressbar"
54         android:layout_width="wrap_content"
55         android:layout_height="wrap_content"
56         android:layout_centerInParent="true"
57         android:gravity="center"
58         android:orientation="vertical"
59         android:visibility="invisible" >
60 
61         <ProgressBar
62             android:layout_width="wrap_content"
63             android:layout_height="wrap_content" />
64 
65         <TextView
66             android:layout_width="wrap_content"
67             android:layout_height="wrap_content"
68             android:layout_centerInParent="true"
69             android:text="正在加载中..."
70             android:textSize="15sp" />
71     </LinearLayout>
72 
73     <ListView
74         android:id="@+id/lv_show_allapp"
75         android:layout_width="match_parent"
76         android:layout_height="match_parent"
77         android:layout_below="@id/tv_software_count" />
78 
79 </RelativeLayout>
View Code