MainActivity.java:
package com.example.skyworth; import java.io.IOException; import java.lang.reflect.Method; import com.example.skyworth.R; import android.media.MediaPlayer; import android.net.Uri; import android.os.Bundle; import android.os.storage.StorageManager; import android.provider.Settings; import android.provider.Settings.SettingNotFoundException; import android.app.Activity; import android.app.ActivityManager; import android.app.KeyguardManager; import android.content.ActivityNotFoundException; import android.content.Context; import android.content.Intent; import android.view.KeyEvent; import android.view.Menu; import android.view.MotionEvent; import android.view.Window; import android.view.WindowManager; import android.widget.Toast; import android.widget.VideoView; public class MainActivity extends Activity { public VideoView vv; public static boolean isShow = false; @Override protected void onCreate(Bundle savedInstanceState) { this.requestWindowFeature(Window.FEATURE_NO_TITLE); this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); getWindow().setFlags( WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); //luke add super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startService(new Intent(this, LockScreenService.class)); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub System.out.println("keyCode:"+keyCode); return super.onKeyDown(keyCode, event); } @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub return super.onTouchEvent(event); } @Override protected void onResume() { // TODO Auto-generated method stub isShow = true; super.onResume(); /* try { Settings.System.putInt(getContentResolver(),android.provider.Settings.System.SCREEN_OFF_TIMEOUT,-1); float result = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT); System.out.println("timeout:"+result); } catch (SettingNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ System.out.println("main activity resume"); // �q�ϵͳ���q� KeyguardManager manager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE); KeyguardManager.KeyguardLock lock = manager .newKeyguardLock("KeyguardLock"); lock.disableKeyguard(); playVideo(); float result; try { result = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT); System.out.println("resume timeout:"+result); } catch (SettingNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override protected void onStop() { // TODO Auto-generated method stub isShow = false; super.onStop(); Settings.System.putInt(getContentResolver(),android.provider.Settings.System.SCREEN_OFF_TIMEOUT,5*60000); float result; try { result = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT); System.out.println("stop timeout:"+result); } catch (SettingNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } stopVideo(); if(LockScreenService.mWakeLock != null) { LockScreenService.mWakeLock.release(); LockScreenService.mWakeLock = null; } /* if(LockScreenService.mWakeLock.isHeld()) { LockScreenService.mWakeLock.release(); }*/ } void playVideo() { String videoUri = "android.resource://com.example.skyworth/" + R.raw.video2; vv = (VideoView)findViewById(R.id.videoView); //vv.setVideoPath("/sdcard/video2.mp4"); vv.setVideoURI(Uri.parse(videoUri)); vv.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { // TODO Auto-generated method stub mp.setLooping(true); try { mp.prepare(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } mp.start(); } }); vv.start(); } void stopVideo() { vv.stopPlayback(); } }
package com.example.skyworth; import java.util.List; import android.app.ActivityManager; import android.app.Service; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.Uri; import android.os.IBinder; import android.os.PowerManager; import android.os.PowerManager.WakeLock; import android.util.Log; import android.widget.VideoView; public class LockScreenService extends Service { private final String ACT_SCREEN_OFF = "android.intent.action.SCREEN_OFF"; //private final String ACT_SCREEN_ON = "android.intent.action.SCREEN_ON"; //private final String ACT_TIME_TICK = "android.intent.action.TIME_TICK"; public static WakeLock mWakeLock; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); // register Broadcast System.out.println("service started"); mWakeLock = null; IntentFilter intentFilter = new IntentFilter(ACT_SCREEN_OFF); registerReceiver(mScreenBCR, intentFilter); /* IntentFilter intentFilter2 = new IntentFilter(ACT_TIME_TICK); registerReceiver(mScreenBCR, intentFilter2);*/ } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); // unregisterReceiver(mScreenBCR); } private BroadcastReceiver mScreenBCR = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub System.out.println("onReceive receive intent:"+intent); System.out.println("act:"+intent.getAction()); if(intent.getAction().equals(ACT_SCREEN_OFF)) { System.out.println("---------screen off"); try { if(mWakeLock == null) { PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "SimpleTimer"); mWakeLock.acquire(); } if(!MainActivity.isShow) { Intent i = new Intent(); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); i.setClass(context, MainActivity.class); context.startActivity(i); } } catch (Exception e) { // TODO: handle exception } } else //if(intent.getAction().equals(ACT_TIME_TICK)) { System.out.println("---------other:"+intent.getAction()); //mUpdateTimeListener.updateTime(); } } }; /* private UpdateTimeListener mUpdateTimeListener; public interface UpdateTimeListener { public void updateTime(); }*/ }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" tools:context=".MainActivity" > <VideoView android:id="@+id/videoView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" > </VideoView> </RelativeLayout>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.skyworth" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="14" /> <uses-permission android:name="android.permission.DISABLE_KEYGUARD"/> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.GET_TASKS" /> <uses-permission android:name="android.permission.WRITE_SETTINGS" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.skyworth.MainActivity" android:label="@string/app_name" android:screenOrientation="landscape" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name="com.example.skyworth.LockScreenService" > <intent-filter> <action android:name="android.intent.action.SCREEN_OFF" /> <!-- <action android:name="android.intent.action.TIME_TICK" /> --> </intent-filter> </service> </application> </manifest>