主要的功能
  • 自定义Button(TextView来做Button)
  • 通过点击不同的Button显示系统程序和应用程序
  • 更改ListView选中时的背景色
PackageManager的功能:
  •安装,卸载应用
  •查询permission相关信息
  •查询Application相关信息(application,activity,receiver,service,provider及相应属性等)
  •查询已安装应用
  •增加,删除permission
  •清除用户数据、缓存,代码段等
先看看效果图,风格可以自己调整


代码就暂时不特别规范的写了,只是测试程序

main.xml,整体布局,默认TextView是没有事件监听的,需要设置其android:clickable属性为true

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:background="@drawable/background_color"
  6. android:orientation="vertical" >
  7. <LinearLayout
  8. android:layout_width="fill_parent"
  9. android:layout_height="10dip"
  10. />
  11. <LinearLayout
  12. android:layout_width="fill_parent"
  13. android:layout_height="40dip"
  14. >
  15. <TextView
  16. android:id="@+id/button01"
  17. android:layout_width="fill_parent"
  18. android:layout_height="fill_parent"
  19. android:layout_weight="1"
  20. android:text="普通应用"
  21. android:gravity="center"
  22. android:background="@drawable/button_selector"
  23. android:focusable="true"
  24. android:clickable="true"
  25. android:layout_marginLeft="10dip"
  26. />
  27. <View android:layout_width="5px" android:layout_height="fill_parent"
  28. android:background="#FFFFFFFF"/>
  29. <TextView
  30. android:id="@+id/button02"
  31. android:layout_width="fill_parent"
  32. android:layout_height="fill_parent"
  33. android:layout_weight="1"
  34. android:text="系统应用"
  35. android:gravity="center"
  36. android:background="@drawable/button_selector"
  37. android:focusable="true"
  38. android:clickable="true"
  39. android:layout_marginRight="10dip"
  40. />
  41. </LinearLayout>
  42. <ListView
  43. android:id="@+id/lv"
  44. android:layout_width="fill_parent"
  45. android:layout_height="wrap_content"
  46. android:fastScrollEnabled="true">

  47. </ListView>"
  48. </LinearLayout>

ListView的item布局:listitem.xml,这里ImageView的大小最好设置为固定的,如果是wrap_content的话,不同应用程序的图标不一样大,显示出来很难看

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="@drawable/list_item_color_bg"
  6. android:orientation="horizontal" >
  7. <ImageView
  8. android:id="@+id/appicon"
  9. android:layout_width="48dip"
  10. android:layout_height="48dip"
  11. android:padding="5dp"
  12. />
  13. <LinearLayout
  14. android:orientation="vertical"
  15. android:layout_width="fill_parent"
  16. android:layout_height="wrap_content"
  17. >
  18. <TextView
  19. android:id="@+id/appName"
  20. android:layout_width="fill_parent"
  21. android:layout_height="wrap_content"
  22. />
  23. <TextView
  24. android:id="@+id/packageName"
  25. android:layout_width="fill_parent"
  26. android:layout_height="wrap_content"
  27. />
  28. </LinearLayout>
  29. </LinearLayout>

下面的是drawable下的一些文件
background_color.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" >
  3. <gradient
  4. android:startColor="#FFFFFFFF"
  5. android:endColor="#FFFFFFFF"
  6. android:angle="270.0"
  7. android:centerY="0.3"
  8. android:centerColor="#FFBDBDBD"
  9. />
  10. </shape>

button_color.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" >
  3. <gradient
  4. android:startColor="#FF7F7F7F"
  5. android:endColor="#FF000000"
  6. android:angle="270.0"
  7. />
  8. </shape>

