다른 어플 실행하기 및 앱스토어로 이동 참고 소스 :: 소림사의 홍반장!
private final String PLAY_STORE = "https://play.google.com/store/apps/details?id=";
 
@SuppressLint("NewApi")
private void launch3rdApp(String packageName) {
     
    Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    PackageManager pm = getActivity().getPackageManager();
    List<ResolveInfo> installedApps = pm.queryIntentActivities(mainIntent, 0);
     
    boolean installed = false;
    for (ResolveInfo resolveInfo : installedApps) {
        if(resolveInfo.activityInfo.packageName.equals(packageName)) {
            installed = true;
            break;
        }
    }
     
    if(installed) {
     
        //if app is running on background, bring the app to foreground.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { // 11
             
            final ActivityManager activityManager = (ActivityManager) getActivity().getSystemService(Context.ACTIVITY_SERVICE);
                final List<RecentTaskInfo> recentTasks = activityManager.getRecentTasks(Integer.MAX_VALUE, ActivityManager.RECENT_IGNORE_UNAVAILABLE);
             
                RecentTaskInfo recentTaskInfo = null;
             
                    for (int i = 0; i < recentTasks.size(); i++)
                    {
                        if (recentTasks.get(i).baseIntent.getComponent().getPackageName().equals(packageName)) {
                       recentTaskInfo = recentTasks.get(i);
                       break;
                    }
                }
             
                if(recentTaskInfo != null && recentTaskInfo.id > -1) {
                    activityManager.moveTaskToFront(recentTaskInfo.persistentId, ActivityManager.MOVE_TASK_WITH_HOME);
                    return;
                }
            }
         
            //excute app by new task
            Intent intent = getActivity().getPackageManager().getLaunchIntentForPackage(packageName);
            if(intent != null) {
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
    }
    else {//not installed
        callback.showActionView(PLAY_STORE + packageName);
    }
}


다른 카테고리의 글 목록

Dev. 안드로이드/참고소스 카테고리의 포스트를 톺아봅니다