Android determines whether the current device is a mobile phone or tablet or an android tablet.

Source: Internet
Author: User

Android determines whether the current device is a mobile phone or tablet or an android tablet.

Android development needs to adapt to mobile phones and tablets. In some cases, it is necessary to determine whether the device is a mobile phone or tablet.
On the Internet, it is often said that the device size, DPI, version number, and whether the phone function is available are used to determine whether the device is accurate.
Here is a concise and powerful method (official usage ):

CREATE TABLE  `friends` (
  `Id` int(10) unsigned NOT NULL auto_increment,
  `providerId` int(10) unsigned NOT NULL default '0',
  `requestId` int(10) unsigned NOT NULL default '0',
  `status` binary(1) NOT NULL default '0',
  PRIMARY KEY  (`Id`),
  UNIQUE KEY `Index_3` (`providerId`,`requestId`),
  KEY `Index_2` (`providerId`,`requestId`,`status`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='providerId is the Id of the users who wish to be friend with';

CREATE TABLE  `users` (
  `Id` int(10) unsigned NOT NULL auto_increment,
  `username` varchar(45) NOT NULL default '',
  `password` varchar(32) NOT NULL default '',
  `email` varchar(45) NOT NULL default '',
  `date` datetime NOT NULL default '0000-00-00 00:00:00',
  `status` tinyint(3) unsigned NOT NULL default '0',
  `authenticationTime` datetime NOT NULL default '0000-00-00 00:00:00',
  `userKey` varchar(32) NOT NULL default '',
  `IP` varchar(45) NOT NULL default '',
  `port` int(10) unsigned NOT NULL default '0',
  PRIMARY KEY  (`Id`),
  UNIQUE KEY `Index_2` (`username`),
  KEY `Index_3` (`authenticationTime`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

To determine whether the phone function is available (you can call some tablets now ):

 public String sendHttpRequest(String params)
 
        {               
 
                URL url;
 
                String result = new String();
 
                try 
 
                {
 
                        url = new URL(AUTHENTICATION_SERVER_ADDRESS);
 
                        HttpURLConnection connection;
 
                        connection = (HttpURLConnection) url.openConnection();
 
                        connection.setDoOutput(true);
 
                        
 
                        PrintWriter out = new PrintWriter(connection.getOutputStream());
 
                        
 
                        out.println(params);
 
                        out.close();
 

 
                        BufferedReader in = new BufferedReader(
 
                                        new InputStreamReader(
 
                                                        connection.getInputStream()));
 
                        String inputLine;
 

 
                        while ((inputLine = in.readLine()) != null) {
 
                                result = result.concat(inputLine);                              
 
                        }
 
                        in.close();                     
 
                } 
 
                catch (MalformedURLException e) {
 
                        e.printStackTrace();
 
                } 
 
                catch (IOException e) {
 
                        e.printStackTrace();
 
                }                       
 
                
 
                if (result.length() == 0) {
 
                        result = HTTP_REQUEST_FAILED;
 
                }
 
                
 
                return result;
 
                
 
        
 
        }
 

 

 

 
        public boolean sendMessage(String message, String ip, int port) 
 
        {
 
                try {
 
                        
 
                        
 
                        String[] str = ip.split("\\.");
 
                        
 
                        byte[] IP = new byte[str.length];
 
                        
 
                        for (int i = 0; i < str.length; i++) {
 
                                
 
                                IP[i] = (byte) Integer.parseInt(str[i]);                                
 
                        }
 
                        Socket socket = getSocket(InetAddress.getByAddress(IP), port);
 
                        if (socket == null) {
 
                                return false;
 
                        }
 
                
 
                        PrintWriter out = null;
 
                        out = new PrintWriter(socket.getOutputStream(), true);
 
                        
 
                        out.println(message);
 
                } catch (UnknownHostException e) {                      
 
                        return false;
 
                        //e.printStackTrace();
 
                } catch (IOException e) {
 
                        return false;                   
 
                        //e.printStackTrace();
 
                }
 
                
 
                return true;            
 
        }

 

As for the device size, resolution, and version number, I personally think that the accuracy is too poor and it is not recommended.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.