AndroidX86模擬器Genymotion的一些使用和另外一款Andy模擬器

命令行啓動虛擬機

當咱們下載安裝好,能夠經過命令行運行指定名字模擬器linux

D:\ProgramFiles\Genymobile\Genymotion\player  --vm-name "Samsung Galaxy S3 - 4.1.1 - API 16 - 720x1280"android

 

:: 列出虛擬設備
Genymotion installer path>\genyshell -c "devices list"

::  Genymotion Shell > devices list
::  
::  |----------------------------------------------------------------------|
::  | No Genymotion virtual device running found                           |
::  | Please, run at least one Genymotion virtual device to use this shell |
::  |----------------------------------------------------------------------|
::  
::  Available devices:
::  
::   Id | Select |    Status     |   Type   |   IP Address    |      Name
::  ----+--------+---------------+----------+-----------------+---------------
::    0 |        |           Off |  virtual |         0.0.0.0 | Google Nexus 7 - 5.1.0 - API 22 - 800x1280
::    1 |        |           Off |  virtual |         0.0.0.0 | Google Nexus S - 2.3.7 - API 10 - 480x800
::    2 |        |           Off |  virtual |  192.168.56.101 | Samsung Galaxy Note 2 - 4.2.2 - API 17 - 720x1280
::    3 |        |           Off |  virtual |         0.0.0.0 | Samsung Galaxy S4 - 4.3 - API 18 - 1080x1920

:: 啓動某虛擬設備 <VM id | VM name>
<Genymotion installer path>\player --vm-name "<virtual device name>"

Since Genymotion 2.5.0
gmtool admin create "Google Nexus 5 - 4.4.4 - API 19 - 1080x1920" myNexus
gmtool admin start myNexusgit

 

 

當使用genymotion的控制檯shell時,一些命令在免費版本不可用,一樣的界面上按鈕也不可用。shell

其實,官方文檔就有:https://www.genymotion.com/#!/support?chapter=start-virtual-devices-command-promptwindows

另外能夠參考:How to start Genymotion device with shell command?服務器

 

VirtualBox DHCP server has not assigned an IP address to virtual

 

D:\ProgramFiles\Oracle\VirtualBox>VBoxManage list dhcpservers
NetworkName: HostInterfaceNetworking-VirtualBox Host-Only Ethernet Adapter #10
IP: 169.254.238.100
NetworkMask: 255.255.0.0
lowerIPAddress: 169.254.238.101
upperIPAddress: 169.254.238.254
Enabled: Yes

NetworkName: HostInterfaceNetworking-VirtualBox Host-Only Ethernet Adapter #11
IP: 169.254.189.100
NetworkMask: 255.255.0.0
lowerIPAddress: 169.254.189.101
upperIPAddress: 169.254.189.254
Enabled: Yes

NetworkName: HostInterfaceNetworking-VirtualBox Host-Only Ethernet Adapter #9
IP: 169.254.190.100
NetworkMask: 255.255.0.0
lowerIPAddress: 169.254.190.101
upperIPAddress: 169.254.190.254
Enabled: Yes

NetworkName: HostInterfaceNetworking-VirtualBox Host-Only Ethernet Adapter
IP: 192.168.56.100
NetworkMask: 255.255.255.0
lowerIPAddress: 192.168.56.101
upperIPAddress: 192.168.56.254
Enabled: Yes

NetworkName: NatNetwork
IP: 10.0.2.3
NetworkMask: 255.255.255.0
lowerIPAddress: 10.0.2.4
upperIPAddress: 10.0.2.254
Enabled: Yes

 
VBoxManage list dhcpservers

 

先禁用virtual box 的dhcp功能 VBoxManage dhcpserver remove --netname "<name>"。網絡

D:\ProgramFiles\Oracle\VirtualBox>VBoxManage dhcpserver remove --netname "HostInterfaceNetworking-Vi
rtualBox Host-Only Ethernet Adapter #10"

D:\ProgramFiles\Oracle\VirtualBox>VBoxManage dhcpserver remove --netname "HostInterfaceNetworking-Vi
rtualBox Host-Only Ethernet Adapter #11"

D:\ProgramFiles\Oracle\VirtualBox>VBoxManage dhcpserver remove --netname "HostInterfaceNetworking-Vi
rtualBox Host-Only Ethernet Adapter #9"

