마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

앱에서 앱으로 연동하는 방법이 궁금합니다.

0 추천
조건사항과 연동앱을 등록하고

조건사항을 만족하면 연동앱을 바로 실행하도록 하는 앱을 작성중인데

 

앱 설정에서의 '다운로드 앱' 형식으로 리스트를 띄우는 방법을 사용하려는데

앱 목록을 열고 앱을 선택할 수 있도록 하는 방법을 모르겠습니다...

구글이나 네이버에서 이런 뷰나 인텐드 사용법을 찾으려면 어떻게 검색해야하는지도 이미 머리가 아프고

괜히 욕심만 나선건 아니길 바라는데...

 

혹시 앱 목록을 불러오고 특정 단일앱을 선택하는 뷰나 기능이 권한조건같은게 빡빡한 편인가요?
익명사용자 님이 2015년 11월 30일 질문

1개의 답변

0 추천
 
채택된 답변
설치된 앱 목록은 PackageManager의 getInstalledApplications 메서드를 사용하시면 설치된 앱 목록을 가져올 수 있습니니다.

또는 아래와 같은 코드로 런칭할 수 있는 앱들의 정보를 가져올 수 있습니다.

final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PackageManager pm = context.getPackageManager();
List<resolveinfo> installedApps = pm.queryIntentActivities(mainIntent, 0);
 
for (ResolveInfo ai : installedApps) {
    Log.d("tag", ai.activityInfo.packageName);
}

 

그리고 실행은 아래와 같이 패키지 이름으로 실행시킬 수 있습니다.

Intent intent = context.getPackageManager().getLaunchIntentForPackage("ParkageName");
startActivity(intent);
mcsong (44,040 포인트) 님이 2015년 12월 1일 답변
혹시 저 소스가 사용되는 예제 파일 어디서 구할 수 있는지 알려주실 수 있을까요?
아래는 제가 사용하는 소스인데요.. 참고해 보세요..


package net.sjava.file.viewmodel;

import android.content.pm.ApplicationInfo;

/**
 *
 * Created by mcsong@gmail.com on 2014-01-08.
 */
public class AppInfoViewModel implements ViewModelable {
    private ApplicationInfo appInfo;

    private String appName;
    private String appVersionName;

    public boolean isUserApp;
    public boolean isLaunchable;

    private long size;
    private long modified;

    private AppInfoViewModel(Builder builder) {
        this.appInfo = builder.appInfo;
        this.appName = builder.appName;
        this.appVersionName = builder.appVersionName;
        this.isUserApp = builder.isUserApp;
        this.isLaunchable = builder.isLaunchable;
        this.size = builder.size;
        this.modified = builder.modified;
    }

    public ApplicationInfo getAppInfo() {
        return appInfo;
    }

    public String getAppName() {
        return appName;
    }

    public String getAppVersionName() {
        return appVersionName;
    }

    public boolean isUserApp() {
        return isUserApp;
    }

    public boolean isLaunchable() {
        return isLaunchable;
    }

    public long getSize() {
        return size;
    }

    public long getModified() {
        return modified;
    }

    @Override
    public String toString() {
        return "AppInfoViewModel{" +
                "appInfo=" + appInfo +
                ", appName='" + appName + '\'' +
                ", appVersionName='" + appVersionName + '\'' +
                ", isUserApp=" + isUserApp +
                ", size=" + size +
                ", modified=" + modified +
                '}';
    }

    public static class Builder {
        private ApplicationInfo appInfo;
        private String appName;
        private String appVersionName;

        private boolean isUserApp;
        private boolean isLaunchable;

        private long size;
        private long modified;

        public Builder(ApplicationInfo appInfo, String appName) {
            this.appInfo = appInfo;
            this.appName = appName;
        }

        public Builder appVersionName(String val) {
            this.appVersionName = val;
            return this;
        }

        public Builder isUserApp(boolean val) {
            this.isUserApp = val;
            return this;
        }

        public Builder isLaunchable(boolean val) {
            this.isLaunchable = val;
            return this;
        }

        public Builder size(long val) {
            this.size = val;
            return this;
        }

        public Builder modified(long val) {
            this.modified = val;
            return this;
        }

        public AppInfoViewModel build() {
            return new AppInfoViewModel(this);
        }
    }

}




import java.io.File;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import android.util.Log;

import net.sjava.file.viewmodel.AppInfoViewModel;
import net.sjava.file.ui.type.SortType;

/**
 * Created by mcsong@gmail.com on 1/3/2014.
 *
 */
