Answers:
如果您正在寻找applicationIdgradle中定义的值,则可以简单地使用
BuildConfig.APPLICATION_ID
如果通过application id引用package name,则可以使用方法Context::getPackageName(http:// http://developer.android.com/reference/android/content/Context.html#getPackageName%28%29)。
如果您希望与其他应用程序进行通信,可以采用多种方法:
如果您能详细说明自己的要求,社区将能够为您提供更好的帮助。
Context.getPackageName() 确实返回应用程序ID而不是程序包名称(尽管它们通常是相同的)。该方法的名称有些令人困惑,它是传统的。来源:developer.android.com/studio/build/application-id
我不确定您指的是什么“应用程序ID”,但是对于您的应用程序的唯一标识符,您可以使用:
当前活动中的getApplication()。getPackageName()方法
getApplicationContext().getPackageName()?
包名称是您的android应用ID。
字符串appId = BuildConfig.APPLICATION_ID
要么
https://play.google.com/store/apps/details?id=com.whatsapp
应用程式编号= com.whatsapp
步骤2:在App Store中打开任何应用程序 示例:facebook
第3步:单击任何应用程序,然后查看“浏览器”链接,最后,id = com.facebook.katana&hl = zh_CN将存在,这是您的应用程序唯一ID。
另外,您还可以获取应用程序运行在其中的进程的ID:
final static int android.os.Process.myPid()
返回此进程的标识符,可与killProcess(int)和sendSignal(int,int)一起使用。
我不确定您需要什么应用/安装ID,但是您可以在Android开发人员的一篇精彩文章中回顾现有的可能性:
总结一下:
UUID.randomUUID() 用于在安装后首次运行应用程序时创建ID并随后进行简单检索TelephonyManager.getDeviceId() 用于实际的设备标识符Settings.Secure.ANDROID_ID 在相对现代的设备上该PackageInfo.sharedUserId字段将显示清单中分配的用户ID。
如果您希望两个应用程序具有相同的userId,以便它们可以看到彼此的数据并以相同的进程运行,然后在清单中为其分配相同的userId:
android:sharedUserId="string"
具有相同sharedUserId的两个程序包也需要具有相同的签名。
我还建议您在这里阅读,以朝着正确的方向前进。
要跟踪安装,您可以例如使用UUID作为标识符,并在安装后首次运行应用程序时简单地创建一个新的。这是一个带有一个静态方法Installation.id(Context context)的名为“ Installation”的类的草图。您可以想象将更多特定于安装的数据写入INSTALLATION文件。
public class Installation {
private static String sID = null;
private static final String INSTALLATION = "INSTALLATION";
public synchronized static String id(Context context) {
if (sID == null) {
File installation = new File(context.getFilesDir(), INSTALLATION);
try {
if (!installation.exists())
writeInstallationFile(installation);
sID = readInstallationFile(installation);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return sID;
}
private static String readInstallationFile(File installation) throws IOException {
RandomAccessFile f = new RandomAccessFile(installation, "r");
byte[] bytes = new byte[(int) f.length()];
f.readFully(bytes);
f.close();
return new String(bytes);
}
private static void writeInstallationFile(File installation) throws IOException {
FileOutputStream out = new FileOutputStream(installation);
String id = UUID.randomUUID().toString();
out.write(id.getBytes());
out.close();
}
}
可以在https://github.com/MShoaibAkram/Android-Unique-Application-ID上看到更多内容