private void showSize()
{
/* 将TextView及ProgressBar设定为空值及0 */
myTextView.setText(""); myProgressBar.setProgress(0);
/* 判断记忆卡是否插入 */
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
/* 取得SD CARD档案路径一般是/sdcard */
File path = Environment.getExternalStorageDirectory();
/* StatFs看文件系统空间使用状况 */
StatFs statFs = new StatFs(path.getPath());
/* Block的size */
long blockSize = statFs.getBlockSize();
/* 总Block数量 */
long totalBlocks = statFs.getBlockCount();
/* 已使用的Block数量 */
long availableBlocks = statFs.getAvailableBlocks();
String[] total = fileSize(totalBlocks * blockSize);
String[] available = fileSize(availableBlocks * blockSize);
/* getMax取得在main.xml里ProgressBar设定的最大值 */
int ss = Integer.parseInt(available[0]) * myProgressBar.getMax() / Integer.parseInt(total[0]);
myProgressBar.setProgress(ss);
String text = "总共" + total[0] + total[1] + "\n"; text += "可用" + available[0] + available[1];
myTextView.setText(text); }
else if (Environment.getExternalStorageState().equals( Environment.MEDIA_REMOVED))
{
String text = "SD CARD已移除"; myTextView.setText(text);
}
}
/* 回传为字符串数组[0]为大小[1]为单位KB或MB */
private String[] fileSize(long size)
{
String str = "";
if (size >= 1024)
{
str = "KB";
size /= 1024;
if (size >= 1024)
{
str = "MB";
size /= 1024;
}
}
DecimalFormat formatter = new DecimalFormat();
/* 每3个数字用,分隔如:1,000 */
formatter.setGroupingSize(3);
String result[] = new String[2];
result[0] = formatter.format(size);
result[1] = str; return result;
}
if (wakeLock == null) {
Logger.d("Acquiring wake lock");
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK,this.getClass().getCanonicalName());
wakeLock.acquire();
}
}
private void releaseWakeLock() {
if (wakeLock != null && wakeLock.isHeld()) {
wakeLock.release();
wakeLock = null;
}
}
acquireWakeLock()方法中获取了 SCREEN_DIM_WAKE_LOCK锁,该锁使 CPU 保持运转,屏幕保持亮度(可以变灰)。这个函数在Activity的 onResume中被调用。releaseWakeLock()方法则是释放该锁。它在Activity的 onPause中被调用。利用Activiy的生命周期,巧妙的让 acquire()和release()成对出现。
protected void onResume()
{
super.onResume();
//获取锁,保持屏幕亮度
acquireWakeLock();
startTimer();
}
PARTIAL_WAKE_LOCK:保持CPU 运转,屏幕和键盘灯有可能是关闭的。
SCREEN_DIM_WAKE_LOCK:保持CPU 运转,允许保持屏幕显示但有可能是灰的,允许关闭键盘灯
SCREEN_BRIGHT_WAKE_LOCK:保持CPU 运转,允许保持屏幕高亮显示,允许关闭键盘灯
FULL_WAKE_LOCK:保持CPU 运转,保持屏幕高亮显示,键盘灯也保持亮度
ACQUIRE_CAUSES_WAKEUP:Normal wake locks don't actually turn on the illumination. Instead, they cause the illumination to remain on once it turns on (e.g. from user activity). This flag will force the screen and/or keyboard to turn on immediately, when the WakeLock is acquired. A typical use would be for notifications which are important for the user to see immediately.
ON_AFTER_RELEASE:f this flag is set, the user activity timer will be reset when the WakeLock is released, causing the illumination to remain on a bit longer. This can be used to reduce flicker if you are cycling between wake lock conditions.
你可能还需要
<uses-permission android:name="android.permission.DEVICE_POWER" />
*如果你获得一个不完整的wakelock,则CPU会继续运行,不必顾及任何计时器甚至用户按下power键后。对于所有的其他的wakelock,CPU在运行,但用户可以直接用power键使设备休眠。
*If you hold a partial wakelock, the CPU will continue to run, irrespective of any timers and even after the user presses the power button. In all other wakelocks, the CPU will run, but the user can still put the device to sleep using the power button.
另外,你能加两个以上的标志,这些仅能影响屏幕的行为。这些标志当组合中有一个PARTIAL_WAKE_LOCK时将没有效果。
In addition, you can add two more flags, which affect behavior of the screen only. These flags have no effect when combined with a PARTIAL_WAKE_LOCK.
Flag Value
Description
ACQUIRE_CAUSES_WAKEUP
正常的wake lock不会实际转到照明。反而,他们使照明保持自他打开时(例如,来自用户activity)。这个标志会强制屏幕和/或键盘立即打开,当这个wakelock已获取时。一个典型用法是将哪些重要的通知立即让用户看到。
Normal wake locks don't actually turn on the illumination. Instead, they cause the illumination to remain on once it turns on (e.g. from user activity). This flag will force the screen and/or keyboard to turn on immediately, when the WakeLock is acquired. A typical use would be for notifications which are important for the user to see immediately.
ON_AFTER_RELEASE
如果这个标志被设置,用户activity计时器当WakeLock被释放时会重新设定,因为照明保持一点长。这个可以用于降低闪烁如果你是在wake lock状态下循环。
If this flag is set, the user activity timer will be reset when the WakeLock is released, causing the illumination to remain on a bit longer. This can be used to reduce flicker if you are cycling between wake lock conditions.
SummaryNested Classes
class
PowerManager.WakeLock
类让你说你需要有这个设备。
Class lets you say that you need to have the device on.
Constants
int
ACQUIRE_CAUSES_WAKEUP
正常的wake lock不会实际的唤醒设备,他们自他准备好则保持着。
Normally wake locks don't actually wake the device, they just cause it to remain on once it's already on.
int
FULL_WAKE_LOCK
Wake lock 以确保屏幕和键盘全部点亮。
Wake lock that ensures that the screen and keyboard are on at full brightness.
int
ON_AFTER_RELEASE
当这个wake lock已释放时,拨开了用户界面的计时器所以屏幕停留稍微长。
When this wake lock is released, poke the user activity timer so the screen stays on for a little longer.
int
PARTIAL_WAKE_LOCK
Wake lock以确保CPU运行。
Wake lock that ensures that the CPU is running.
int
SCREEN_BRIGHT_WAKE_LOCK
Wake lock以确保屏幕全亮;键盘背光允许关闭。
Wake lock that ensures that the screen is on at full brightness; the keyboard backlight will be allowed to go off.
int
SCREEN_DIM_WAKE_LOCK
Wake lock以确保屏幕打开(但可以是暗淡的);键盘背光将允许关闭。
Wake lock that ensures that the screen is on (but may be dimmed); the keyboard backlight will be allowed to go off.
Public Methods
void
goToSleep(long time)
强制设备进入休眠。
Force the device to go to sleep.
boolean
IsScreenOn()
返回屏幕是否当前亮着。
Returns whether the screen is currently on.
PowerManager.WakeLock
newWakeLock(int flags, String tag)
得到一个Wake lock在这个标志参数的级别。
Get a wake lock at the level of the flags parameter.
void
reboot(String reason)
重启设备
Reboot the device.
void
userActivity(long when, boolean noChangeLights)
用户界面发生。
User activity happened.
6月份接手一个新的项目,该项目之前是有其他同事尝试做了一些前期的探索,雏形也有小成。
之前是由web做了一个服务器段,然后由各mobile平台通过浏览器访问,当然web端都是基于mobile的UI做的展示,雏形已成。由于用户体验以及人员的调整该项目由我接手继续的研发工作。
我个人比较善于native的开发,同时领导也希望我能够开发出native的版本,由于各种安全机制我们是不能在外网建设web服务器的。唯一外部可访问的是我们刚刚购买的salesforce云计算的平台。所以我要做的是要求native app数据访问sfdc。很顺利我已经完成了native 的原型版本。于此同时也展开的了一些思考。
html5 for mobile
不太懂技术的大老板总是会问,你现在做出来的版本可以支持其他平台嘛?以及自适应分辨率的问题。同时部门也有项目组在进行html5相关的尝试,传统企业往往不愿做新的尝试总是喜欢按部就班的做手头上的工作。但是这次不一样了,老大愿意投入较少的资源去尝试新兴的技术。
最近一段时间一直传言app store 审核周期长达3个月,并且有app无故下架的事件层出不穷。同样的事情也在我朋友的公司中出现了,并非空穴来风。
一些较有实力的互联网公司为了不依赖于app store,自行开发了网页版的程序,移动端只要通过浏览器访问就可以。于此同时近期Facebook CEO 扎克伯格在TC Disrupt大会上公开表示,押注HTML5是个巨大的错误,还提出了要以长远的眼光看待html5.也就是说他较早的投入于html5研发力量,忽略了用户体验等其他细节。
我同意html5目前是不可能替代的native的开发的,也相信html5在未来的不断发展中会有较大的发展,mobile 开发的未来的趋势就是html5.
2012-9-17