button_selector.xml,这是点击TextView自定义的Button的selector文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:constantSize="true" >
  4. <!-- 获得焦点时的背景图片 -->
  5. <item
  6. android:state_focused="true"
  7. >
  8. <shape>
  9. <gradient
  10. android:startColor="#FFE5CF33"
  11. android:endColor="#FFF1E7A2"
  12. android:angle="90.0"
  13. />
  14. </shape>
  15. </item>
  16. <!-- 设置响应所有事件 -->
  17. <item android:state_enabled="true"
  18. android:state_pressed="false"
  19. >
  20. <shape>
  21. <gradient
  22. android:startColor="#FF1B1B1B"
  23. android:endColor="#FF969696"
  24. android:angle="90.0"
  25. />
  26. </shape>
  27. </item>
  28. <!-- 按钮点击时候的背景 -->
  29. <item android:state_enabled="true"
  30. android:state_pressed="true">
  31. <shape>
  32. <gradient
  33. android:startColor="#FF000000"
  34. android:endColor="#FF474747"
  35. android:angle="90.0"
  36. />
  37. </shape>
  38. </item>
  39. <item android:state_enabled="false"
  40. android:state_pressed="true">
  41. <shape>
  42. <gradient
  43. android:startColor="#FF000000"
  44. android:endColor="#ff474747"
  45. android:angle="90.0"
  46. />
  47. </shape>
  48. </item>
  49. <!-- 默认情况下的背景 -->
  50. <item>
  51. <shape>
  52. <gradient
  53. android:startColor="#FF000000"
  54. android:endColor="#FF474747"
  55. android:angle="90.0"
  56. />
  57. </shape>
  58. </item>
  59. </selector>

list_item_color_bg.xml,这是点击ListView时item自定义的选中背景

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android" >
  3. <item
  4. android:state_pressed="true"
  5. android:drawable="@color/green"
  6. ></item>
  7. <item
  8. android:drawable="@color/white"
  9. ></item>
  10. </selector>

