android 4.0后不允许屏蔽掉home键

屏蔽home键的方法

// 屏蔽掉Home键

public void onAttachedToWindow() {

this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);

super.onAttachedToWindow();

}

下面是实例demo

android4.0以上版本运行之后就会报错 java.lang.IllegalArgumentException: Window type can not be changed after the window is added.

android4.0以下就没有问题

代码

package com.happylock;

import java.sql.Date;

import java.text.SimpleDateFormat;

import android.app.Activity;

import android.content.Intent;

import android.graphics.Bitmap;

import android.graphics.drawable.AnimationDrawable;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.util.Log;

import android.view.KeyEvent;

import android.view.Window;

import android.view.WindowManager;

import android.widget.ImageView;

import android.widget.TextView;

import com.happylock.R;

import com.happylock.service.ZdLockService;

import com.happylock.util.DateUtil;

import com.happylock.util.ImageUtil;

public class LockActivity extends Activity {

private static String TAG = "QINZDLOCK";

private SliderRelativeLayout sliderLayout = null;

private WindowManager manager;

private ImageView dot_left;

private ImageView dot_right;

private AnimationDrawable animArrowDrawable = null;

private AnimationDrawable animArrowDrawable1 = null;

private TextView tv_date = null;

private TextView tv_date1 = null;

private TextView tv_time = null;

private static final int msgKey1 = 1;

public static int MSG_LOCK_SUCESS = 1;

public static final int FLAG_HOMEKEY_DISPATCHED = 0x80000000;//android 4.0版本以上的屏蔽home键

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

requestWindowFeature(Window.FEATURE_NO_TITLE);

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

this.getWindow().setFlags(FLAG_HOMEKEY_DISPATCHED, FLAG_HOMEKEY_DISPATCHED);//android 4.0版本以上的屏蔽home键 关键代码

super.onCreate(savedInstanceState);

setContentView(R.layout.lockactivity_main);

manager = (WindowManager) getSystemService(WINDOW_SERVICE);

tv_date = (TextView) findViewById(R.id.tv_date);

tv_date1 = (TextView) findViewById(R.id.tv_date1);

tv_time = (TextView) findViewById(R.id.tv_time);

new TimeThread().start();

// 设置图片

Bitmap bitmap = ImageUtil.scaleImage(manager, getResources(),

R.drawable.dot_line_left, 50, 50);

Bitmap bitmap1 = ImageUtil.scaleImage(manager, getResources(),

R.drawable.dot_line_right, 50, 50);

dot_left = (ImageView) findViewById(R.id.dot_left);

dot_right = (ImageView) findViewById(R.id.dot_right);

dot_left.setImageBitmap(bitmap);

dot_right.setImageBitmap(bitmap1);

initViews();

startService(new Intent(LockActivity.this, ZdLockService.class));

sliderLayout.setMainHandler(mHandler);

}

private void initViews() {

sliderLayout = (SliderRelativeLayout) findViewById(R.id.slider_layout);

// 获得动画,并开始转动

animArrowDrawable = new AnimationDrawable();

animArrowDrawable1 = new AnimationDrawable();

}

@Override

protected void onResume() {

super.onResume();

// 设置动画

mHandler.postDelayed(AnimationDrawableTask, 300); // 开始绘制动画

}

@Override

protected void onPause() {

super.onPause();

animArrowDrawable.stop();

animArrowDrawable1.stop();

}

protected void onDestory() {

super.onDestroy();

}

// 通过延时控制当前绘制bitmap的位置坐标

private Runnable AnimationDrawableTask = new Runnable() {

public void run() {

animArrowDrawable.start();

animArrowDrawable1.start();

mHandler.postDelayed(AnimationDrawableTask, 300);

}

};

private Handler mHandler = new Handler() {

public void handleMessage(Message msg) {

Log.i(TAG, "handleMessage :  #### ");

if (MSG_LOCK_SUCESS == msg.what)

finish(); // 锁屏成功时,结束我们的Activity界面

}

};

// 屏蔽掉Home键

public void onAttachedToWindow() {

this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);

super.onAttachedToWindow();

}

// 屏蔽掉Back键

public boolean onKeyDown(int keyCode, KeyEvent event) {

if (event.getKeyCode() == KeyEvent.KEYCODE_BACK){

return true;

}else{

return super.onKeyDown(keyCode, event);

}

}

// 开启一个线程,动态显示时间

public class TimeThread extends Thread {

@Override

public void run() {

do {

try {

Thread.sleep(1000);

Message msg = new Message();

msg.what = msgKey1;

timeHandler.sendMessage(msg);

} catch (InterruptedException e) {

e.printStackTrace();

}

} while (true);

}

}

private Handler timeHandler = new Handler() {

@Override

public void handleMessage(Message msg) {

super.handleMessage(msg);

switch (msg.what) {

case msgKey1:

Date curDate = new Date(System.currentTimeMillis());// 获取当前时间

// 设置年-月-日

String str = new SimpleDateFormat("yyyy-MM-dd").format(curDate);

// 设置周几

String str1 = DateUtil.DateToWeek(curDate);

// 设置时:分

String str2 = new SimpleDateFormat("HH:mm").format(curDate);

tv_date.setText(str);

tv_date1.setText(str1);

tv_time.setText(str2);

break;

default:

break;

}

}

};

}

解决方法http://blog.csdn.net/com360/article/details/6663586

上一篇:Ceph RGW 和 niginx 配置要点


下一篇:23、Interpreter 解释器模式