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

命令行启动虚拟机

当我们下载安装好,可以通过命令行运行指定名字模拟器

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

 

:: 列出虚拟设备
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 myNexus

 

 

当使用genymotion的控制台shell时,一些命令在免费版本不可用,同样的界面上按钮也不可用。

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

另外可以参考: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 Adapter

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 

   默认网关. . . . . . . . . . . . . :   

 

 

但是这里,有VirtualBox Host-Only Ethernet Adapter适配器,但是ipconfig没有看到地址。

无参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'

 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(在该模式下,可设置电池等级和电池状态)
2、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(在该模式下,可设置电池等级和电池状态)
3、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.(如果电池模式已设置为“主机电池”,返回的值便是该主机电池的值。)
4、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"(如果之前模式为“主机电池”,即可仅有设定更换为“手动模式”;         
5、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.(有时,如果显示为电池状态无法建立,一般是在没有设置主机电池的情况下。)
6、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        //修改属性 
2、开始设置修改当前系统时间

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

3、查看是否生效

date 

二、用代码实现修改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差的秒数即可,简单明了。这样客户、用户使用起来更方便一些。

 

 

 

 

 

 

posted @ 2015-10-08 22:16  山岚的一缺  阅读(6341)  评论(0编辑  收藏  举报
喜欢
评论
收藏
顶部