首先,需要启动一个后台服务,用于注册一个BroadcastReceiver 这个BroadcastReceiver用于监听Intent.ACTION_SCREEN_OFF 这样在发生这个事件Intent.ACTION_SCREEN_OFF 也就是用户锁屏或者屏幕上锁时触发。 Service中注册BroadcastReceiver代码 view plaincopy to clipboardprint?
package com.pingbao;
import android.app.KeyguardManager;
import android.app.Service;
import android.app.KeyguardManager.KeyguardLock;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;
public class ZyScreenService extends Service {
KeyguardManager mKeyguardManager = null;
private KeyguardLock mKeyguardLock = null;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate()
{
// TODO Auto-generated method stub

super.onCreate();
}
@Override
public void onStart(Intent intent, int startId)
{
// TODO Auto-generated method stub
Log.i("in Service","in Service");
mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
mKeyguardLock = mKeyguardManager.newKeyguardLock("");
mKeyguardLock.disableKeyguard();
Log.i("in Service1","in Service1");
BroadcastReceiver mMasterResetReciever = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
try {
Intent i = new Intent();
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setClass(context, ZyScreenSaver.class);
context.startActivity(i);
// finish();
Log.i("BroadcastReceiver","BroadcastReceiver");
} catch (Exception e) {
Log.i("Output:", e.toString());
}
}
};
registerReceiver(mMasterResetReciever, new IntentFilter(
Intent.ACTION_SCREEN_OFF));
}

}
然后在我们启动的ZyScreenSaver这个屏保界面Activity里写上 view plaincopy to clipboardprint?
package com.pingbao;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
public class ZyScreenSaver extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
Log.i("MyScreenSaver", "MyScreenSaver");
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
super.onKeyDown(keyCode, event);
if (keyCode == KeyEvent.KEYCODE_BACK) {
Log.i("4", "4");
showDialog(1);
}
// finish();
return true;
}
protected Dialog onCreateDialog(int id) {
switch (id) {
case 1:
return new AlertDialog.Builder(ZyScreenSaver.this).setIcon(
R.drawable.icon).setTitle("退出屏保").setMessage(
"是否退出屏保").setPositiveButton("确定",
new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
finish();
}
})
.setNegativeButton("取消",
new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
removeDialog(1);

}
})
.create();
}
return null;
}
}
然后在我们的主界面里写上启动服务就可以了 view plaincopy to clipboardprint?
package com.pingbao;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
public class Zypingbao extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i("pingbao","pingbao");
Intent mService=new Intent(Zypingbao.this,ZyScreenService.class);//启动服务
mService.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startService(mService);


}
}
在Manifest里贴权限 view plaincopy to clipboardprint?
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />

更多相关文章

  1. Android动态获取json解析后显示到RecyclerView
  2. android TextView 如何动态获取宽度
  3. Android发送dtmf键盘事件(模拟通话界面键盘事件)
  4. Android(安卓)Studio Fragment问题与解决
  5. Sina微博的开发心得-1 logo界面
  6. Android(安卓)4.2.2 ScreenLock上Music远程播放控制界面的创建流
  7. Android判断某个Activity是否正在前台显示
  8. Android界面设计
  9. Android中获取实时网速(2)

随机推荐

  1. Android(安卓)Java 获取剪切板的内容,MD5
  2. Android视频处理 --处理视频第一帧缩略图
  3. Android——eclipse共享library以及导出j
  4. Android 只开启一个Activity实例
  5. android 设置EditText和TextView不可见
  6. Android 6.0替换原系统Launcher傻瓜教程
  7. Android中的快捷键
  8. Android跨进程唤醒APP,启动指定页面
  9. AndroidStudio开发遇见问题总结
  10. Android动画之Animation