管理手机上已安装的程序,如何只显示用户自己安装的程序?
程序大概分成三个部分:
1.获取手机已安装的所有应用package的信息(其中包括用户自己安装的,还有系统自带的);
2.滤除系统自带应用;
3.通过列表显示出应用程序的图标(icon),和其他文字信息(应用名称,包名称package name,版本号等等)。
首先,定义一个数据结构,来保存应用程序信息(icon,name,packageName,versionName,versionCode,等):
package com.mainroadlee.appdisplayer; import android.graphics.drawable.Drawable; import android.util.Log; /** * 定义一个数据结构,保存应用程序信息(icon,name,packageName,versionName,versionCode)等等。 * * @version 2012-09-11 */ public class AppInfo { public String appName = ""; public String packageName = ""; public String versionName = ""; public int versionCode = 0; public Drawable appIcon = null; public void print() { Log.v("app", "Name:" + appName + " Package:" + packageName); Log.v("app", "Name:" + appName + " versionName:" + versionName); Log.v("app", "Name:" + appName + " versionCode:" + versionCode); } }
然后,通过PackageManager来获取已安装的所有程序:
// 使用列表存储已安装的非系统应用程序 ArrayList<AppInfo> appList = new ArrayList<AppInfo>(); // 获取手机已安装的所有应用package的信息(其中包括用户自己安装的,还有系统自带的) List<PackageInfo> packages = getPackageManager().getInstalledPackages(0);
判断一个程序是否属于系统程序还是自己安装的程序:
if((packageInfo.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM)==0) { //非系统应用 } else { //系统应用 }
完整代码:
package com.mainroadlee.appdisplayer; import java.util.ArrayList; import java.util.List; import com.mainroadlee.appdisplayer.R; import android.app.Activity; import android.content.Context; import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; public class MainActivity extends Activity { // 使用列表存储已安装的非系统应用程序 ArrayList<AppInfo> appList = new ArrayList<AppInfo>(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 获取手机已安装的所有应用package的信息(其中包括用户自己安装的,还有系统自带的) List<PackageInfo> packages = getPackageManager() .getInstalledPackages(0); for (int i = 0; i < packages.size(); i++) { PackageInfo packageInfo = packages.get(i); AppInfo tmpInfo = new AppInfo(); tmpInfo.appName = packageInfo.applicationInfo.loadLabel( getPackageManager()).toString(); tmpInfo.packageName = packageInfo.packageName; tmpInfo.versionName = packageInfo.versionName; tmpInfo.versionCode = packageInfo.versionCode; tmpInfo.appIcon = packageInfo.applicationInfo .loadIcon(getPackageManager()); // 如果属于非系统程序,则添加到列表显示 if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { appList.add(tmpInfo); } } for (int i = 0; i < appList.size(); i++) { appList.get(i).print(); } // 获取ListView并且绑定适配器 ListView appListView = (ListView) findViewById(R.id.listview); AppAdapter appAdapter = new AppAdapter(MainActivity.this, appList); appListView.setDividerHeight(5); if (appListView != null) { appListView.setAdapter(appAdapter); } } /** * 自定义BaseAdapter */ public class AppAdapter extends BaseAdapter { Context context; ArrayList<AppInfo> dataList = new ArrayList<AppInfo>(); public AppAdapter(Context context, ArrayList<AppInfo> inputDataList) { this.context = context; dataList.clear(); for (int i = 0; i < inputDataList.size(); i++) { dataList.add(inputDataList.get(i)); } } @Override public int getCount() { return dataList.size(); } @Override public Object getItem(int position) { return dataList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { final AppInfo appUnit = dataList.get(position); if (convertView == null) { LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = vi.inflate(R.layout.app_row, null); convertView.setClickable(true); } TextView appNameText = (TextView) convertView .findViewById(R.id.appName); ImageView appIcon = (ImageView) convertView.findViewById(R.id.icon); if (appNameText != null) appNameText.setText(appUnit.appName); if (appIcon != null) appIcon.setImageDrawable(appUnit.appIcon); return convertView; } } }
布局文件:
main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/title_info" /> <ListView android:id="@+id/listview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_margin="6dp" android:cacheColorHint="#00000000" android:clickable="true" android:divider="#00000000" android:drawSelectorOnTop="false" android:scrollbars="vertical" /> </LinearLayout>
app_row.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="fill_parent" android:orientation="horizontal" > <ImageView android:id="@+id/icon" android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/icon" /> <TextView android:id="@+id/appName" android:layout_width="fill_parent" android:layout_height="50dp" android:gravity="center" android:textColor="#FFFFFF" android:textSize="20sp" /> </LinearLayout>Android开发中如何获取用户安装的应用程序信息 转载https://www.codesocang.com/appboke/33584.html
热门源码