D:\ProgramFiles\Oracle\VirtualBox>VBoxManage list dhcpservers
NetworkName: HostInterfaceNetworking-VirtualBox Host-Only Ethernet Adapter
IP: 192.168.56.100
NetworkMask: 255.255.255.0
lowerIPAddress: 192.168.56.101
upperIPAddress: 192.168.56.254
Enabled: Yes

NetworkName: NatNetwork
IP: 10.0.2.3
NetworkMask: 255.255.255.0
lowerIPAddress: 10.0.2.4
upperIPAddress: 10.0.2.254
Enabled: Yes
View Code

 

 

 

VirtualBox Host-Only ethernet Adapterapp

VirtualBox安裝後會在Windows分配一個網卡——虛擬網卡而已,在控制面板\網絡和 Internet\網絡鏈接中能夠看到 VirtualBox Host-Only Network,這個網卡的IP通常是

以太網適配器VirtualBox Host-Only Network: 

   鏈接特定的DNS 後綴. . . . . . . : 

   本地連接IPv6 地址. . . . . . . . : fe80::9849:c1ab:2f1b:f7f2%18 

   IPv4 地址. . . . . . . . . . . . : 192.168.56.1 

   子網掩碼  . . . . . . . . . . . . : 255.255.255.0 

   默認網關. . . . . . . . . . . . . :   less

 

 

可是這裏,有VirtualBox Host-Only Ethernet Adapter適配器,可是ipconfig沒有看到地址。dom

無參ipconfig調用,默認狀況下,僅顯示綁定到 TCP/IP 的每一個適配器的 IP 地址、子網掩碼和默認網關。

 

D:\ProgramFiles\Oracle\VirtualBox>ipconfig

Windows IP 配置


以太網適配器 以太網:

媒體狀態 . . . . . . . . . . . . : 媒體已斷開鏈接
鏈接特定的 DNS 後綴 . . . . . . . :

無線局域網適配器 本地鏈接* 2:

媒體狀態 . . . . . . . . . . . . : 媒體已斷開鏈接
鏈接特定的 DNS 後綴 . . . . . . . :

以太網適配器 以太網 2:

鏈接特定的 DNS 後綴 . . . . . . . :
本地連接 IPv6 地址. . . . . . . . : fe80::4521:6580:93a9:16dc%43
IPv4 地址 . . . . . . . . . . . . : 192.168.56.1
子網掩碼 . . . . . . . . . . . . : 255.255.255.0
默認網關. . . . . . . . . . . . . :

無線局域網適配器 WLAN:

鏈接特定的 DNS 後綴 . . . . . . . : lan
本地連接 IPv6 地址. . . . . . . . : fe80::fc82:67dc:9ef4:c27f%17
IPv4 地址 . . . . . . . . . . . . : 192.168.100.105
子網掩碼 . . . . . . . . . . . . : 255.255.255.0
默認網關. . . . . . . . . . . . . : 192.168.100.1

隧道適配器 isatap.lan:

媒體狀態 . . . . . . . . . . . . : 媒體已斷開鏈接
鏈接特定的 DNS 後綴 . . . . . . . : lan

隧道適配器 Teredo Tunneling Pseudo-Interface:

媒體狀態 . . . . . . . . . . . . : 媒體已斷開鏈接
鏈接特定的 DNS 後綴 . . . . . . . :

隧道適配器 isatap.{BB4C487E-1A6F-FFFF-BBBD-FEB6DFFFFFCF}:

媒體狀態 . . . . . . . . . . . . : 媒體已斷開鏈接
鏈接特定的 DNS 後綴 . . . . . . . :

 

帶參數ipconfig /all                  ... 顯示詳細信息

D:\ProgramFiles\Oracle\VirtualBox>ipconfig /all

Windows IP 配置

主機名 . . . . . . . . . . . . . : DESKTOP-IRTI28J
主 DNS 後綴 . . . . . . . . . . . :
節點類型 . . . . . . . . . . . . : 混合
IP 路由已啓用 . . . . . . . . . . : 否
WINS 代理已啓用 . . . . . . . . . : 否
DNS 後綴搜索列表 . . . . . . . . : lan

 

無線局域網適配器 本地鏈接* 2:

