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

前端 未结 2 931
说谎
说谎 2020-11-27 04:18

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 d
相关标签:
2条回答
  • 2020-11-27 04:28

    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
    0 讨论(0)
  • 2020-11-27 04:41

    What you are looking for are intents and intent filters.

    Everything you want to know is detailed on the Android developer guide.

    http://developer.android.com/guide/topics/intents/intents-filters.html

    0 讨论(0)
提交回复
热议问题