这里有几点注意

  • 点击不同按钮后需要切换不同的视图,此时由于数据源已经改变,所以需要点击后立即变成改变后的结果,所以需要刷新ListView,使用adapter.notifyDataSetChanged()方法即可刷新ListView
  • 系统应用程序和普通应用程序我放到了不同的ArrayList中了,如果感觉这部分设计有问题的话,希望高手指点

  1. package com.loulijun.appshow;

  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;

  6. import android.app.Activity;
  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.os.Bundle;
  12. import android.view.LayoutInflater;
  13. import android.view.View;
  14. import android.view.ViewGroup;
  15. import android.widget.BaseAdapter;
  16. import android.widget.ImageView;
  17. import android.widget.ListView;
  18. import android.widget.TextView;
  19. import android.widget.Toast;
  20. public class AppShowActivity extends Activity {
  21. /** Called when the activity is first created. */
  22. private TextView customAppsBtn, systemAppsBtn;
  23. private ListView lv;
  24. private List<PackageInfo> customApps; // 普通应用程序
  25. private List<PackageInfo> systemApps; // 系统应用程序
  26. MyAdapter adapter;
  27. @Override
  28. public void onCreate(Bundle savedInstanceState) {
  29. super.onCreate(savedInstanceState);
  30. setContentView(R.layout.main);
  31. customAppsBtn = (TextView)findViewById(R.id.button01);
  32. systemAppsBtn = (TextView)findViewById(R.id.button02);
  33. lv = (ListView)findViewById(R.id.lv);
  34. //加载系统应用和普通应用程序
  35. loadApps();
  36. adapter = new MyAdapter(this, customApps);
  37. lv.setAdapter(adapter);

  38. customAppsBtn.setOnClickListener(new TextView.OnClickListener()
  39. {
  40. @Override
  41. public void onClick(View v) {
  42. Toast.makeText(getApplicationContext(), "普通应用", Toast.LENGTH_SHORT).show();
  43. //切换到普通程序列表
  44. updateAppList(customApps);
  45. }

  46. });
  47. systemAppsBtn.setOnClickListener(new TextView.OnClickListener()
  48. {
  49. @Override
  50. public void onClick(View v) {
  51. Toast.makeText(getApplicationContext(), "系统应用", Toast.LENGTH_SHORT).show();
  52. //切换到系统程序列表
  53. updateAppList(systemApps);
  54. }

  55. });
  56. }

  57. //列出普通应用程序
  58. private void loadApps()
  59. {

  60. customApps = new ArrayList<PackageInfo>();
  61. systemApps = new ArrayList<PackageInfo>();
  62. //得到PackageManager对象
  63. PackageManager pm = this.getPackageManager();
  64. //得到系统安装的所有程序包的PackageInfo对象
  65. List<PackageInfo> packages = pm.getInstalledPackages(0);
  66. for(PackageInfo pi:packages)
  67. {
  68. //列出普通应用
  69. if((pi.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM)<=0)
  70. {
  71. customApps.add(pi);
  72. }
  73. //列出系统应用,总是感觉这里设计的有问题,希望高手指点
  74. if((pi.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM)>0)
  75. {
  76. systemApps.add(pi);
  77. }
  78. }
  79. }

  80. private void updateAppList(List<PackageInfo> apps)
  81. {
  82. adapter.setData(apps);
  83. adapter.notifyDataSetChanged();
  84. }

  85. //ViewHolder静态类
  86. static class ViewHolder
  87. {
  88. public ImageView appicon;
  89. public TextView appName;
  90. public TextView packageName;
  91. }

  92. class MyAdapter extends BaseAdapter
  93. {
  94. private LayoutInflater mInflater = null;
  95. private List<PackageInfo> apps;
  96. private MyAdapter(Context context, List<PackageInfo> apps)
  97. {
  98. this.mInflater = LayoutInflater.from(context);
  99. this.apps = apps;
  100. }
  101. //setData()要在MyAdapter类里设置,用于设置数据源
  102. public void setData(List<PackageInfo> apps)
  103. {
  104. this.apps = apps;
  105. }
  106. @Override
  107. public int getCount() {
  108. // TODO Auto-generated method stub
  109. return customApps.size();
  110. }
  111. @Override
  112. public Object getItem(int position) {
  113. // TODO Auto-generated method stub
  114. return position;
  115. }
  116. @Override
  117. public long getItemId(int position) {
  118. // TODO Auto-generated method stub
  119. return position;
  120. }
  121. @Override
  122. public View getView(int position, View convertView, ViewGroup parent) {
  123. ViewHolder holder = null;


  124. if(convertView == null)
  125. {
  126. holder = new ViewHolder();

  127. convertView = mInflater.inflate(R.layout.list_item, null);
  128. holder.appicon = (ImageView)convertView.findViewById(R.id.appicon);
  129. holder.appName = (TextView)convertView.findViewById(R.id.appName);
  130. holder.packageName = (TextView)convertView.findViewById(R.id.packageName);
  131. convertView.setTag(holder);
  132. }else
  133. {
  134. holder = (ViewHolder)convertView.getTag();
  135. }
  136. PackageManager pm = getPackageManager(); // 得到pm对象
  137. PackageInfo info = apps.get(position);
  138. ApplicationInfo appInfo = info.applicationInfo;

  139. holder.appicon.setImageDrawable(pm.getApplicationIcon(appInfo));
  140. holder.appName.setText(pm.getApplicationLabel(appInfo));
  141. holder.packageName.setText(appInfo.packageName);
  142. return convertView;
  143. }

  144. }
  145. }

更多相关文章

  1. Android(安卓)属性系统(翻译)
  2. selector修改TextView点击后的颜色属性
  3. android 点击EditText 弹出日期选择器DatePickerDialog
  4. Android发送信息模拟系统
  5. Android系统的启动流程(转载)
  6. Android系统的四大组件详解
  7. 【Android话题-2.5系统服务】ServiceManager启动和工作原理是怎
  8. Android(安卓)7.1 系统Setting界面增加返回机制
  9. android 击缩略图查看大图

随机推荐

  1. Android版本名和API Level对应关系
  2. Android Studio第十八期 - Snaphelper
  3. 移动互联网盈利知识汇总
  4. 如何解决Android的SDK与ADT不匹配问题
  5. android,进入页面textview默认获得焦点问
  6. android permission Suggestion: add 'to
  7. android更新
  8. Cocos2d-x Lua实现从Android回调到Lua的
  9. 解决 Android SDK下载和更新失败“Connec
  10. Android 创建全局变量和Context