下面具体介绍一下实现过程:
一 、5秒不操作自动锁屏界面
这个界面非常简单,就一个TextView,直接贴代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="30sp"
android:gravity="center"
android:textStyle="bold"
android:textColor="#698048"
android:text="5秒不操作自动锁屏"
/>
</RelativeLayout>
二 、具体实现逻辑
首先看MainPresenter类,是计时具体的实现逻辑类,主要是通过Runnable来实现操作,当无操作时5秒后会执行Runnable,跳转到锁屏界面,5秒内有操作会重置计时,具体代码如下:
public class MainPresenter
{
public final static int MSG_SHOW_TIPS = 0x01;
private IMainView mMainView;
private MainHandler mMainHandler;
private boolean tipsIsShowed = true;
private Runnable tipsShowRunable = new Runnable()
{
@Override
public void run()
{
mMainHandler.obtainMessage(MSG_SHOW_TIPS).sendToTarget();
}
};
public MainPresenter(IMainView view)
{
mMainView = view;
mMainHandler = new MainHandler();
}
/**
*
* <无操作时开始计时>
* <功能详细描述>
*
* @see [类、类#方法、类#成员]
*/
public void startTipsTimer()
{
mMainHandler.postDelayed(tipsShowRunable, 5000);
}
/**
*
* <结束当前计时,重置计时>
* <功能详细描述>
*
* @see [类、类#方法、类#成员]
*/
public void endTipsTimer()
{
mMainHandler.removeCallbacks(tipsShowRunable);
}
public void resetTipsTimer()
{
tipsIsShowed = false;
mMainHandler.removeCallbacks(tipsShowRunable);
mMainHandler.postDelayed(tipsShowRunable, 5000);
}
public class MainHandler extends Handler
{
@Override
public void handleMessage(Message msg)
{
super.handleMessage(msg);
switch (msg.what)
{
case MSG_SHOW_TIPS:
mMainView.showTipsView();
tipsIsShowed = true;
// 屏保显示,两秒内连续按下键盘Enter键可关闭屏保
break;
}
}
}
}
三 、MainActivity.java是主页面
主要功能是重写OnKeyDown方法,监听键盘按下事件,一旦触发键盘事件,就会重置计时,另外在onResume()方法中默认开始计时,showTipsView()方法是计时完成弹出屏保界面。
public class MainActivity extends Activity implements IMainView
{
private MainPresenter mPresenter;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPresenter = new MainPresenter(this);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
//重置计时
mPresenter.resetTipsTimer();
return false;
}
@Override
protected void onResume()
{
//启动默认开始计时
mPresenter.startTipsTimer();
super.onResume();
}
@Override
protected void onPause()
{
//有其他操作时结束计时
mPresenter.endTipsTimer();
super.onPause();
}
@Override
public void showTipsView()
{
//展示屏保界面
Intent intent = new Intent(MainActivity.this, MyTimeActivity.class);
startActivity(intent);
}
}
另外大家看到MainActivity实现了IMainView接口,IMainView很简单,就封装了一个showTipsView()方法,MainActivity实现了该方法而已,IMainView.java代码如下。
public interface IMainView
{
public void showTipsView();
}
四 、屏保界面MyTimeActivity.java
MyTimeActivity界面很简单,就是一个背景图,外加一个系统时间显示,还有就是两秒内连续两次按下回车键会退出屏保界面的逻辑,具体代码如下,关键部分都加了注释:
public class MyTimeActivity extends Activity
{
private static final int MSGKEY = 0x10001;
private long exitTime = 0;
//屏保右上角的系统时间
private TextView mTime;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.lock_screen);
mTime = (TextView)findViewById(R.id.mytime);
//启动线程刷新屏保界面右上角的时间
new TimeThread().start();
}
public class TimeThread extends Thread
{
@Override
public void run()
{
do
{
try
{
//更新时间
Thread.sleep(1000);
Message msg = new Message();
msg.what = MSGKEY;
mHandler.sendMessage(msg);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
} while (true);
}
}
/**
* 2秒内连续两次按下回车键退出屏保
* 重写方法
* @param keyCode
* @param event
* @return
* @see android.app.Activity#onKeyDown(int, android.view.KeyEvent)
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_ENTER)
{
exit();
return false;
}
return super.onKeyDown(keyCode, event);
}
public void exit()
{
if ((System.currentTimeMillis() - exitTime) > 2000)
{
Toast.makeText(getApplicationContext(), "再按一次解锁键即可进入应用", Toast.LENGTH_SHORT).show();
exitTime = System.currentTimeMillis();
}
else
{
finish();
}
}
private Handler mHandler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
super.handleMessage(msg);
switch (msg.what)
{
case MSGKEY:
long sysTime = System.currentTimeMillis();
CharSequence sysTimeStr = DateFormat.format("hh:mm", sysTime);
mTime.setText(sysTimeStr);
break;
}
}
};
}
原文地址:http://blog.csdn.net/wangkeke1860/article/details/46611969