找包名的应用程序共享意向 [英] Get package name for the application share intent

查看:108
本文介绍了找包名的应用程序共享意向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得用于创建共享意向的应用程序名称或包名。我正在接收通过意向共享数据的应用程序,我怎么能拿器应用的包名?像[intent.getSourceBackageName()]

I would like to get the application name or its package name that create the shared intent. I'm making application that receive shared data via intent, how could I get the package name of the creator application? something like [intent.getSourceBackageName()]

编辑:
林不知道如果我的问题是清楚的。但我需要找出打电话给我,例如,如果我从浏览器份额,我需要找到一种方法来知道浏览器应用程序的应用程序的名称是共享数据的应用程序和我在一起。

Im not sure if my question is clear. But I need to find out the name of the app that called mine, for example, if I share from Browser, I need to find a way to know that Browser app is the application which share data with me.

推荐答案

获取的开始你的活动应用程序的包名称:

Getting the package-name of the app that started your Activity:

/**
 * 
 * @param activity
 * @return The calling application's package name or {@code null} if the activity was not
 *         started with {@link #startActivityForResult}
 */
public static String getCallingPackageName(Activity activity) {
    return activity.getCallingPackage();
}

获取该开始你的活动中的应用程序名称:

Getting the app name that started your Activity:

/**
 * Get the name of the app that started this Activity.
 * 
 * @param activity
 * @return The calling application's app name or {@code null} if the activity was not started
 *         with {@link #startActivityForResult}
 */
public static String getCallingAppName(Activity activity) {
    final String packageName = activity.getCallingPackage();
    if (packageName == null) {
        return null;
    }
    final PackageManager pm = activity.getPackageManager();
    try {
        return pm.getApplicationInfo(packageName, 0).loadLabel(pm).toString();
    } catch (NameNotFoundException ignored) {
    }
    return null;
}


注:

通过创建具有以下意图过滤器的活动测试:

Tested by creating an activity with the following intent filter:

<intent-filter>
    <action android:name="android.intent.action.GET_CONTENT" />

    <data android:mimeType="*/*" />

    <category android:name="android.intent.category.OPENABLE" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

开启Gmail - >组成一​​个新的消息,并选择附加文件 - >选择测试程序

Opened Gmail -> composed a new message and selected "Attach file" -> chose the test app.

getCallingAppName(本)返回的文件

getCallingPackageName(本)返回的 com.android.documentsui

在我的情况下,因为Gmail默认打开的文档返回文档,而不是Gmail中。所以,你应该考虑到它可能无法真正知道原来的应用程序和结果总是除非你的活动调用了 startActivityForResult

In my case it returned Documents instead of Gmail because Gmail opens Documents by default. So, you should consider that it may be impossible to really know the original app and the result will always be null unless your Activity was called with startActivityForResult

这篇关于找包名的应用程序共享意向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