媒體狀態 . . . . . . . . . . . . : 媒體已斷開鏈接
鏈接特定的 DNS 後綴 . . . . . . . :
描述. . . . . . . . . . . . . . . : Microsoft Wi-Fi Direct Virtual Adapter
物理地址. . . . . . . . . . . . . : FF-FF-FF-FF-BF-63
DHCP 已啓用 . . . . . . . . . . . : 是
自動配置已啓用. . . . . . . . . . : 是

以太網適配器 以太網 2:

鏈接特定的 DNS 後綴 . . . . . . . :
描述. . . . . . . . . . . . . . . : VirtualBox Host-Only Ethernet Adapter
物理地址. . . . . . . . . . . . . : 08-22-27-52-94-FE
DHCP 已啓用 . . . . . . . . . . . : 是
自動配置已啓用. . . . . . . . . . : 是
本地連接 IPv6 地址. . . . . . . . : fe80::4521:6580:93a9:16dc%43(首選)
IPv4 地址 . . . . . . . . . . . . : 192.168.56.1(首選)
子網掩碼 . . . . . . . . . . . . : 255.255.255.0
默認網關. . . . . . . . . . . . . :
DHCPv6 IAID . . . . . . . . . . . : 721944615
DHCPv6 客戶端 DUID . . . . . . . : 00-01-00-01-1D-51-2B-73-00-90-F5-F6-AE-FD
DNS 服務器 . . . . . . . . . . . : fec0:0:0:ffff::1%1
fec0:0:0:ffff::2%1
fec0:0:0:ffff::3%1
TCPIP 上的 NetBIOS . . . . . . . : 已啓用

無線局域網適配器 WLAN:

鏈接特定的 DNS 後綴 . . . . . . . : lan
描述. . . . . . . . . . . . . . . : Realtek RTL8723AE Wireless LAN 802.11n PCI-E NIC
物理地址. . . . . . . . . . . . . : FF-E3-FF-FF-FF-FF
DHCP 已啓用 . . . . . . . . . . . : 是
自動配置已啓用. . . . . . . . . . : 是
本地連接 IPv6 地址. . . . . . . . : fe80::fc82:67dc:9ef4:c27f%17(首選)
IPv4 地址 . . . . . . . . . . . . : 192.168.100.105(首選)
子網掩碼 . . . . . . . . . . . . : 255.255.255.0
得到租約的時間 . . . . . . . . . : 2015年11月9日 21:44:07
租約過時的時間 . . . . . . . . . : 2015年11月10日 21:59:41
默認網關. . . . . . . . . . . . . : 192.168.100.1
DHCP 服務器 . . . . . . . . . . . : 192.168.100.1
DHCPv6 IAID . . . . . . . . . . . : 103342919
DHCPv6 客戶端 DUID . . . . . . . : 00-01-01-11-1D-51-2B-73-00-90-F5-F6-AE-FD
DNS 服務器 . . . . . . . . . . . : 192.168.100.1
TCPIP 上的 NetBIOS . . . . . . . : 已啓用
ipconfig

 

 

雖然早就看到這個問題的解決有一個是取消windows的hy設置,可是總覺得分配ip和這個無關。最後不得不關掉這個設置重啓機器,確實可行,機器爲win10。

 

安裝好VirtualBox出現Host-Only,啓動genymotion.exe出現Host-Only #2,啓動一個虛擬機出現Host-Only #3

C:\Program Files\Oracle\VirtualBox>VBoxManage list dhcpservers
NetworkName:    HostInterfaceNetworking-VirtualBox Host-Only Ethernet Adapter
IP:             0.0.0.0
NetworkMask:    0.0.0.0
lowerIPAddress: 0.0.0.0
upperIPAddress: 0.0.0.0
Enabled:        No

NetworkName:    HostInterfaceNetworking-VirtualBox Host-Only Ethernet Adapter #2
IP:             192.168.191.100
NetworkMask:    15.0.0.0
lowerIPAddress: 192.168.191.101
upperIPAddress: 192.168.191.254
Enabled:        Yes

NetworkName:    HostInterfaceNetworking-VirtualBox Host-Only Ethernet Adapter #3
IP:             192.168.43.100
NetworkMask:    15.0.0.0
lowerIPAddress: 192.168.43.101
upperIPAddress: 192.168.43.254
Enabled:        Yes
VBoxManage list dhcpservers

 

 

