如何获取Android应用程序ID?

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

How to get Android application id?

android

提问by arams

In Android, how do I get the application's id programatically (or by some other method), and how can I communicate with other applications using that id?

在 Android 中,如何以编程方式(或通过其他方法)获取应用程序的 id,以及如何使用该 id 与其他应用程序通信?

回答by Jeremie

If your are looking for the value defined by applicationIdin gradle, you can simply use

如果您正在寻找applicationId在 gradle 中定义的值,您可以简单地使用

BuildConfig.APPLICATION_ID 

回答by Gaurav Vaish

If by application id, you're referring to package name, you can use the method Context::getPackageName(http://http://developer.android.com/reference/android/content/Context.html#getPackageName%28%29).

如果通过application id,您指的是package name,则可以使用该方法Context::getPackageNamehttp://http://http://developer.android.com/reference/android/content/Context.html#getPackageName%28%29)。

In case you wish to communicate with other application, there are multiple ways:

如果您希望与其他应用程序通信,有多种方式:

  1. Start an activity of another application and send data in the "Extras" of the "Intent"
  2. Send a broadcast with specific action/category and send data in the extras
  3. If you just need to share structured data, use content provider
  4. If the other application needs to continuously run in the background, use Server and "bind" yourself to the service.
  1. 启动另一个应用程序的 Activity 并在“Intent”的“Extras”中发送数据
  2. 发送具有特定动作/类别的广播并在附加内容中发送数据
  3. 如果您只需要共享结构化数据,请使用内容提供程序
  4. 如果其他应用程序需要在后台持续运行,请使用 Server 并将自己“绑定”到该服务。

If you can elaborate your exact requirement, the community will be able to help you better.

如果您可以详细说明您的确切要求,社区将能够更好地帮助您。

回答by reflog

i'm not sure what "application id" you are referring to, but for a unique identifier of your application you can use:

我不确定您指的是什么“应用程序 ID”,但是对于您的应用程序的唯一标识符,您可以使用:

getApplication().getPackageName() method from your current activity

getApplication().getPackageName() 方法来自您当前的活动

回答by Raviraj

Package name is your android app id .

包名称是您的 android 应用程序 id 。

String appId = BuildConfig.APPLICATION_ID

String appId = BuildConfig.APPLICATION_ID

Or

或者

https://play.google.com/store/apps/details?id=com.whatsapp

https://play.google.com/store/apps/details?id=com.whatsapp

App Id = com.whatsapp

应用程序 ID = com.whatsapp

回答by Amrit Raj

Step 1: Open the Google Play Store

第 1 步:打开 Google Play 商店

Step 2: Open any App in App Store Example: facebook

第 2 步:在 App Store 中打开任何应用程序 示例:facebook

Step 3: Click on any App and Look at the Browser link and At the End id=com.facebook.katana&hl=enwill be there and this is your Apps Unique Id.

第 3 步:单击任何应用程序并查看浏览器链接,最后id=com.facebook.katana&hl=en将出现在那里,这是您的应用程序唯一 ID。

回答by Tony Frolov

Else you can get id of process your application runs in:

否则,您可以获得应用程序运行的进程 ID:

final static int android.os.Process.myPid()
Returns the identifier of this process, which can be used with killProcess(int) and sendSignal(int, int).

final static int android.os.Process.myPid()
返回此进程的标识符,可与killProcess(int) 和sendSignal(int, int) 一起使用。

回答by youri

I am not sure what you need the app/installation ID for, but you can review the existing possibilities in a great article from Android developers:

我不确定您需要应用程序/安装 ID 的用途,但您可以在 Android 开发人员的一篇很棒的文章中查看现有的可能性:

To sum up:

总结:

  • UUID.randomUUID()for creating id on the first time an app runs after installation and simple retrieval afterwards
  • TelephonyManager.getDeviceId()for actual device identifier
  • Settings.Secure.ANDROID_IDon relatively modern devices
  • UUID.randomUUID()用于在应用程序安装后第一次运行时创建 id,然后进行简单的检索
  • TelephonyManager.getDeviceId()用于实际设备标识符
  • Settings.Secure.ANDROID_ID在相对现代的设备上

回答by tir38

If you are using the new** Gradle build system then getPackageNamewill oddly return application Id, not package name. So MasterGaurav's answer is correct but he doesn't need to start off with ++

如果您使用的是新的** Gradle 构建系统,那么getPackageName奇怪的是返回应用程序 ID,而不是包名称。所以 MasterGaurav 的答案是正确的,但他不需要从 ++ 开始

If by application id, you're referring to package name...

如果通过应用程序 ID,您指的是包名称...

See more about the differences here.

此处查看有关差异的更多信息。

** not so new at this point

** 在这一点上不是那么新

++ I realize that his answer made perfect sense in 2011

++ 我意识到他的回答在 2011 年非常有道理

回答by Will

The PackageInfo.sharedUserIdfield will show the user Id assigned in the manifest.

PackageInfo.sharedUserId字段将显示清单中分配的用户 ID 。

If you want two applications to have the same userId, so they can see each other's data and run in the same process, then assign them the same userId in the manifest:

如果您希望两个应用程序具有相同的 userId,以便它们可以看到彼此的数据并在同一个进程中运行,那么在清单中为它们分配相同的 userId:

android:sharedUserId="string"

The two packages with the same sharedUserId need to have the same signature too.

具有相同 sharedUserId 的两个包也需要具有相同的签名。

I would also recommend reading herefor a nudge in the right direction.

我还建议阅读此处以推动正确的方向。

回答by advantej

If the whole purpose is to communicate data with some other application, use Intent's sendBroadcast methods.

如果整个目的是与其他应用程序通信数据,请使用 Intent 的 sendBroadcast 方法。