android:如何从我的应用程序打开另一个应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2923265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:54:59  来源:igfitidea点击:

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

androidapiandroid-activity

提问by mtmurdock

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:

我了解在我自己的应用程序中打开另一个 Activity 时如何使用 Intent 和 startActivity(),但是如何启动不同的应用程序?具体来说:

  • 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)?
  • 您如何确定用户是否在其设备上安装了所需的应用程序?
  • 你如何启动那个应用程序?
  • 您如何将参数传递给该应用程序?
  • 您如何找到特定应用程序(例如 Adob​​e 阅读器或谷歌地图)的所有这些信息?

回答by Tim Kryger

How to see if Intent is available:

如何查看 Intent 是否可用:

  1. Try calling Intent and deal with ActivityNotFoundExceptionif 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 Managerto 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);
    }
    
  1. 尝试调用 Intent 并处理ActivityNotFoundException它是否不可用

    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();
    }
    

    或者

  2. 查询包管理器看是否提前:

    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
  1. Google 应用程序的可用 Intent 列表
  2. 第 3 方 @ OpenIntents 的意图列表

回答by HXCaine

What you are looking for are intentsand intent filters.

您正在寻找的是意图意图过滤器

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

您想知道的一切都在 Android 开发人员指南中详细说明。

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

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