功能限制解除之一

Genymotion Shell > phone call 110
No valid license found. This command is not available with the Free version.
You can purchase a license on https://shop.genymotion.com/ to use this command.
>>> Failed

 

 

 

命令行工具crack要點:

搜索字符串,nop前一個跳轉,嘗試命令phone call 110,patch修改OK。

 

 

和虛擬設備交互

Interacting with virtual devices

To modify sensor statuses and values of a virtual device, use the commands listed below:

  • devices list
    Lists available virtual devices and provides details such as current status, IP address and name.
  • devices refresh
    Refreshes the virtual device list.
  • devices select
    Selects a virtual device. Add the ID of the virtual device you wish to select (as displayed in the device list).
  • devices ping
    Sends a ping message to check that the virtual device is responding.
  • devices factoryreset
    Resets the virtual device to factory state. Add the ID of the device you wish to reset (as displayed in the device list). Adding the keyword force at the end skips the warning message about data loss.
  • battery getmode
    Returns the current battery mode of the selected virtual device.
  • battery setmode
    Sets the battery mode of the selected virtual device. The mode can be:
    • host: the virtual battery values reflect the host battery values (if any);
    • manual: the virtual battery charge level and state of charge can be manually set.
  • battery getlevel
    Returns the current battery charge level of the selected virtual device. If the battery mode is "host", the returned value is the host value.
  • battery setlevel
    Sets the battery charge level of the selected virtual device. The value must range from 0% to 100%. This command forces the activation of the manual mode.
  • battery getstatus
    Returns the current battery state of charge of the selected virtual device.
  • battery setstatus
    Sets the battery connection status of the selected virtual device. Values can be:
    • discharging: simulates that the power supply is unplugged and the battery is discharging.
      The value ranges from 0% to 100%.
    • charging: simulates that the power supply is plugged in and the battery is charging.
      The value ranges from 0% to 100%.
    • notcharging: simulates that the power supply has just been unplugged and the battery is not discharging yet.
      The value ranges from 0% to 100%.
    • full: simulates that the battery is fully charged.
  • gps getstatus
    Returns the status of the GPS signal reception.
  • gps setstatus
    Sets the activation status of the GPS signal reception. Values can be:
    • enabled: the GPS signal reception is enabled;
    • disabled: the GPS signal reception is disabled.
  • gps getlatitude
    Returns the current latitude value (if the GPS is activated and a latitude value is set) or 0.
  • gps setlatitude
    Sets the latitude value (and forces the activation of the GPS if deactivated). The value must range from -90° to 90°.
  • gps getlongitude
    Returns the current longitude value (if the GPS is activated and a longitude value is set) or 0.
  • gps setlongitude
    Sets the longitude value (and forces the activation of the GPS if deactivated). The value must range from -180° to 180°.
  • gps getaltitude
    Returns the current altitude value (if the GPS is activated and an altitude value is set) or 0.
  • gps setaltitude
    Sets the altitude value (and forces the activation of the GPS if deactivated). The value must range from -20m to 10000m.
  • gps getaccuracy
    Returns the current accuracy value (if the GPS is activated and an accuracy value is set) or 0.
  • gps setaccuracy
    Set the accuracy value (and forces the activation of the GPS if deactivated). The value must range from 0m to 200m.
  • gps getbearing
    Returns the current bearing value (if the GPS is activated and a bearing value is set) or 0.
  • gps setbearing
    Sets the bearing value (and forces the activation of the GPS if deactivated). The value must range from 0° to 359,99°.
  • rotation setangle
    Sets the rotation angle value of the virtual device. The value must range from 0° to 359°.
  • android version
    Returns the Android version of the selected virtual device.
  • android getandroidid
    Returns the Android ID number of the selected virtual device.

    This is only possible with an Indie or a Business license.

  • android setandroidid
    Sets the Android ID number of the selected virtual device.
    Values can be:
    • random: a random Android ID number is generated;
    • custom: you must enter a value containing 16 hexadecimal digits.

    This is only possible with an Indie or a Business license.

    If several users exist on the Android system, the Android ID can only be set for the first user.

  • android getdeviceid
    Returns the device ID (IMEI/MEID) number of the selected virtual device.

    This is only possible with an Indie or a Business license.

  • android setdeviceid
    Sets the device ID (IMEI/MEID) number of the selected virtual device.
    Values can be:
    • none: no device ID number is generated.
    • random: a random device ID number is generated.
    • custom: you must enter a value containing alphanumeric characters, dots, dashes and/or underscores.

    This is only possible with an Indie or a Business license.

  • network getprofile
    Returns the current network profile of the selected virtual device.
  • network setprofile
    Sets the network profile of the selected virtual device. Values can be:
    • none: no network profile is enabled.
    • no-data: no network connection is simulated.
    • gprs: a GPRS network connection is simulated.
    • edge: an Edge network connection is simulated.
    • 3g: a 3G network connection is simulated.
    • 4g: a 4G network connection is simulated.
    • 4g-high-losses: a 4G network connection with packet loss set to 10% is simulated.
    • 4g-bad-dns: a 4G network connection with a DNS delay set to 3000ms is simulated.
    • wifi: a Wi-Fi network connection is simulated.

 電話功能

    • phone call <phone_number> 模擬某號碼的來電
      simulates an incoming phone call from a given phone number.
    • phone sms <phone_number> <message> 模擬某號碼的短信
      simulates an incoming text message from a given phone number.
    • phone baseband <command> 模擬基帶命令
      sends commands directly to the baseband. This command has been implemented in order to ease script creation. For more information on the phone baseband command and its capabilities, please refer to Baseband commands(或本頁鏈接).

