如何从另一个 android 应用程序调用一个 android 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2728465/
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:06:47  来源:igfitidea点击:

How to call one android application from another android application

android

提问by Akshata

I want to call one android application from another application

我想从另一个应用程序调用一个 android 应用程序

I have tried some examples, but they are not working for me, I'm getting a Package parse error.

我尝试了一些示例,但它们对我不起作用,我收到了包解析错误。

Consider there are two applications: Application1 and Application2

考虑有两个应用程序:Application1 和 Application2

I want to call Application2 from Application1

我想从 Application1 调用 Application2

Here is the some sample code to do this:

这是执行此操作的一些示例代码:

Intent i = new Intent();
i.setAction(android.content.Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(fileName),"application/vnd.android.package-archive"); 
startActivity(i);

Here fileName = "file://data/data/package_name/files/Application1.apk";

这里 fileName = "file://data/data/package_name/files/Application1.apk";

回答by Saio

I think this code will help:

我认为这段代码会有所帮助:

Intent intent = new Intent(Intent.ACTION_RUN);
intent.setComponent(new ComponentName("<packet name>", "<class name>"));
List list = packageManager.queryIntentActivities(intent, packageManager.COMPONENT_ENABLED_STATE_DEFAULT);

if(list.size() > 0)
{
 Log.i("Log", "Have application" + list.size());
 startActivity(intent);
}
else
{
    Log.i("Log", "None application");
}

回答by Keith

Are you trying to launch an application that is not installed? It looks like you're trying to execute an app by passing app1 the location of the .apk file for app2 on the sd card - this won't work. The Android OS will know how to invoke app2 via Intents only after the user has installed it (consider the security risks if you could just invoke any arbitrary code sitting on the sd card).

您是否正在尝试启动未安装的应用程序?看起来您正在尝试通过向 app1 传递 sd 卡上 app2 的 .apk 文件的位置来执行应用程序 - 这将不起作用。Intent只有在用户安装app2之后,Android 操作系统才会知道如何通过s调用它(如果您可以调用任何位于 sd 卡上的任意代码,请考虑安全风险)。

Assuming app1 and app2 are installed, look at the AndroidManifest.xml file for app2. This file will indicate what kind of Intents it will respond to.

假设已安装 app1 和 app2,请查看 app2 的 AndroidManifest.xml 文件。该文件将指示Intent它将响应的类型。

see http://developer.android.com/guide/topics/intents/intents-filters.html#npexfor a good example.

一个很好的例子见http://developer.android.com/guide/topics/intents/intents-filters.html#npex

Please show us the AndroidManifest.xml file for app2 if you need more help.

如果您需要更多帮助,请向我们展示 app2 的 AndroidManifest.xml 文件。

回答by Khushi

Intent i4=new Intent(Intent.ACTION_MAIN);

PackageManager manager = getPackageManager();

i4 = manager.getLaunchIntentForPackage("com.apk");//apk name

i4.addCategory(Intent.CATEGORY_LAUNCHER);

startActivity(i4);

回答by Tan Lee

private void handleCallGooglePlay(Activity mActivity, String packageClass) {
        try {
            mActivity.startActivity(new Intent("android.intent.action.VIEW", Uri.parse("market://details?id=" + packageClass)));
        } catch (Exception var4) {
            mActivity.startActivity(new Intent("android.intent.action.VIEW", Uri.parse(package_name)));  //package name
        }
    }