SurfaceView SurfaceHolder MediaPlayer Service BroadcastReceiver KeyguardManager PowerManager
思路:启动一个服务,监听灭屏广播,当收到广播的时候,点亮屏幕,禁用锁屏,调用使用SurfaceView播放视频。
<service android:name=".LockService" > <intent-filter> <category android:name="android.intent.category.default" /> </intent-filter> </service>
Intent intent = new Intent(); intent.setClass(this,LockService.class); startService(intent);
final IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_SCREEN_OFF); registerReceiver(mReceiver, filter);
private final BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); if (Intent.ACTION_SCREEN_OFF.equals(action)) { Log.d("yzy","screen_off..."); if(HelloActivity.isScreenOff == false){ wakeUpScreen(); Intent intent1 = new Intent(); intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent1.setClass(LockService.this, HelloActivity.class);//启动屏保界面 startActivity(intent1); HelloActivity.isScreenOff = true; } } } };
KeyguardManager mKeyguardManager = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE); KeyguardLock mKeyguardLock = mKeyguardManager.newKeyguardLock(""); mKeyguardLock.disableKeyguard();//解锁屏幕,也就是 关闭 屏幕 锁定 功能 PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE); PowerManager.WakeLock mWakeLock = pm.newWakeLock( PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "SimpleTimer"); mWakeLock.acquire(); //释放屏幕常亮锁 if(null != mWakeLock) { //可以灭屏了 // mWakeLock.release(); }
public class WallPaperSurfaceView extends SurfaceView implements Callback { private SurfaceHolder mSurfaceHolder; public MediaPlayer mediaPlayer; String tag = "BackgroundSurfaceView"; private HelloActivity helloActivity; @Override protected void onWindowVisibilityChanged(int visibility) { // TODO Auto-generated method stub super.onWindowVisibilityChanged(visibility); } @Override public boolean onTouchEvent(MotionEvent event) { helloActivity.finish(); HelloActivity.isScreenOff = false; return super.onTouchEvent(event); } public WallPaperSurfaceView(Context context, AttributeSet attrs) { super(context, attrs); helloActivity = (HelloActivity) context; mSurfaceHolder = getHolder(); //设置SurfaceView自己不管理的缓冲区 mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); mSurfaceHolder.addCallback(this); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceCreated(SurfaceHolder holder) { Log.d("yzy","surfaceCreated....."); mediaPlayer=new MediaPlayer(); play(); } @Override public void surfaceDestroyed(SurfaceHolder holder) { holder.removeCallback(this); if(mediaPlayer!=null ) { mediaPlayer.release(); mediaPlayer = null; } } private void play() { try { if(mediaPlayer == null) { mediaPlayer = new MediaPlayer(); } mediaPlayer.reset(); mediaPlayer .setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setScreenOnWhilePlaying(true); //设置需要播放的视频 mediaPlayer.setDataSource("/mnt/sdcard/sun.mp4"); mediaPlayer.setDisplay(mSurfaceHolder); mediaPlayer.prepare(); mediaPlayer.setLooping(true); //播放 mediaPlayer.start(); } catch (Exception e) { Log.e(tag, "kc" , e); } } }