安德鲁斯Launcher得到的装在手机的应用程序列表
Launcher最基本的是讓所有的應用程序和入口圖標的列表。有兩種方法來獲得,一般:
PackageInfo
ResolveInfo
執行獲取全部APP的Launcher而且同意進行點擊事件,進入到應用
以下通過這兩種方法獲取到全部應用的列表:
建立基本數據:
PakageMod.java
public class PakageMod {public String pakageName;public String appName;public Drawable icon;public PakageMod() {super();}public PakageMod(String pakageName, String appName, Drawable icon) {super();this.pakageName = pakageName;this.appName = appName;this.icon = icon;} }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:gravity="center" android:orientation="vertical" > <ImageView android:id="@+id/apps_image" android:layout_width="48dip" android:layout_height="48dip" android:icon="@drawable/ic_launcher" /> <TextView android:id="@+id/apps_textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:maxEms="5" android:maxLines="1" android:text="good" /> </LinearLayout>
以下在Activity中獲取到列表并顯示到GridView中,并點擊進入應用;
使用PackageInfo
public class PackageInfoDemo extends Activity {private GridView gridview;private PackageManager pManager;private List<PakageMod> datas;private String tag = "MainActivity";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);this.setContentView(R.layout.activity_main);// 取得gridviewgridview = (GridView) findViewById(R.id.gridview);// 獲取圖片、應用名、包名pManager = PackageInfoDemo.this.getPackageManager();List<PackageInfo> appList = getAllApps(PackageInfoDemo.this);datas = new ArrayList<PakageMod>();for (int i = 0; i < appList.size(); i++) {PackageInfo pinfo = appList.get(i);PakageMod shareItem = new PakageMod();// 設置圖片shareItem.icon = pManager.getApplicationIcon(pinfo.applicationInfo);// 設置應用程序名字shareItem.appName = pManager.getApplicationLabel(pinfo.applicationInfo).toString();// 設置應用程序的包名shareItem.pakageName = pinfo.applicationInfo.packageName;datas.add(shareItem);}gridview.setAdapter(new baseAdapter(this, datas));// 點擊應用圖標時,做出響應gridview.setOnItemClickListener(new ClickListener());}public static List<PackageInfo> getAllApps(Context context) {List<PackageInfo> apps = new ArrayList<PackageInfo>();PackageManager pManager = context.getPackageManager();// 獲取手機內全部應用List<PackageInfo> packlist = pManager.getInstalledPackages(0);for (int i = 0; i < packlist.size(); i++) {PackageInfo pak = (PackageInfo) packlist.get(i);// if()里的值假設<=0則為自己裝的程序。否則為系統project自帶if ((pak.applicationInfo.flags & pak.applicationInfo.FLAG_SYSTEM) <= 0) {// 加入自己已經安裝的應用程序// apps.add(pak);}apps.add(pak);}return apps;}private class ClickListener implements OnItemClickListener {@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { Intent intent = new Intent(); intent = PackageInfoDemo.this.getPackageManager() .getLaunchIntentForPackage(datas.get(position).pakageName); startActivity(intent); } } }
使用ResolveInfo
public class ResolveInfoDemo extends Activity {private GridView gridview;private PackageManager pManager;private List<PakageMod> datas;private String tag = "ResolveInfoDemo";@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);this.setContentView(R.layout.activity_main);// 取得gridviewgridview = (GridView) findViewById(R.id.gridview);// 獲取圖片、應用名、包名pManager = this.getPackageManager();datas = new GetAllApps(this).getDatas();gridview.setAdapter(new DemoGridAdapter(this, datas));gridview.setOnItemClickListener(new ClickListener());}// 當用戶點擊應用程序圖標時,將對這個類做出響應private class ClickListener implements OnItemClickListener {@Overridepublic void onItemClick(AdapterView<?> adapterView, View view, int arg2,long arg3) {Intent intent = new Intent();intent = getPackageManager().getLaunchIntentForPackage(datas.get(arg2).pakageName);startActivity(intent);}} }GetAllApps.java
這里getDrawableForDensity須要是15版本號以上的SDK支持。全部低版本號的Launcher不能夠使用;
本博文使用的兩種方法都不須要配置不論什么權限。
Android4.2的Launcher中使用ResolveInfo進行獲取所用的應用列表,這里的ResolveInfo的Demo也是從Launcher源代碼中抄出來的;
也許還有第3種方法.......待續;
本文來自于CSDN博客,轉載請聯系作者;
注明出處http://blog.csdn.net/dreamintheworld/article/details/39718581
版權聲明:本文博客原創文章,博客,未經同意,不得轉載。
轉載于:https://www.cnblogs.com/gcczhongduan/p/4755759.html
總結
以上是生活随笔為你收集整理的安德鲁斯Launcher得到的装在手机的应用程序列表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 编写高质量代码改善C#程序的157个建议
- 下一篇: Objective-C:随机的读取文件中