Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

api - android: how do i open another app from my app?

I understand how to use intents and startActivity() when opening another activity within my own app, but how do you start a different app? specifically:

  • How do you determine if the user has the needed app installed on their device?
  • How do you start that app?
  • How do you pass parameters to that app?
  • How do you find all this info out for a specific app (say Adobe reader, or google maps)?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

How to see if Intent is available:

  1. Try calling Intent and deal with ActivityNotFoundException if it isn't available

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(path, "application/pdf");
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    
    try {
        startActivity(intent);
    } 
    catch (ActivityNotFoundException e) {
        Toast.makeText(OpenPdf.this, 
            "No Application Available to View PDF", 
            Toast.LENGTH_SHORT).show();
    }
    

    or

  2. Query the Package Manager to see if it is ahead of time:

    PackageManager packageManager = getPackageManager();
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setType("application/pdf");
    
    List list = packageManager.queryIntentActivities(intent,
        PackageManager.MATCH_DEFAULT_ONLY);
    
    if (list.size() > 0) {
        intent.setDataAndType(path, "application/pdf");
        startActivity(intent);
    }
    

How to pass parameters to an application or know its capabilities:

  1. List of Available Intents for Google Applications
  2. List of Intents by 3rd parties @ OpenIntents

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...