问题
I have two apps for Android tablets and Android phones. For tablet app I set android:minSdkVersion=\"11\"
. But nowadays Android phones like Galaxy S3 has Android version 4.0.4 so S3 users can download my tablet app from Google Play Store. I want to warn phone users to download phone app when they install tablet app. Vice versa for tablet users download tablet app when they run phone app.
Is there any easy way to detect the device type?
Edit:
I found solution on this link.
In your manifest file you can declare screen feature for handset and tablets then Google Play decides download permissions for both phone and tablet.
回答1:
Use this:
public static boolean isTablet(Context context) {
return (context.getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK)
>= Configuration.SCREENLAYOUT_SIZE_LARGE;
}
which will return true if the device is operating on a large screen.
Some other helpful methods can be found here.
回答2:
You can also try this
Add boolean parameter in resource file.
in res/values/dimen.xml file add this line
<bool name="isTab">false</bool>
while in res/values-sw600dp/dimen.xml file add this line:
<bool name="isTab">true</bool>
then in your java file get this value:
if(getResources().getBoolean(R.bool.isTab)) {
System.out.println("tablet");
} else {
System.out.println("mobile");
}
回答3:
This code snippet will tell whether device type is 7" Inch or more and Mdpi or higher resolution. You can change the implementation as per your need.
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;
}
回答4:
I think this should detect if something is able to make a phonecall, everything else would be a tablet/TV without phone capabilities.
As far as I have seen this is the only thing not relying on screensize.
public static boolean isTelephone(){
//pseudocode, don't have the copy and paste here right know and can't remember every line
return new Intent(ACTION_DIAL).resolveActivity() != null;
}
回答5:
Use Google Play store capabilities and only enable to download your tablet app on tablets and the phone app on phones.
If the user then installs the wrong app then they must have installed using another method.
回答6:
We had the similar issue with our app that should switch based on the device type - Tab/Phone. IOS gave us the device type perfectly but the same idea wasn't working with Android. the resolution/ DPI method failed with small res tabs, high res phones. after a lot of torn hairs and banging our heads over the wall, we tried a weird idea and it worked exceptionally well that won't depend on the resolutions. this should help you too.
in the Main class, write this and you should get your device type as null for TAB and mobile for Phone.
String ua=new WebView(this).getSettings().getUserAgentString();
if(ua.contains("Mobile")){
//Your code for Mobile
}else{
//Your code for TAB
}
回答7:
Use following code to identify device type.
private boolean isTabletDevice() {
if (android.os.Build.VERSION.SDK_INT >= 11) { // honeycomb
// test screen size, use reflection because isLayoutSizeAtLeast is only available since 11
Configuration con = getResources().getConfiguration();
try {
Method mIsLayoutSizeAtLeast = con.getClass().getMethod("isLayoutSizeAtLeast");
Boolean r = (Boolean) mIsLayoutSizeAtLeast.invoke(con, 0x00000004); // Configuration.SCREENLAYOUT_SIZE_XLARGE
return r;
} catch (Exception x) {
return false;
}
}
return false;
}
回答8:
If you want to decide device is tablet or phone based on screen inch, you can use following
device 6.5 inches or higher consider as tablet, but some recent handheld phone has higher diagonal value. good thing with following solution you can set the margin.
public boolean isDeviceTablet(){
DisplayMetrics metrics = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(metrics);
float yInches= metrics.heightPixels/metrics.ydpi;
float xInches= metrics.widthPixels/metrics.xdpi;
double diagonalInches = Math.sqrt(xInches*xInches + yInches*yInches);
if (diagonalInches>=6.5) {
return true;
}
return false;
}
来源:https://stackoverflow.com/questions/11330363/how-to-detect-device-is-android-phone-or-android-tablet