How Android gets mobile phone information (1)

Source: Internet
Author: User

1. Mobile phone information viewing assistant Feasibility Analysis

Before writing a program, you need to perform some feasibility analysis on the required functions to achieve specific purpose. If some functions cannot be implemented, You can adjust them as soon as possible.
Here we will analyze the functions required by the project, mainly information viewing and information collection, such as version information and hardware information, which can be obtained by reading system files or running system commands, for example, you need to obtain the installed software information and runtime information through APIS. The implementation of API interfaces is not a problem. The main focus is on how to implement the function of running system commands and obtaining the returned results. The specific implementation code is as follows:

Java code:

Public class cmdexecute {
Public synchronized string run (string [] cmd, string workdirectory) throws ioexception {
String result = "";
Try {
Processbuilder builder = new processbuilder (CMD );
Inputstream in = NULL;
// Set a path
If (workdirectory! = NULL ){
Builder. Directory (new file (workdirectory ));
Builder. redirecterrorstream (true );
Process = builder. Start ();
In = process. getinputstream ();
Byte [] Re = new byte [1024];

While (in. Read (re )! =-1)
Result = Result + new string (re );
}
If (in! = NULL ){
In. Close ();
}
} Catch (exception ex ){
Ex. printstacktrace ();
}
Return result;
}
}

1.2 Mobile phone information display assistant function implementation
1.2.1 mobile phone information display assistant Main Interface
According to the preset plan, the entry for viewing four types of information is placed on the main interface. The layout file is main. XML, which is basically composed of a list component. The implementation code is as follows:
Here main. XML uses the linearlayout layout, where a listview component is placed.
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android:/orientation = "vertical" Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<Listview
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: Id = "@ + ID/itemlist"/>
</Linearlayout>

1.2.2 Viewing System Information implementation
When you click the first line in the main interface, that is, the line "system information", the Execution Code is as follows:
Java code:

Case 0:
Intent. setclass (eoeinfosassistant. This, system. Class );
Startactivity (intent );
Break;

After the code is run, the system interface is displayed. This is the main interface for viewing system information, which is similar to the main interface, that is, the List displays several system information to be viewed.

1.2.2.1 operating system version
Click the "operating system version" option in the first line of the graphic interface to open a new interface, which corresponds to showinfo. java file, and then you need to display the operating system version information of the device, which is included in/proc/version and can be called directly. The cmdexencute class provided in the feasibility analysis is used to call the cat command of the system to obtain the content of the file. The implementation code is as follows:

Java code:

Public static string fetch_version_info (){
String result = NULL;
Cmdexecute cmdexe = new cmdexecute ();
Try {
String [] ARGs = {"/system/bin/cat", "/proc/version "};
Result = cmdexe. Run (ARGs, "system/bin /");
} Catch (ioexception ex ){
Ex. printstacktrace ();
}
Return result;
}

The above Code uses the cmdexecute class and calls the system's "/system/bin/cat" tool to obtain the content in "/proc/version. The displayed search result shows that the system version of this device is Linux version 2.6.25-018430-gfea26b0.

Related Article

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.