KAP*_*OID 55 android device-detection
我有两个适用于Android平板电脑和Android手机的应用.对于平板电脑的应用程序我设置android:minSdkVersion="11".但是现在像Galaxy S3这样的Android手机拥有Android 4.0.4版本,因此S3用户可以从Google Play商店下载我的平板电脑应用程序.我想警告手机用户在安装平板电脑应用程序时下载手机应用程序.反之亦然平板电脑用户在运行手机应用时下载平板电脑应用.
有没有简单的方法来检测设备类型?
我找到了这个链接的解决方案.
在您的清单文件中,您可以为手机和平板电脑声明屏幕功能,然后Google Play会决定手机和平板电脑的下载权限.
Ale*_*ood 129
用这个:
public static boolean isTablet(Context context) {
return (context.getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK)
>= Configuration.SCREENLAYOUT_SIZE_LARGE;
}
Run Code Online (Sandbox Code Playgroud)
如果设备在大屏幕上运行,它将返回true.
Bha*_*iya 20
您也可以在资源文件中尝试此
添加布尔参数.
在res/values/dimen.xml文件中添加此行
<bool name="isTab">false</bool>
Run Code Online (Sandbox Code Playgroud)
而在res/values-sw600dp/dimen.xml文件中添加以下行:
<bool name="isTab">true</bool>
Run Code Online (Sandbox Code Playgroud)
然后在你的java文件中获取此值:
if(getResources().getBoolean(R.bool.isTab)) {
System.out.println("tablet");
} else {
System.out.println("mobile");
}
Run Code Online (Sandbox Code Playgroud)
此代码段将告知设备类型是7英寸或更高以及Mdpi或更高分辨率.您可以根据需要更改实施.
private static boolean isTabletDevice(Context activityContext) {
boolean device_large = ((activityContext.getResources().getConfiguration().screenLayout &
Configuration.SCREENLAYOUT_SIZE_MASK) ==
Configuration.SCREENLAYOUT_SIZE_LARGE);
if (device_large) {
DisplayMetrics metrics = new DisplayMetrics();
Activity activity = (Activity) activityContext;
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
if (metrics.densityDpi == DisplayMetrics.DENSITY_DEFAULT
|| metrics.densityDpi == DisplayMetrics.DENSITY_HIGH
|| metrics.densityDpi == DisplayMetrics.DENSITY_MEDIUM
|| metrics.densityDpi == DisplayMetrics.DENSITY_TV
|| metrics.densityDpi == DisplayMetrics.DENSITY_XHIGH) {
AppInstance.getLogger().logD("DeviceHelper","IsTabletDevice-True");
return true;
}
}
AppInstance.getLogger().logD("DeviceHelper","IsTabletDevice-False");
return false;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
82541 次 |
| 最近记录: |