Detect if app was downloaded from Android Market

前端 未结 5 1369
被撕碎了的回忆
被撕碎了的回忆 2020-11-28 01:56

I have an Android library that uploads data to a test server and production server. I\'d like developers using this library to use the test server when developing, and produ

5条回答
  •  孤城傲影
    2020-11-28 02:32

    Yes, you could use the signature for that. If you use a debug key to sign your app during development and a release key when uploading your app to the market you can check for the signature that the app was signed with and based on that use test or production server. Here is a small code piece to read the signature of your app:

        try {
            PackageManager manager = context.getPackageManager(); 
            PackageInfo appInfo = manager.getPackageInfo(
                YOUR_PACKAGE_NAME, PackageManager.GET_SIGNATURES);
    
            // Now test if the first signature equals your debug key.
            boolean shouldUseTestServer = 
                appInfo.signatures[0].toCharsString().equals(YOUR_DEBUG_KEY);
    
        } catch (NameNotFoundException e) {
            // Expected exception that occurs if the package is not present.
        }
    

    YOUR_PACKAGE_NAME must be something like 'com.wsl.CardioTrainer'. It must be the package name you used in your AndroidManifest.xml. Good Luck

    mark

提交回复
热议问题