Baseband commands

The phone baseband commands send raw data directly to the baseband. In this section, you will find all commands available in Genymotion Shell to directly interact with the baseband.

Commands below return results in your logcat.

  • gsm list
    lists all incoming and outgoing phone calls and their state.
  • gsm call <phone_number>
    simulates a new incoming call from a given phone number.
  • gsm busy <remote_number>
    hangs up an outgoing call (performed from the integrated Dialer application) and reports the remote phone as busy.
  • gsm hold <remote_number>
    simulates that an incoming or outgoing phone call (performed from the integrated Dialer application) is put on hold.
  • gsm accept <remote_number>
    simulates that the outgoing phone call (performed from the integrated Dialer application) is answered.
  • gsm cancel <phone_number>
    simulates that an incoming or outgoing phone call is hung up.
  • gsm data
    modifies data connection state. Values can be:
    • unregistered or off: no network available;
    • home or on: on local network, non-roaming;
    • roaming: on roaming network;
    • searching: searching networks;
    • denied: emergency calls only.
  • gsm voice
    modifies voice connection state. Values can be:
    • unregistered or off: no network available;
    • home or on: on local network, non-roaming;
    • roaming: on roaming network;
    • searching: searching networks;
    • denied: emergency calls only.
  • gsm status
    displays the current status of the GSM emulation.
  • gsm signal <rssi> <ber> <rs_snr>
    changes the reported strength and error rate on next (15s) update.
    • rssi: the value must range from 0 to 31. Returns 99 if the value is unknown.
    • ber: the value must range from 0% to 7%. Returns 99 if the value is unknown.
    • rs_snr: the value must range from -200 to 300.
  • cdma ssource <ssource>
    sets the current CDMA subscription source. Values can be:
    • nv : the subscription is read from non-volatile RAM;
    • ruim: the subscription is read from RUIM.
  • cdma prl_version <version>
    dumps the current PRL version.
  • sms send <phone_number> <message>
    simulates an incoming text message from a given phone number.
  • sms pdu <hexstring>
    simulates a new incoming text message defined in the PDU format.

adb命令

where-to-find-info-on-androids-service-call-shell-command

檢查服務是否存在
$ adb shell service check phone #< ServiceName >
Service phone: found

經過adb 命令撥打電話
adb shell service call phone 2 s16 "10086" #< PhoneNumber >
adb shell service call phone 5 s16

adb shell service call isms 5 s16 "PhoneNumber" i32 0 i32 0 s16 "BodyText"

經過adb 命令發送短信,須要兩步
adb shell am start -a android.intent.action.SENDTO -d sms:10086(發送目的號碼) --es sms_body "hello"(短信內容) --ez exit_on_sent true
adb shell input keyevent 66 // 模擬發送按鍵
MyApp:V
adb shell am start -a android.provider.Telephony.SMS_RECEIVED

 

