How to launch an Activity from another Application in Android

后端 未结 12 1427
春和景丽
春和景丽 2020-11-21 06:02

I want to launch an installed package from my Android application. I assume that it is possible using intents, but I didn\'t find a way of doing it. Is there a link, where t

12条回答
  •  攒了一身酷
    2020-11-21 06:36

    // check for the app if it exist in the phone it will lunch it otherwise, it will search for the app in google play app in the phone and to avoid any crash, if no google play app installed in the phone, it will search for the app in the google play store using the browser : 
    
     public void onLunchAnotherApp() {
    
            final String appPackageName = getApplicationContext().getPackageName();
    
            Intent intent = getPackageManager().getLaunchIntentForPackage(appPackageName);
            if (intent != null) {
    
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
    
            } else {
    
                onGoToAnotherInAppStore(intent, appPackageName);
    
            }
    
        }
    
        public void onGoToAnotherInAppStore(Intent intent, String appPackageName) {
    
            try {
    
                intent = new Intent(Intent.ACTION_VIEW);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setData(Uri.parse("market://details?id=" + appPackageName));
                startActivity(intent);
    
            } catch (android.content.ActivityNotFoundException anfe) {
    
                intent = new Intent(Intent.ACTION_VIEW);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setData(Uri.parse("http://play.google.com/store/apps/details?id=" + appPackageName));
                startActivity(intent);
    
            }
    
        }
    

提交回复
热议问题