public class LocalAppInstalledProvider {
    private SortType mSortType;
    private PackageManager mPackageManager;

    private List<AppInfoViewModel> launchableAppViewModels;
    private List<AppInfoViewModel> nonLaunchableAppViewModels;

    // Enum Sort type
    public LocalAppInstalledProvider(Context mContext, SortType mSortType) {
        this.mPackageManager = mContext.getPackageManager();
        this.mSortType = mSortType;

        this.build();
    }

    public List<AppInfoViewModel> getItems() {

        final Collator collator = Collator.getInstance(Locale.getDefault());
        if(launchableAppViewModels.size() > 1 ) {
            Collections.sort(launchableAppViewModels, new Comparator<AppInfoViewModel>() {
                @Override
                public int compare(AppInfoViewModel c1, AppInfoViewModel c2) {
                    return collator.compare(c1.getAppName(), c2.getAppName());
                }
            });
        }

        if(nonLaunchableAppViewModels.size() > 1 ) {
            Collections.sort(nonLaunchableAppViewModels, new Comparator<AppInfoViewModel>() {
                @Override
                public int compare(AppInfoViewModel c1, AppInfoViewModel c2) {
                    return collator.compare(c1.getAppName(), c2.getAppName());
                }
            });
        }

        List<AppInfoViewModel> apps = new ArrayList<>(launchableAppViewModels.size() + nonLaunchableAppViewModels.size());
        if(mSortType == SortType.Alphabetical) {
            apps.addAll(launchableAppViewModels);
            apps.addAll(nonLaunchableAppViewModels);
            return apps;
        }

        // reverse 1번 2번 3번 버그.

        Collections.reverse(launchableAppViewModels);
        Collections.reverse(nonLaunchableAppViewModels);

        apps.addAll(launchableAppViewModels);
        apps.addAll(nonLaunchableAppViewModels);

        return apps;
    }

    public void setmSortType(SortType mSortType) {
        this.mSortType = mSortType;
    }

    private void build() {
        //final PackageManager pm = mContext.getPackageManager();

        //get a list of installed apps.
        //addedItems = pm.getInstalledApplications(PackageManager.GET_META_DATA);
        String appName = "";
        String appVersionName = "1.0";

        File apkFile;

        launchableAppViewModels = new ArrayList<>();
        nonLaunchableAppViewModels = new ArrayList<>();

        //addedItems = new ArrayList<>();
        List<ApplicationInfo> apps = mPackageManager.getInstalledApplications(PackageManager.GET_META_DATA);

        for (ApplicationInfo appInfo : apps) {

            // 이걸로 간다.
            //Log.d("aa", "C : " + appInfo.loadLabel(pm).toString());

            apkFile = new File(appInfo.publicSourceDir);

            try {
                appName = appInfo.loadLabel(mPackageManager).toString();
            } catch(Exception e) {
                e.printStackTrace();
            }

            appVersionName = getAppVersion(appInfo.packageName);

            AppInfoViewModel.Builder builder = new AppInfoViewModel.Builder(appInfo, appName);
            builder.appVersionName(appVersionName);
            builder.isUserApp(isUserApp(appInfo));
            builder.isLaunchable(isLaunchableApp(mPackageManager, appInfo));
            builder.size(apkFile.length());
            builder.modified(apkFile.lastModified());
            AppInfoViewModel appInfoViewModel = builder.build();

            if(appInfoViewModel.isLaunchable)
                launchableAppViewModels.add(appInfoViewModel);
            else
                nonLaunchableAppViewModels.add(appInfoViewModel);
        }
    }

    private String getAppVersion(String className) {
        try {
            String version = mPackageManager.getPackageInfo(className, 0).versionName;
            if(TextUtils.isEmpty(version))
                return "";

            return version.split("-")[0].split("_")[0];
        } catch (PackageManager.NameNotFoundException e) {
            Log.e("AA", "Package name not found", e);
        }

        return "";
    }


    public static boolean isUserApp(ApplicationInfo applicationInfo) {
        if(applicationInfo == null)
            return false;

        return applicationInfo.sourceDir.startsWith("/data/app/");
    }

    public static boolean isSystemApp(ApplicationInfo applicationInfo) {
        if(applicationInfo == null)
            return false;

        return applicationInfo.sourceDir.toLowerCase().startsWith("/system/");
    }

    public static boolean isLaunchableApp(PackageManager pm, ApplicationInfo applicationInfo) {
        if(pm == null || applicationInfo == null)
            return false;

        if (pm.getLaunchIntentForPackage(applicationInfo.packageName) != null)
            return true;

        return false;
    }

}
...