How to check Architecture of android device and show them?

痞子三分冷 提交于 2019-12-24 02:58:12

问题


I am working on a app which works on the Architecture of devices, means if user is using 32bit phone, it will show some different function andfor 64bit some other function. If there is a way that can also print their bit on device and let user know. Thank in advance


回答1:


My answer borrows from both Patel and Majumder. If you want to do something with a specific architecture, then you should check for which specific instruction set you want to work under(if you want to find out more than if it just 32 or 64 bit). You can see descriptions for the ABIs here.

As explained by Patel, after API Level 21, some of the values become deprecated so you should check for those before calling on your functions. I have provided some code yo help you check for this.

final String DEBUG_TAG_ARC = "Supported ABIS";

int OSNumber = Integer.valueOf(Build.VERSION.SDK_INT);
Toast.makeText(this, "API: "+OSNumber, Toast.LENGTH_SHORT).show();
Log.d(DEBUG_TAG_ARC, "API: "+OSNumber);


//if OS is less than API 21
if(OSNumber < Build.VERSION_CODES.LOLLIPOP ) {
    String archType = Build.CPU_ABI2;
    Toast.makeText(this, archType, Toast.LENGTH_SHORT).show();

    String archType2 = Build.CPU_ABI2;
    Toast.makeText(this, archType2, Toast.LENGTH_SHORT).show();

    Log.d(DEBUG_TAG_ARC, "Supports the following: "+archType+" and "+archType2);

    //here is were you do something with the values
    //somthing like this
    //32BIT
    if(archType.equals("x86") || archType.equals("x86")){
        //do something
    }
    //64BIT
    else if(archType.equals("x86_64") || archType.equals("x86_64")){
        //do something
    }
}

//if OS is greater than or equal toAPI 21
//check for supported ABIS
if(OSNumber >= Build.VERSION_CODES.LOLLIPOP ) {
    String[] supportedABIS = Build.SUPPORTED_ABIS;
    String[] supportedABIS_32_BIT = Build.SUPPORTED_32_BIT_ABIS;
    String[] supportedABIS_64_BIT = Build.SUPPORTED_64_BIT_ABIS;

    for(String aSupportedABI: supportedABIS){
        Log.d(DEBUG_TAG_ARC, aSupportedABI);
    }

    for(String aSupportedABI: supportedABIS_32_BIT){
        Log.d(DEBUG_TAG_ARC, "32BIT : "+aSupportedABI);
    }

    for(String aSupportedABI: supportedABIS_64_BIT){
        Log.d(DEBUG_TAG_ARC, "64BIT : "+aSupportedABI);
    }

    //checks that there is support for 32 or 64BIT
    Log.d(DEBUG_TAG_ARC,"length of 32BIT support: "+supportedABIS_32_BIT.length);
    Log.d(DEBUG_TAG_ARC,"length of 64BIT support: "+supportedABIS_64_BIT.length);

    //to do something with the 32BIT Architecture
    //if there is nothing (0) in the ordered list then it means that it does not support it
    if(supportedABIS_32_BIT.length > 0 ){
        //do something with the 32BIT ARCH
    }
    //to do something with the 64BIT Architecture
    //if there is nothing (0) in the ordered list then it means that it does not support it
    else if(supportedABIS_64_BIT.length > 0 ){
        //do something with the 64BIT ARCH
    }

}

You can also see all of this information on the Android Monitor by selecting Debug and entering Supported ABIS into the search field. Hope this helps.




回答2:


Checkout Build (android.os.build)

Get the information about CPU and ABIs

String cpuABI = Build.CPU_ABI; // Return the name of the instruction set (CPU type + ABI convention) of native code.
String cpuABI2 = Build.CPU_ABI2; // Return The name of the second instruction set (CPU type + ABI convention) of native code.

Above two fields were deprecated in API level 21.

Use below fields for API level 21 or higher.

String[] supportedABIS = Build.SUPPORTED_ABIS; // Return an ordered list of ABIs supported by this device.
String[] supported32BitABIS = Build.SUPPORTED_32_BIT_ABIS; // Return an ordered list of 32 bit ABIs supported by this device.
String[] supported64BitABIS = Build.SUPPORTED_64_BIT_ABIS; // Return an ordered list of 64 bit ABIs supported by this device.



回答3:


https://developer.android.com/reference/android/os/Build.html

Go to the above reference, the Strings at fields section can be used.

If you want to show device Architecture in a toast, then use this below code;

String abi = Build.CPU_ABI;
Toast arc = Toast.makeText(getApplicationContext(), "Device Architecture is "+ abi, Toast.LENGTH_LONG);
arc.show();


来源:https://stackoverflow.com/questions/41605726/how-to-check-architecture-of-android-device-and-show-them

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!