Android PhoneGap - 在 phonegap 中检测设备类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11077853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:55:28  来源:igfitidea点击:

PhoneGap - Detect device type in phonegap

androidcordovaphonegap-plugins

提问by Preet_Android

I'm building an application in phonegap for the three different platforms: Android, iOS And Blackberry. While doing so, I have to detect the device type so that I can manage the height and width of my app on different platforms and for different devices like Android Phone, Android Tablet, iPhone, iPad and Blackberry...

我正在为三个不同的平台在 phonegap 中构建应用程序:Android、iOS 和 Blackberry。这样做时,我必须检测设备类型,以便我可以在不同平台和不同设备(如 Android 手机、Android 平板电脑、iPhone、iPad 和黑莓)上管理我的应用程序的高度和宽度...

回答by Sindre

New in Phonegap, and simplest, is that you can use the device.platform:

Phonegap 的新功能,最简单的是,您可以使用 device.platform:

// Depending on the device, a few examples are:
//   - "Android"
//   - "BlackBerry"
//   - "iOS"
//   - "webOS"
//   - "WinCE"
//   - "Tizen"
//   - "browser"
var devicePlatform = device.platform;

回答by JDev

if you want to get device type before onDeviceReady, you can get like this.

如果你想在 onDeviceReady 之前获取设备类型,你可以这样。

var deviceType = (navigator.userAgent.match(/iPad/i))  == "iPad" ? "iPad" : (navigator.userAgent.match(/iPhone/i))  == "iPhone" ? "iPhone" : (navigator.userAgent.match(/Android/i)) == "Android" ? "Android" : (navigator.userAgent.match(/BlackBerry/i)) == "BlackBerry" ? "BlackBerry" : "null";

alert(deviceType);

回答by RouR

cordova.platformId 
// "android"

回答by Ice Box

you can use device.name property to get the detailed info about the device.

您可以使用 device.name 属性来获取有关设备的详细信息。

    function onDeviceReady() {
    var name=device.name ;
       }

or to get only the platform you can use device.platform property.

或者只获取平台,您可以使用 device.platform 属性。

    function onDeviceReady() {
    var name=device.name ;
    var plat=device.platform;
       }

回答by Aamirkhan

use this code as ur html file

将此代码用作您的 html 文件

Device Properties Example

设备属性示例

<script type="text/javascript" charset="utf-8" src="phonegap-1.0.0.js"></script>
<script type="text/javascript" charset="utf-8">

// Wait for PhoneGap to load
//
document.addEventListener("deviceready", onDeviceReady, false);

// PhoneGap is ready
//
function onDeviceReady() {
    var element = document.getElementById('deviceProperties');

    element.innerHTML = 'Device Name: '     + device.name     + '<br />' + 
                        'Device PhoneGap: ' + device.phonegap + '<br />' + 
                        'Device Platform: ' + device.platform + '<br />' + 
                        'Device UUID: '     + device.uuid     + '<br />' + 
                        'Device Version: '  + device.version  + '<br />';
}

</script>

Loading device properties...

正在加载设备属性...

i suggest u to check this in device Best Of Luck Aamirkhan I.

我建议你在设备 Best Of Luck Aamirkhan I 中检查这个。

回答by Dominik Kirschenhofer

You can have a look at the following link: http://docs.phonegap.com/en/1.0.0/phonegap_device_device.md.html

您可以查看以下链接:http: //docs.phonegap.com/en/1.0.0/phonegap_device_device.md.html

There are several methods to get device infos from phonegap.

有几种方法可以从 phonegap 获取设备信息。

An other option is to have a look at the browser type. (IE = WP7, Safari = iOS,...)

另一种选择是查看浏览器类型。(IE = WP7,Safari = iOS,...)

I'm not sure if you will be able to detect if you are at an Android phone or tablet... Anyway your html apps should be able to handle any screen resolution. You can have different resolutions at the phones also!

我不确定您是否能够检测到您使用的是 Android 手机还是平板电脑...无论如何,您的 html 应用程序应该能够处理任何屏幕分辨率。您也可以在手机上使用不同的分辨率!

回答by vikky

function onDeviceReady()
{
// ***IMPORTANT: access device object only AFTER "deviceready" event    
document.getElementById("name").innerHTML = device.name;
document.getElementById("pgversion").innerHTML = device.cordova ? device.cordov:device.phonegap;
document.getElementById("platform").innerHTML = device.platform;
document.getElementById("uuid").innerHTML = device.uuid;
document.getElementById("version").innerHTML = device.version;

}

See Here for device.platform

请参阅此处了解device.platform

回答by mr.subtle

If you need this information because you're trying to format the screen correctly - you should be using CSS @media queries to determine the appropriate screen layout and apply CSS accordingly.

如果您因为尝试正确设置屏幕格式而需要此信息 - 您应该使用 CSS @media 查询来确定适当的屏幕布局并相应地应用 CSS。

Use Google to find info on "responsive design with CSS"

使用 Google 查找有关“使用 CSS 的响应式设计”的信息

回答by Martijn Scheffer

i would certainly NOT use the device type when laying out a page, the layout should be based on screen size only

我当然不会在布局页面时使用设备类型,布局应该仅基于屏幕大小

you can use the javascript properties:

您可以使用 javascript 属性:

width = window.innerWidth
height = window.innerHeight

or use CSS:

或使用 CSS:

.element{   
    height: 50vh;  //(height = 50 percent of screen)
}

see vmin,vmax, vh, vw

见 vmin、vmax、vh、vw