How to get Android application id?

后端 未结 13 616
失恋的感觉
失恋的感觉 2020-12-07 18:26

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?

相关标签:
13条回答
  • 2020-12-07 18:44

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

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

    See more about the differences here.

    ** not so new at this point

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

    0 讨论(0)
  • 2020-12-07 18:47

    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:

    • http://android-developers.blogspot.com/2011/03/identifying-app-installations.html

    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_ID on relatively modern devices
    0 讨论(0)
  • 2020-12-07 18:49

    For getting AppId (or package name, how some says), just call this:

    But be sure that you importing BuildConfig with your app id packages path

    BuildConfig.APPLICATION_ID 
    
    0 讨论(0)
  • 2020-12-07 18:53

    To track installations, you could for example use a UUID as an identifier, and simply create a new one the first time an app runs after installation. Here is a sketch of a class named “Installation” with one static method Installation.id(Context context). You could imagine writing more installation-specific data into the INSTALLATION file.

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

    }

    Yon can see more at https://github.com/MShoaibAkram/Android-Unique-Application-ID

    0 讨论(0)
  • 2020-12-07 18:56

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

    BuildConfig.APPLICATION_ID 
    
    0 讨论(0)
  • 2020-12-07 18:56

    Package name is your android app id .

    String appId = BuildConfig.APPLICATION_ID

    Or

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

    App Id = com.whatsapp

    0 讨论(0)
提交回复
热议问题