标签:android开发
如何在安卓上设置快捷图标
新建项目:快捷图标 1、想干什么事情 2、起个名字 3、修改图标 写第一步代码要注册一个广播,观察源码在创建快捷方式的时候在清单文件里是怎么设置广播的
创建快捷方式的原理:在安卓源码中搜索Launcher2文件夹里的清单文件发现在里面的receiver节点是系统如何创建快捷方式的广播与频道,即我们需要创建快捷方式的时候就要调用到此广播的频道,然后截取后进行改变广播里的意图,把广播的意图改成自己的意图然后再发送出去
<!-- Intent received used to install shortcuts from other applications -->
<receiver
android:name="com.android.launcher2.InstallShortcutReceiver"
android:permission="com.android.launcher.permission.INSTALL_SHORTCUT">
<intent-filter>
<action android:name="com.android.launcher.action.INSTALL_SHORTCUT" />
</intent-filter>
</receiver>
在SplashActivity.class加上方法createShortcut去创建快捷图标
在清单文件里MainActivity节点加上自定义的action:目的是把系统创建快捷方式的广播改变意图,而把创建出来的快捷方式的路径改成主页面,通过action和category来限定
<activity
android:name="com.itheima.mobile47.MainActivity"
android:screenOrientation="portrait" >
<intent-filter >
<action android:name="aaa.bbb.ccc"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
继续编写创建快捷方法的代码
/**
* 创建快捷方式
*/
private void createShortcut() {
Intent intent = new Intent();
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
//干什么事,叫什么名,长什么样
Intent shortcutIntent = new Intent();
intent.putExtra("duplicate", false);//只允许一个快捷图标
shortcutIntent.setAction("aaa.bbb.ccc");
shortcutIntent.addCategory(Intent.CATEGORY_DEFAULT);
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "黑马快捷");
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, BitmapFactory.decodeResource(getResources(), R.drawable.main_icon));
sendBroadcast(intent);
}
实现软件管家的功能:功能3 MainActivity.class新建case2的点击事件(软件管家)
case 2:
//软件管家
intent = new Intent(MainActivity.this,AppManagerActivity.class);
startActivity(intent);
break;
AppManagerActivity.class和清单文件节点
AppManagerActivity.class
public class AppManagerActivity extends Activity {
@ViewInject(R.id.tv_rom)
TextView tv_rom;
@ViewInject(R.id.tv_sd)
TextView tv_sd;
@ViewInject(R.id.list_view)
private ListView list_view;
@ViewInject(R.id.ll_loading)
private LinearLayout ll_loading;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app_manager);
ViewUtils.inject(this);
// 获取到SD卡的剩余空间
long sd_freeSpace = Environment.getExternalStorageDirectory()
.getFreeSpace();
tv_sd.setText("sd卡剩余:" + Formatter.formatFileSize(this, sd_freeSpace));
// 获取到内置sd卡的空间
long rom_freeSpace = Environment.getDataDirectory().getFreeSpace();
tv_rom.setText("rom剩余:" + Formatter.formatFileSize(this, rom_freeSpace));
initData();
}
private List<AppInfo> infos;
private List<AppInfo> userAppInfos;
private List<AppInfo> systemAppInfos;
private class TaskRunnable implements Runnable {
@Override
public void run() {
infos = AppInfoparser.getAppInfo(AppManagerActivity.this);
userAppInfos = new ArrayList<AppInfo>();
systemAppInfos = new ArrayList<AppInfo>();
for (AppInfo info : infos) {
if (info.isUserApp()) {
userAppInfos.add(info);
} else {
systemAppInfos.add(info);
}
}
handler.sendEmptyMessage(0);
}
}
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
// 加载完成之后。隐藏进度条
ll_loading.setVisibility(View.INVISIBLE);
AppManagerAdapter adapter = new AppManagerAdapter(
AppManagerActivity.this, infos);
list_view.setAdapter(adapter);
};
};
private void initData() {
// 展示进度条
ll_loading.setVisibility(View.VISIBLE);
// 开启线程池加载所有的app
ThreadPoolManager threadPoolManager = ThreadPoolManager.getInstance();
TaskRunnable taskRunnable = new TaskRunnable();
threadPoolManager.addTask(taskRunnable);
}
private class ViewHolder {
// icon图标
ImageView iv_icon;
// 应用名字
TextView tv_app_name;
// 应用安装的位置
TextView tv_app_location;
// 应用的大小
TextView tv_app_size;
}
private class AppManagerAdapter extends BaseAdapter {
private View view;
private ViewHolder holder;
private List<AppInfo> apps;
private AppInfo appInfo;
public AppManagerAdapter(AppManagerActivity appManagerActivity,
List<AppInfo> infos) {
this.apps = infos;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (position == 0) {
TextView textView = new TextView(parent.getContext());
textView.setTextColor(Color.WHITE);
textView.setBackgroundColor(Color.GRAY);
textView.setText("应用程序:" + userAppInfos.size() + "个");
return textView;
} else if (position == userAppInfos.size() + 1) {
TextView textView = new TextView(parent.getContext());
textView.setTextColor(Color.WHITE);
textView.setBackgroundColor(Color.GRAY);
textView.setText("系统程序:" + systemAppInfos.size() + "个");
return textView;
}
if (convertView != null && convertView instanceof LinearLayout) {
view = convertView;
holder = (ViewHolder) view.getTag();
} else {
view = View.inflate(AppManagerActivity.this,
R.layout.item_app_manager, null);
holder = new ViewHolder();
holder.iv_icon = (ImageView) view.findViewById(R.id.iv_icon);
holder.tv_app_location = (TextView) view
.findViewById(R.id.tv_app_location);
holder.tv_app_name = (TextView) view
.findViewById(R.id.tv_app_name);
holder.tv_app_size = (TextView) view
.findViewById(R.id.tv_app_size);
view.setTag(holder);
}
appInfo = apps.get(position);
if (position < userAppInfos.size() + 1) {
appInfo = userAppInfos.get(position - 1);
} else {
appInfo = systemAppInfos.get(position - 1 - userAppInfos.size()
- 1);
}
holder.iv_icon.setImageDrawable(appInfo.getmDrawable());
if (appInfo.isInRom()) {
holder.tv_app_location.setText("手机内存");
} else {
holder.tv_app_location.setText("外部存储");
}
holder.tv_app_name.setText(appInfo.getAppName());
holder.tv_app_size.setText(Formatter.formatFileSize(
AppManagerActivity.this, appInfo.getApkSize()));
return view;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
if (position == 0) {
return null;
}
if (position == userAppInfos.size() + 1) {
return null;
}
if (position < userAppInfos.size() + 1) {
appInfo = userAppInfos.get(position - 1);
} else {
appInfo = systemAppInfos.get(position - 1 - userAppInfos.size()
- 1);
}
return appInfo;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public int getCount() {
// 默认情况直接使用size 。但是当前需要加2个item的条目
return apps.size() + 1 + 1;
}
}
}
新建activityappmanager.xml布局文件
activityappmanager.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/textview_title_style"
android:gravity="center"
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="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="剩余手机内部" />
<TextView
android:id="@+id/tv_sd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="sd卡:" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/ll_loading"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:visibility="invisible" >
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="玩命加载中...." />
</LinearLayout>
<include layout="@layout/list_view" />
</FrameLayout>
</LinearLayout>
在appManagerActivity.class里注入的布局文件里显示sd卡的剩余空间
AppManagerActivity.java
// 获取到SD卡的剩余空间
long sd_freeSpace = Environment.getExternalStorageDirectory()
.getFreeSpace();
tv_sd.setText("sd卡剩余:" + Formatter.formatFileSize(this, sd_freeSpace));
// 获取到内置sd卡的空间
long rom_freeSpace = Environment.getDataDirectory().getFreeSpace();
tv_rom.setText("rom剩余:" + Formatter.formatFileSize(this, rom_freeSpace));
然后在init方法里初始化app的列表
AppManagerActivity.java
private List<AppInfo> infos;
private List<AppInfo> userAppInfos;
private List<AppInfo> systemAppInfos;
private class TaskRunnable implements Runnable {
@Override
public void run() {
infos = AppInfoparser.getAppInfo(AppManagerActivity.this);
userAppInfos = new ArrayList<AppInfo>();
systemAppInfos = new ArrayList<AppInfo>();
for (AppInfo info : infos) {
if (info.isUserApp()) {
userAppInfos.add(info);
} else {
systemAppInfos.add(info);
}
}
handler.sendEmptyMessage(0);
}
}
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
// 加载完成之后。隐藏进度条
ll_loading.setVisibility(View.INVISIBLE);
AppManagerAdapter adapter = new AppManagerAdapter(
AppManagerActivity.this, infos);
list_view.setAdapter(adapter);
};
};
private void initData() {
// 展示进度条
ll_loading.setVisibility(View.VISIBLE);
// 开启线程池加载所有的app
ThreadPoolManager threadPoolManager = ThreadPoolManager.getInstance();
TaskRunnable taskRunnable = new TaskRunnable();
threadPoolManager.addTask(taskRunnable);
}
private class ViewHolder {
// icon图标
ImageView iv_icon;
// 应用名字
TextView tv_app_name;
// 应用安装的位置
TextView tv_app_location;
// 应用的大小
TextView tv_app_size;
}
private class AppManagerAdapter extends BaseAdapter {
private View view;
private ViewHolder holder;
private List<AppInfo> apps;
private AppInfo appInfo;
public AppManagerAdapter(AppManagerActivity appManagerActivity,
List<AppInfo> infos) {
this.apps = infos;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (position == 0) {
TextView textView = new TextView(parent.getContext());
textView.setTextColor(Color.WHITE);
textView.setBackgroundColor(Color.GRAY);
textView.setText("应用程序:" + userAppInfos.size() + "个");
return textView;
} else if (position == userAppInfos.size() + 1) {
TextView textView = new TextView(parent.getContext());
textView.setTextColor(Color.WHITE);
textView.setBackgroundColor(Color.GRAY);
textView.setText("系统程序:" + systemAppInfos.size() + "个");
return textView;
}
if (convertView != null && convertView instanceof LinearLayout) {
view = convertView;
holder = (ViewHolder) view.getTag();
} else {
view = View.inflate(AppManagerActivity.this,
R.layout.item_app_manager, null);
holder = new ViewHolder();
holder.iv_icon = (ImageView) view.findViewById(R.id.iv_icon);
holder.tv_app_location = (TextView) view
.findViewById(R.id.tv_app_location);
holder.tv_app_name = (TextView) view
.findViewById(R.id.tv_app_name);
holder.tv_app_size = (TextView) view
.findViewById(R.id.tv_app_size);
view.setTag(holder);
}
appInfo = apps.get(position);
if (position < userAppInfos.size() + 1) {
appInfo = userAppInfos.get(position - 1);
} else {
appInfo = systemAppInfos.get(position - 1 - userAppInfos.size()
- 1);
}
holder.iv_icon.setImageDrawable(appInfo.getmDrawable());
if (appInfo.isInRom()) {
holder.tv_app_location.setText("手机内存");
} else {
holder.tv_app_location.setText("外部存储");
}
holder.tv_app_name.setText(appInfo.getAppName());
holder.tv_app_size.setText(Formatter.formatFileSize(
AppManagerActivity.this, appInfo.getApkSize()));
return view;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
if (position == 0) {
return null;
}
if (position == userAppInfos.size() + 1) {
return null;
}
if (position < userAppInfos.size() + 1) {
appInfo = userAppInfos.get(position - 1);
} else {
appInfo = systemAppInfos.get(position - 1 - userAppInfos.size()
- 1);
}
return appInfo;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public int getCount() {
// 默认情况直接使用size 。但是当前需要加2个item的条目
return apps.size() + 1 + 1;
}
}
在engie里新建AppInfoparser.java
AppInfoparser.java
public class AppInfoparser {
public static List<AppInfo> getAppInfo(Context context) {
// 获取到安装包的管理者
PackageManager pm = context.getPackageManager();
// 获取到安装包的所有基本程序的信息
List<PackageInfo> infos = pm.getInstalledPackages(0);
List<AppInfo> lists = new ArrayList<AppInfo>();
for (PackageInfo info : infos) {
AppInfo appInfo = new AppInfo();
// 获取到app的包名
String packageName = info.packageName;
appInfo.setPackageName(packageName);
// 获取到icon图标
Drawable icon = info.applicationInfo.loadIcon(pm);
appInfo.setmDrawable(icon);
// 获取到应用程序的名字
String appname = (String) info.applicationInfo.loadLabel(pm);
appInfo.setAppName(appname);
// 获取到apk的路径
String apkPath = info.applicationInfo.sourceDir;
appInfo.setApkPath(apkPath);
File file = new File(apkPath);
// 获取到apk的大小
long apkSize = file.length();
appInfo.setApkSize(apkSize);
int flags = info.applicationInfo.flags;
// 判断当前是应用app还是系统app
if ((flags & info.applicationInfo.FLAG_SYSTEM) != 0) {
// true 表示是系统应用
appInfo.setUserApp(false);
} else {
// false 表示是用户应用
appInfo.setUserApp(true);
}
// 判断当前是app是否是安装到哪里
if ((flags & info.applicationInfo.FLAG_EXTERNAL_STORAGE) != 0) {
// ture 表示安装到外部存储 sd卡
appInfo.setInRom(false);
} else {
// false 表示安装到机身内存。rom
appInfo.setInRom(true);
}
lists.add(appInfo);
//
// System.out.println("应用程序的包名:" +packageName);
// System.out.println("应用程序的路径:" +apkPath);
// System.out.println("应用程序的大小:" +apkSize);
// System.out.println("应用程序的名字:" +appname);
// System.out.println("-------------------------------------");
// System.out.println(appInfo.toString());
// 如果是/system/app那么就是系统应用
// if(apkPath.startsWith("/system/app")){
//
// }else{
//
// }
}
return lists;
}
}
继续在bean里实现AppInfo
AppInfo.java
public class AppInfo {
/**
* 图标
*/
private Drawable mDrawable = null;
/**
* 应用程序的名字
*/
private String appName = null;
/**
* 应用程序的包名
*/
private String packageName = null;
/**
* 应用程序的大小
*/
private long apkSize;
/**
* 应用程序的路径
*/
private String apkPath = null;
/**
* 安装到哪里
*/
private boolean isInRom = false;
/**
* 判断是系统app还是用户app
*/
private boolean isUserApp = false;
public Drawable getmDrawable() {
return mDrawable;
}
public void setmDrawable(Drawable mDrawable) {
this.mDrawable = mDrawable;
}
public String getAppName() {
return appName;
}
public void setAppName(String appName) {
this.appName = appName;
}
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
public long getApkSize() {
return apkSize;
}
public void setApkSize(long apkSize) {
this.apkSize = apkSize;
}
public String getApkPath() {
return apkPath;
}
public void setApkPath(String apkPath) {
this.apkPath = apkPath;
}
public boolean isInRom() {
return isInRom;
}
public void setInRom(boolean isInRom) {
this.isInRom = isInRom;
}
public boolean isUserApp() {
return isUserApp;
}
public void setUserApp(boolean isUserApp) {
this.isUserApp = isUserApp;
}
@Override
public String toString() {
return "AppInfo [appName=" + appName
+ ", packageName=" + packageName + ", apkSize=" + apkSize
+ ", apkPath=" + apkPath + ", isInRom=" + isInRom
+ ", isUserApp=" + isUserApp + "]";
}
}
继续编写AppInfoParser.java 课下复习下这一块内容 写完了AppInfoparser.java里的方法,然后在appManagerActivity.class的init方法里调用 继续在AppInfoparser.java里标记应用是否是系统应用 继续在AppInfoparser.java里判断app安装在哪里
设置完毕后就在AppManagerActivity.class展现出来
实现activityappmanager.xml布局里的listView布局 新建itemappmanager.xml布局
itemappmanager.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="horizontal" >
<ImageView
android:id="@+id/iv_icon"
android:layout_width="45dp"
android:layout_height="45dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:singleLine="true"
android:textColor="#000" />
<TextView
android:id="@+id/tv_app_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#88000000" />
</LinearLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/tv_app_size"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</LinearLayout>
设置完后在getView里注入listView布局 使用holder来初始化item里的控件
itemappmanager.xml:修改imageView里的值限制资源不走形 AppManagerManager.java中的initData()使用线程池来改进原initData()中的代码,但是listView的setAdapter不能在线程中进行,所以配合handler来进行 继续增加特效在activityappmanager.xml里,让用户滑动listview时有progressBar的效果 默认的时候先让其隐藏 然后在AppManagerActivity.class加载控件(在initData中)显示进度条 然后在handler里加载完成后隐藏进度条 在AppManagerActivity.class里继续定义两个集合(用户应用集合,系统应用集合),然后在子线程中遍历存储进集合中。 在MyBaseAdapter.java里的getCount()中返回的lists.size()需要+1+1 最后选择了原生态的BaseAdapter() 修改myAdapter中的内容,为了加两个item的条目,显示系统app多少个,用户app多个个的信息 在getItem里进行设置判断 判断完毕后,就在getView里进行给Item新建统计条目。 然后调整代码 调整小bug,更改getView里的view填充的小细节
AppManagerActivity.java
public class AppManagerActivity extends Activity {
@ViewInject(R.id.tv_rom)
TextView tv_rom;
@ViewInject(R.id.tv_sd)
TextView tv_sd;
@ViewInject(R.id.ll_loading)
private LinearLayout ll_loading;
@ViewInject(R.id.list_view)
private ListView list_view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_app_manager);
ViewUtils.inject(this);
//获取到SD卡的剩余空间
long sd_freeSpace = Environment.getExternalStorageDirectory().getFreeSpace();
tv_sd.setText("sd卡剩余:"+Formatter.formatFileSize(this, sd_freeSpace));
//获取到内置sd卡的空间
long rom_freeSpace = Environment.getDataDirectory().getFreeSpace();
tv_rom.setText("rom剩余:"+Formatter.formatFileSize(this, rom_freeSpace));
initData();
}
private List<AppInfo> infos;
private List<AppInfo> userAppInfos;
private List<AppInfo> systemAppInfos;
private class TaskRunnable implements Runnable{
@Override
public void run() {
infos = AppInfoparser.getAppInfo(AppManagerActivity.this);
userAppInfos = new ArrayList<AppInfo>();
systemAppInfos = new ArrayList<AppInfo>();
for(AppInfo info : infos){
if(info.isUserApp()){
userAppInfos.add(info);
}else{
systemAppInfos.add(info);
}
}
handler.sendEmptyMessage(0);
}
}
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
//加载完成之后,隐藏进度条
ll_loading.setVisibility(View.INVISIBLE);
AppManagerAdapter adapter = new AppManagerAdapter(AppManagerActivity.this, infos);
list_view.setAdapter(adapter);
};
};
private void initData() {
//显示进度条
ll_loading.setVisibility(View.VISIBLE);
//开启线程池加载所有的app
ThreadPoolManager threadPoolManager = ThreadPoolManager.getInstance();
TaskRunnable taskRunnable = new TaskRunnable();
threadPoolManager.addTask(taskRunnable);
}
private class ViewHolder{
//icon图标
ImageView iv_icon;
//应用名字
TextView tv_app_name;
//应用安装的位置
TextView tv_app_location;
//应用的大小
TextView tv_app_size;
//应用的路径
TextView tv_app_path;
}
private class AppManagerAdapter extends BaseAdapter{
private View view;
private ViewHolder holder;
private List<AppInfo> apps;
private AppInfo appInfo;
public AppManagerAdapter(AppManagerActivity appManagerActivity,List<AppInfo>infos) {
this.apps = infos;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//在listView的首位显示应用程序的个数,用装了应用程序的集合userAppInfos来获取长度
//在该listview处显示的是自定义的控件textview
if(position == 0){
TextView textview = new TextView(parent.getContext());
textview.setBackgroundColor(Color.GRAY);
textview.setText("应用程序:"+userAppInfos.size()+"个");
return textview;
//在listView的第0位(listview下标从0开始)加上应用程序的个数再+1的长度处显示系统程序的个数
//在该listView处显示的是自定义的控件textView
}else if(position == userAppInfos.size()+1){
TextView textview = new TextView(parent.getContext());
textview.setBackgroundColor(Color.GRAY);
textview.setText("系统程序:"+systemAppInfos.size()+"个");
return textview;
}
//布局窗体填充
//当原来的布局窗体不为空时,就把原来的convertView赋给view,并配合holder,从holder里取出之前
//定义过的控件变量
if(convertView!=null && convertView instanceof LinearLayout){
view = convertView;
holder = (ViewHolder) view.getTag();
//否则,就把布局文件注入进来,并获取布局文件中的控件变量,并赋予holder去管理
}else{
view = View.inflate(AppManagerActivity.this,
R.layout.item_app_manager, null);
holder = new ViewHolder();
holder.iv_icon = (ImageView) view.findViewById(R.id.iv_icon);
holder.tv_app_location = (TextView) view.findViewById(R.id.tv_app_location);
holder.tv_app_name = (TextView) view.findViewById(R.id.tv_app_name);
holder.tv_app_size = (TextView) view.findViewById(R.id.tv_app_size);
holder.tv_app_path = (TextView) view.findViewById(R.id.tv_app_path);
view.setTag(holder);
}
//每一次滑动listview就获取一个单元的对象,赋给appinfo(自定义的用来存储app信息的对象)
//appInfo = apps.get(position);
//以下是给布局文件复制的操作
//判断
//根据装应用程序的集合或者装系统程序的集合个数去获取集合里的对象并放进对应的position里去
if(position < userAppInfos.size() + 1){
appInfo = userAppInfos.get(position -1 );
}else if(position >= userAppInfos.size() + 1 && position <=(userAppInfos.size()+systemAppInfos.size()+2)){
appInfo = systemAppInfos.get(position -userAppInfos.size()-1-1);
}
//在上面一步确定了在某一listview的position位置该设置什么程序对象了之后,就把对象里的内容放进控件里
holder.iv_icon.setImageDrawable(appInfo.getmDrawable());
if(appInfo.isInRom()){
holder.tv_app_location.setText("手机内存");
}else{
holder.tv_app_location.setText("外部存储");
}
holder.tv_app_name.setText(appInfo.getAppName());
holder.tv_app_size.setText(Formatter.formatFileSize(AppManagerActivity.this, appInfo.getApkSize()));
holder.tv_app_path.setText(appInfo.getApkPath());
return view;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return apps.size()+1+1;
}
@Override
public Object getItem(int position) {
//该位置已经用来显示应用程序的个数了
if(position == 0){
return 0 ;
}
//该位置已经用来显示系统程序的个数了
if(position == userAppInfos.size() + 1 ){
return null;
}
//从position=1开始到应用程序个数+1为止,我们就应用程序集合中拿出对象,在集合中是从下标0开始拿取
if(position< userAppInfos.size()+1){
appInfo = userAppInfos.get(position - 1 );
//其他情况的position如何获取系统程序集合中的系统程序对象
}else{
appInfo = systemAppInfos.get(position -2 - userAppInfos.size());
}
return appInfo;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
}
}
获得系统和用户应用的信息
AppInfoparser.java
public class AppInfoparser {
public static List<AppInfo> getAppInfo(Context context){
//获取到安装包的管理者
PackageManager pm = context.getPackageManager();
//获取到安装包的所有基本程序的信息
List<PackageInfo> infos = pm.getInstalledPackages(0);
//初始化lists
List<AppInfo> lists = new ArrayList<AppInfo>();
//把安装包集合分别拿出来,然后封装到AppInfo对象里去
for(PackageInfo info : infos){
AppInfo appInfo = new AppInfo();
//获取到app的包名
String packageName = info.packageName;
appInfo.setPackageName(packageName);
//获取到icon图标
Drawable icon = info.applicationInfo.loadIcon(pm);
appInfo.setmDrawable(icon);
//获取到应用程序的名字
String appname = (String) info.applicationInfo.loadLabel(pm);
appInfo.setAppName(appname);
//获取到apk的路径
String apkPath = info.applicationInfo.sourceDir;
appInfo.setApkPath(apkPath);
File file = new File(apkPath);
//获取到apk的大小
long apkSize = file.length();
appInfo.setApkSize(apkSize);
int flags = info.applicationInfo.flags;
//判断当前是应用app还是系统app;位运算
if((flags & info.applicationInfo.FLAG_SYSTEM)!= 0 ){
//true 表示是系统应用
appInfo.setUserApp(false);
}else{
//false 表示是用户应用
appInfo.setUserApp(true);
}
//判断当前是app是否安装到哪里
if((flags & info.applicationInfo.FLAG_EXTERNAL_STORAGE)!=0){
//true 表示安装到外部存储sd卡
appInfo.setInRom(false);
}else{
//false 表示安装到机身内存。rom
appInfo.setInRom(true);
}
lists.add(appInfo);
}
return lists;
}
}
资料下载
标签:android开发
原文地址:http://blog.csdn.net/faith_yee/article/details/44921021