锁屏后会自动唤醒并播放----遇到的问题及解决方案
2018-03-22 19:18
357 查看
1、保证屏幕常亮,不暗屏、黑屏
方法一:xml配置在res/values/style.xml中添加一个style,如下:<!--
设置屏幕常亮
-->
<style name="ThemeScreenOn" parent="AppTheme">
<item name="android:keepScreenOn">true</item>
</style>然后在AndroidManifest.xml中引用我们定义的style,如下:
方法二:代码配置//屏幕常量
Window window = this.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
2、锁屏后会自动唤醒并播放演示(只要进程不杀死)
注意:屏幕灭屏的广播必须是动态的,静态的不好用
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// 屏幕唤醒
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP |
PowerManager.SCREEN_DIM_WAKE_LOCK, "StartupReceiver");//最后的参数是LogCat里用的Tag
wl.acquire();
// 屏幕解锁
KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock kl = km.newKeyguardLock("StartupReceiver");//参数是LogCat里用的Tag
kl.disableKeyguard();
// 开机启动
Intent mainIntent = new Intent(context, VideoShowactivity.class);
//在BroadcastReceiver中显示Activity,必须要设置FLAG_ACTIVITY_NEW_TASK标志
mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mainIntent);
}
};
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(receiver, filter);
}
当按Home键的时候,锁屏自启动(没有走onDestroy()方法),但是点击返回键时锁屏没有自启动(因为走了onDestroy()方法)
解决方案:
处理返回键:点击2次才退出应用的,
// 记录用户首次点击返回键的时间
private long firstTime = 0;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
if (System.currentTimeMillis() - firstTime > 2000) {
Toast.makeText(VideoShowactivity.this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
firstTime = System.currentTimeMillis();
} else {
// finish();//正常走的逻辑(退出----锁屏不会起来)
//以下是方案
Intent home = new Intent(Intent.ACTION_MAIN);
home.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
home.addCategory(Intent.CATEGORY_HOME);
startActivity(home);
}
return true;
}
return super.onKeyDown(keyCode, event);
}
ps:回到了桌面,但是么有走onDestroy()方法,所以锁屏会启动
方法一:xml配置在res/values/style.xml中添加一个style,如下:<!--
设置屏幕常亮
-->
<style name="ThemeScreenOn" parent="AppTheme">
<item name="android:keepScreenOn">true</item>
</style>然后在AndroidManifest.xml中引用我们定义的style,如下:
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/ThemeScreenOn"> ... </application>
方法二:代码配置//屏幕常量
Window window = this.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /**** * 设置手机屏幕常亮 */ getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); setContentView(R.layout.activity_main); } }
2、锁屏后会自动唤醒并播放演示(只要进程不杀死)
注意:屏幕灭屏的广播必须是动态的,静态的不好用
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// 屏幕唤醒
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP |
PowerManager.SCREEN_DIM_WAKE_LOCK, "StartupReceiver");//最后的参数是LogCat里用的Tag
wl.acquire();
// 屏幕解锁
KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock kl = km.newKeyguardLock("StartupReceiver");//参数是LogCat里用的Tag
kl.disableKeyguard();
// 开机启动
Intent mainIntent = new Intent(context, VideoShowactivity.class);
//在BroadcastReceiver中显示Activity,必须要设置FLAG_ACTIVITY_NEW_TASK标志
mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mainIntent);
}
};
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(receiver, filter);
}
当按Home键的时候,锁屏自启动(没有走onDestroy()方法),但是点击返回键时锁屏没有自启动(因为走了onDestroy()方法)
解决方案:
处理返回键:点击2次才退出应用的,
// 记录用户首次点击返回键的时间
private long firstTime = 0;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
if (System.currentTimeMillis() - firstTime > 2000) {
Toast.makeText(VideoShowactivity.this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
firstTime = System.currentTimeMillis();
} else {
// finish();//正常走的逻辑(退出----锁屏不会起来)
//以下是方案
Intent home = new Intent(Intent.ACTION_MAIN);
home.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
home.addCategory(Intent.CATEGORY_HOME);
startActivity(home);
}
return true;
}
return super.onKeyDown(keyCode, event);
}
ps:回到了桌面,但是么有走onDestroy()方法,所以锁屏会启动
相关文章推荐
- 【iOS-Cocos2d游戏开发之十八】解决滚屏背景/拼接地图有黑边(缝隙)/动画播放出现毛边以及禁止游戏中自动锁屏问题!
- 解决滚屏背景/拼接地图有黑边(缝隙)/动画播放出现毛边以及禁止游戏中自动锁屏问题!
- 【iOS-Cocos2d游戏开发之十八】解决滚屏背景/拼接地图有黑边(缝隙)/动画播放出现毛边以及禁止游戏中自动锁屏问题!【2011年12月18日补充】
- Mysql服务启动遇到“某些服务启动后自动停止”的问题的解决方案
- SurfaceView+MediaPlayer 播放视频 锁屏 没有画面等各种问题的解决方案。
- 【iOS-Cocos2d游戏开发之十八】解决滚屏背景/拼接地图有黑边(缝隙)/动画播放出现毛边以及禁止游戏中自动锁屏问题!【2011年12月18日补充】
- 【iOS-Cocos2d游戏开发之十八】解决滚屏背景/拼接地图有黑边(缝隙)/动画播放出现毛边以及禁止游戏中自动锁屏问题!【2011年12月18日补充】
- CentOS遇到Qt编译问题的解决方案(error: cannot find -lGL)
- android 开发中遇到的错误及解决方式(更新R.java文件不能自动更新问题)
- CentOS 7编译安装php7.0.7以及可能遇到的问题的解决方案
- IOS 打包所遇到的问题以及解决方案
- 关于animation PlayState在ios上不起作用, 以及ios前端页面音乐不能自动播放的问题
- hadoop-2.7.3搭建过程中遇到的问题及解决方案
- 接触linux系统中遇到的各个问题及解决方案
- 搭建springMVC+spring+mybatis遇到的问题及解决方案
- 关于流复制中可能遇到的问题及解决方案(一)捕获阶段。
- WIFI在手机锁屏后,自动断开的问题
- WebView加载h5页面播放音乐或视频 返回,按锁屏键,按home键 播放不停止问题
- 搭建EF6.0+MVC4搭建框架遇到的问题及解决方案
- 上网本 Ubuntu下安装android遇到的问题及解决方案