您的位置:首页 > 其它

锁屏后会自动唤醒并播放----遇到的问题及解决方案

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,如下:
<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()方法,所以锁屏会启动
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