模擬器電量驅動

 lgocat一直報告錯誤:12-01 14:50:50.566: E/Genymotion(323): Could not open '/sys/class/power_supply/genymotion_fake_path/present'

控制檯1或其餘 控制檯2:使用watchprops命令監控變化
  C:\Users\fangss>adb shell
root@android:/ # watchprops
watchprops
root@android:/ # setprop genyd.battery.mode host
setprop genyd.battery.mode host
1448952348 genyd.battery.mode = 'host'
1448952350 genyd.battery.mode = 'manual'
1448952350 genyd.battery.level = '50000000'
1448952350 genyd.battery.full = '50000000'
1448952350 genyd.battery.capacity = '100'
1448952350 genyd.battery.status = 'Not charging'
1448952350 genyd.ac.online = '1'
從界面上的電池控制對話框關閉 1448952364 genyd.battery.mode = 'auto'
1448952365 genyd.battery.mode = 'manual'
1448952365 genyd.battery.level = '50000000'
1448952365 genyd.battery.full = '50000000'
1448952365 genyd.battery.capacity = '100'
1448952365 genyd.battery.status = 'Not charging'
1448952365 genyd.ac.online = '1'

 一、battery getmode:Return the current battery mode of the selected virtual device. (返回當前的電池模式選擇的虛擬設備。)The mode can only be(該模式僅限於):

host:The virtual battery reflect the host battery (if exists) (以虛擬電池顯示爲主機電池——若是存在)
manual:In this mode, you can set the level and status battery values(在該模式下,可設置電池等級和電池狀態)
二、battery setmode:Set the battery mode.(設置電池模式。)The mode can only be(該模式僅限於):          
host:The virtual battery reflect the host battery (if exists) (以虛擬電池顯示爲主機電池——若是存在)
 
manual:In this mode, you can set the level and status battery values(在該模式下,可設置電池等級和電池狀態)
三、battery getlevel:Return the current battery amount of power.(返回當前電池的電量。) The value can only be between 0% and 100%.(只能在0%到100%之間設置);
If the battery mode is "host", the returned value is the host value.(若是電池模式已設置爲「主機電池」,返回的值即是該主機電池的值。)
四、battery setlevel:Set the current battery amount of power.(設置當前電池的電量。) The value can only be between 0% and 100%.(只能在0%到100%之間設置);
Set the battery level force the "manual" mode(設置電池級別爲強制「手動」模式):if the last mode was "host", then it's turned to "manual"(若是以前模式爲「主機電池」,便可僅有設定更換爲「手動模式」;         
五、battery getstatus:Return the current battery status.(返回當前電池狀態。)There are 4 possible status(應該有4個狀態):           
Discharging(放電):The power supply is disconnected and the battery is discharging(斷開電源和電池放電);
Charging(充電):The power supply is connected and the battery is charging(電源鏈接和電池充電);
Full(充滿):The battery is full.(電池充滿。);
Unknown(未知):Sometimes, the battery status cannot be established, it happens when there is no host battery.(有時,若是顯示爲電池狀態沒法創建,通常是在沒有設置主機電池的狀況下。)
六、battery setstatus:Set the current battery status(設置當前電池狀態). There are 4 possible status(應該有4個狀態):          
Discharging(放電):The power supply is disconnected and the battery is discharging. (斷開電源和電池放電);            
Charging(充電): The power supply is connected and the battery is charging. (電源鏈接和電池充電);            
Full(充滿):The battery is full. (電源鏈接和電池充電);            
Unknown(未知):Sometimes, the battery status cannot be established, it happens when there is no host battery. (有時,若是顯示爲電池狀態沒法創建,通常是在沒有設置主機電池的狀況下。)
 

Andy模擬器

出來有一段時間了,如今流暢度和兼容度未知,地址http://www.andyroid.net

Try Andy for android and see what happens.

官方SDK自帶模擬器

有新進展
 

微軟發佈的Android模擬器

出示

模擬器時間/時區

取得系統時間

要獲得目前的時間,這個卻是很容易,就是 System.currentTimeMillis()。不過要注意的是,這個函式傳回的是 UTC (GMT) 這個時區的時間,像臺灣所在的時區是 UTC+8,你得將這個函式所傳回的值再加 8 個小時,纔是你要的答案。實際上,固然不用本身加,你能夠借用 Calendar 這個類別來幫你。

