간단한 안드로이드 런처들 소스를 찾아보면 두가지 종류의 소스를 찾아볼 수 있습니다.
두가지 방법
1. https://github.com/arnabc/simplelauncher2. https://code.tutsplus.com/tutorials/build-a-custom-launcher-on-android--cms-21358
두개의 차이점은 실행할 앱의 목록을 구성시키는 API 구성이 다릅니다.
첫번재 방법
1.번 방식의 소스를 살펴보면 아래 소스내 아래와 같은 부분이 있습니다.https://github.com/arnabc/simplelauncher/blob/master/SimpleLauncher/src/main/java/ch/arnab/simplelauncher/AppsLoader.java
| @Override | |
| public ArrayList<AppModel> loadInBackground() { | |
| // retrieve the list of installed applications | |
| List<ApplicationInfo> apps = mPm.getInstalledApplications(0); | |
| if (apps == null) { | |
| apps = new ArrayList<ApplicationInfo>(); | |
| } | |
| final Context context = getContext(); | |
| // create corresponding apps and load their labels | |
| ArrayList<AppModel> items = new ArrayList<AppModel>(apps.size()); | |
| for (int i = 0; i < apps.size(); i++) { | |
| String pkg = apps.get(i).packageName; | |
| // only apps which are launchable | |
| if (context.getPackageManager().getLaunchIntentForPackage(pkg) != null) { | |
| AppModel app = new AppModel(context, apps.get(i)); | |
| app.loadLabel(context); | |
| items.add(app); | |
| } | |
| } | |
| // sort the list | |
| Collections.sort(items, ALPHA_COMPARATOR); | |
| return items; | |
| } |
mPm.getInstalledApplications(0); 이 함수에의해 전체 설치된 어플의 목록을 가져오게 되고, if (context.getPackageManager().getLaunchIntentForPackage(pkg) != null) 해당 코드에 의해 그중에 실행가능한 어플들만 목록을 채우게 됩니다.
두번째 방법
2. 번의 방식은 아래의 코드에 의해 Intent.CATEGORY_LAUNCHER, queryIntentActivities() 에 의해 Activity의 목록들이 돌아 옵니다.private PackageManager manager;private List<AppDetail> apps; private void loadApps(){ manager = getPackageManager(); apps = new ArrayList<AppDetail>(); Intent i = new Intent(Intent.ACTION_MAIN, null); i.addCategory(Intent.CATEGORY_LAUNCHER); List<ResolveInfo> availableActivities = manager.queryIntentActivities(i, 0); for(ResolveInfo ri:availableActivities){ AppDetail app = new AppDetail(); app.label = ri.loadLabel(manager); app.name = ri.activityInfo.packageName; app.icon = ri.activityInfo.loadIcon(manager); apps.add(app); }}
댓글 없음:
댓글 쓰기