源文章地址【 http://blog.blundell-apps.com/tut-keep-screen-onawake-3-possible-ways/ 】
备注:
非直译,作者根据自己的说话习惯改了部分表述。
====
这篇教程将会告诉你如何防止android里activity的屏幕失效,这样你就可以在没有用户交互和屏幕休眠的情况下继续展示一些信息。
不到迫不得已的情况下不要使用WakeLock,除非你需要明确的操作设备硬件(处理器等等)。
一个需要保持屏幕常亮的场景类似于一个菜谱程序,你需要让菜谱显示在屏幕上而且用户不需要每隔几分钟触摸下屏幕来防止屏幕休眠。
防止屏幕休眠有3种可能的方法。其中两种方法比较不错,你可以根据自己的习惯来选择其中一种。第三种方法比较激进,适用于一些特殊的场景,比如你想让系统的其他模块同样保持工作状态(比如处理器)。
下面是三种方法的步骤:
1)在你的XML layout里面声明屏幕常亮
2)在onCreate函数里面通知window manager你需要屏幕常亮。
3)WakeLock–关键的下载或者处理期间,你很清楚此时android系统不能休眠。
小心并且仅在需要时才使用这些麻烦的WakeLock。参考 Battery Killers
ScreenOnFlagActivity.java
下面的这个Activity使用window manager来保持屏幕常亮,在整个Activity生命周期里manager都会有效,所以你不需要担心。屏幕可能会变暗但是不会休眠。你不需要在manifest里面申请权限。
package com.blundell.tut.ui.phone; import com.blundell.tut.R; import android.app.Activity; import android.os.Bundle; import android.view.WindowManager; /** * This Activity keeps the screen on using the window manager, you don't have to worry about managing this * it will be kept on for the duration of the Activity life. * No permissions are needed in your manifest. * @author paul.blundell */ public class ScreenOnFlagActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_flag); getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); } }
ScreenOnWakeLockActivity.java
这个Activity获得了一个wakelock来保持屏幕常亮。这你需要在mainfest里申请一个权限。管理好你的wakelock非常重要,当activity finished的时候释放掉他们(在 onPause 回调里)。
package com.blundell.tut.ui.phone; import com.blundell.tut.R; import android.app.Activity; import android.os.Bundle; import android.os.PowerManager; import android.os.PowerManager.WakeLock; /** * This Activity aquires a wakelock to keep the screen on whilst in this activity. * This requires a permission in your manifest * It is important to manage your wakelocks and always release them when finished * @author paul.blundell * */ public class ScreenOnWakeLockActivity extends Activity { private static final String TAG = "com.blundell.tut.ui.phone.ScreenOnWakeLockActivity.WAKE_LOCK_TAG"; private WakeLock wakeLock; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_wake_lock); PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE); wakeLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, TAG); } @Override protected void onResume() { super.onResume(); wakeLock.acquire(); } @Override protected void onPause() { super.onPause(); wakeLock.release(); } }
ScreenOnXmlActivity.java
这个activity通过在XML layout文件里增加一个android:keepScreenOn=”true”标志来保持屏幕常亮,你不需要在manifest里面申请权限。
package com.blundell.tut.ui.phone; import com.blundell.tut.R; import android.app.Activity; import android.os.Bundle; /** * This activity keeps the screen on using a flag in the XML layout file we are using 'android:keepScreenOn="true"' * We don't use a wakelock so no manifest permissions are needed * * @author paul.blundell * */ public class ScreenOnXmlActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_xml); } }
主activity用来在各个activity间进行跳转,需要注意的是他使用了另外一篇博文中的注解方法,这里再一次介绍了 onclick xml的用法和函数的调用位置。
MainActivity.java
package com.blundell.tut.ui.phone; import com.blundell.tut.R; import com.blundell.tut.annotations.FromXML; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @FromXML public void onFlagActivityClick(View v){ Intent intent = new Intent(this, ScreenOnFlagActivity.class); startActivity(intent); } @FromXML public void onXmlActivityClick(View v){ Intent intent = new Intent(this, ScreenOnXmlActivity.class); startActivity(intent); } @FromXML public void onWakeLockActivityClick(View v){ Intent intent = new Intent(this, ScreenOnWakeLockActivity.class); startActivity(intent); } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="onFlagActivityClick" android:text="Activity - Flag" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="onXmlActivityClick" android:text="Activity - Xml" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="onWakeLockActivityClick" android:text="Activity - WakeLock" /> </LinearLayout>
最后是androidmainfest,这里需要为WakeLockActivity申请wakelock权限,其他的activity不需要申请权限。
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.blundell.tut" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".ui.phone.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".ui.phone.ScreenOnFlagActivity" /> <activity android:name=".ui.phone.ScreenOnXmlActivity" /> <activity android:name=".ui.phone.ScreenOnWakeLockActivity" /> </application> </manifest>
结束啦~ 确保你根据你想达到的目标来使用上述的一种方法。不要过度的使用wakelock,(尽可能避免使用它)。申请的权限越少,用户选择安装程序的可能性越大。
源码下载
Eclipse source project: here
GitHub Repo: here
希望对你有所帮助,有问题尽管提.
发表评论