更改系統時間

要更改系統的時間,這可就難倒我了。在 m5 SDK 上,用 SystemClock.setCurrentTimeMillis(),雖然會有警告訊息,不過系統時間仍是會被更改。到了 v0.9 SDK,這招就沒效了。掃過 Android 的原始程式,他們的確是用這個函式在更改系統時間。但是在個人程式裏,無論我加了那些 permission,都是給我個 Permission denied 錯誤。最好,只好在 這裏 發文問了這個問題,有個應該是 Google 的工程師,給了下面這個答案。

User apps cannot set the time. Only the system processes can. This is for security, but also because it won't have much effect--the time is reset from the radio via network time.

看了,要在 application 這一層更改系統時間,應該是沒解了。若是有人知道其餘答案的,幫忙解答一下。

取得與更改系統時區

用 TimeZone.getDefault() 就能夠取得當前的時區,若是你沒有特別設定的話,那你取到的,都是 GMT 這個時區。看起來,至少在 Windows 平臺上, Android Emulator 仍是不會正確決定當前的時區。目前我知道的惟一方法就是,在啓動 Emulator 的參數上,加上 -timezone "Asia/Taipei"。透過啓動 Emulator 的參數能夠更改時區,那能夠透過 API 來更改系統時區嗎?我試過 TimeZone.setDefault(TimeZone.getTimeZone("Asia/Taipei")),也用過 System.setProperty("user.timezone", "Asia/Taipei"),沒一個有效。因此看起來,也暫時無解。有誰知道如何透過 API 更改系統時區?

 

 

設置系統的時區配置

cat /data/property/persist.sys.timezone //查看當前時區配置文件
setprop persist.sys.timezone GMT        //修改屬性 
二、開始設置修改當前系統時間

date -s "yyyymmdd.[[[hh]mm]ss]"  或System/bin/date -s "yyyymmdd.[[[hh]mm]ss]"

三、查看是否生效

date 

2、用代碼實現修改android 系統時間的方法

public void testDate(){
try {
Process process = Runtime.getRuntime().exec("su");
String datetime="20131023.112800"; //測試的設置的時間【時間格式 yyyyMMdd.HHmmss】
DataOutputStream os = new DataOutputStream(process.getOutputStream());
os.writeBytes("setprop persist.sys.timezone GMT\n");
os.writeBytes("/system/bin/date -s "+datetime+"\n");
os.writeBytes("clock -w\n");
os.writeBytes("exit\n");
os.flush();
} catch (IOException e) {
e.printStackTrace();
}
}

 

android默認系統日期、時間、時區更改

 

    作android平臺,常常會遇到產品須要更改系統默認時區日期時間的問題。android更改時區相對比較容易實現,網上也有不少資料,直接設置一個屬性便可,例如設置上海東八區,persist.sys.timezone=Asia/Shanghai。可是如何實現更改默認系統時間呢?

      在android中,Linux內核中、rtc時鐘,默認的起始日期都是1970年1月1日,那麼如何把默認日期指到2012-01-01呢?筆者在實踐中發現,在RTC驅動中能夠很容易實現。在RTC驅動加載的時候,通常都有個probe函數須要先執行,所以在probe函數裏下手最直接有效。RTC從1970-01-01開始,那固然很容易把默認值設置到2012-01-01,所須要設置的seconds也就是從1970-01-01所差的秒數,以秒爲單位。所以,一旦讀出來的RTC值小於咱們預想的值好比2012-01-01(1325402913)小,咱們就把它設置到這個時間點。

[cpp] view plaincopy

1.  seconds = rtc_read_time();  

2.  printk("init PMU/RTC time to  %ld \n", seconds);  

3.    

4.  if(seconds <= 1325402913) {  

5.      seconds = 1325402913;/*2012-01-01*/  

6.      ret = rtc_set_time(seconds);  

7.      RTC_DBG("Init Set time: %ld, ret =0x%x\n", seconds, ret);  

8.  }  

 筆者經過結果顯示,在android平臺上是可行的。要設置到哪天幾點幾分,只要算好具體1970-01-01 00:00差的秒數便可,簡單明瞭。這樣客戶、用戶使用起來更方便一些。

相關文章
相關標籤/搜索