在2.3版本以下重写下面方法就能重写home键
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
}
PS:
在AndroidMainfest.xml需要加权限:
<uses-permission android:name = "android.permission.DISABLE_KEYGUARD"/><!-- 屏蔽HOME键需要的权限 -->
在配置文件中,在你使用了Notification的activity中加一个属性android: android:launchMode="singleInstance"
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:background="#000000" tools:context=".MainActivity" > <ImageView android:id="@+id/iv_drag" android:layout_centerInParent="true" android:src="@drawable/lock_slide_icon_normal_no_quick_launcher" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <ImageView android:layout_alignParentLeft="true" android:id="@+id/iv_left" android:layout_centerInParent="true" android:src="@drawable/lock_left_download_icon_normal" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <ImageView android:layout_alignParentRight="true" android:id="@+id/iv_right" android:layout_centerInParent="true" android:src="@drawable/lock_right_icon_normal" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </RelativeLayout>
img.setOnTouchListener(new OnTouchListener() { private int height; public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // leftImageView_left:32rightImageView_right:688 leftImageView_left = leftImageView.getLeft();<span style="font-family: KaiTi_GB2312;">① 左边图片的左边界 距离屏幕左边距离</span> leftImageView_right = leftImageView.getRight();<span style="font-family: KaiTi_GB2312;">① 左边图片 右边界 距离屏幕左边的距离</span> rightImageView_right = rightImageView.getRight();<span style="font-family: KaiTi_GB2312;">① 右两边图片的右边界距离屏幕左边的距离</span> rightImageView_left = rightImageView.getLeft();<span style="font-family: KaiTi_GB2312;">① 右两边图片的左边 距离屏幕左边的距离</span> System.out.println("leftImageView_left:" + leftImageView_left + "rightImageView_right:" + rightImageView_right); img.setImageDrawable(getResources().getDrawable(R.drawable.lock_slide_icon_pressed));//改一下 圆环 大的 变成小的 System.out.println("event.getRawX()" + event.getRawX() + "event.getRawY()" + event.getRawY()); System.out.println("event.getX()" + event.getX() + "event.getY()" + event.getY()); height = (int) (event.getRawY() - 50); System.out.println("v.getTop()" + v.getTop() + "event.getBottom()" + v.getBottom() + "v.getLeft()" + v.getLeft() + "v.getRight" + v.getRight()); left = v.getLeft(); right = v.getRight(); top = v.getTop(); bottom = v.getBottom(); break; case MotionEvent.ACTION_MOVE: System.out.println("----------event.getRawX()" + event.getRawX() + "event.getRawY()" + event.getRawY()); System.out.println("----------event.getX()" + event.getX() + "event.getY()" + event.getY()); mx = (int) (event.getRawX()); //② 记录 中间圆环距离屏幕左边界距离 my = (int) (event.getRawY() - 50);//<span style="font-family:KaiTi_GB2312;">② 记录 中间圆环距离屏幕上边界距离</span> Log.i(TAG1, " mx " + mx + " my" + my + " img.getWidth()/2" + img.getWidth() / 2 + " img.getHeight()/2" + img.getHeight() / 2); if (mx < width / 2) {// ②这里的 with 是手机屏幕的宽度,如果 圆环距离屏幕左边距离小于手机屏幕宽度的二分之一 说明向左滑动 if (mx < leftImageView_right) {// ②这里是判断 有没有滑动到左边图片的右边,如果 小于 说明 圆环 跟左边的图片 已经发生了接触,一旦接触 让 圆环移动到已左边图片中心的位置。 v.layout(leftImageView_left, top, leftImageView_right, bottom); left_flag = true //② 标志 如果到了这个位置 代表滑到最左边了,此时松手 会解锁屏幕 } else {// 没有接触 那就 按移动的位置去 重新绘制 圆环 layout(l ,t, r, b) // ②左 上 右 下 四个坐标 v.layout(mx - img.getWidth() / 2, top, mx + img.getWidth() / 2, bottom); left_flag = false; } } else if (mx > width / 2) {//②<span style="font-family: KaiTi_GB2312;">这里的 with 是手机屏幕的宽度,如果 圆环距离屏幕左边距离大于手机屏幕宽度的二分之一 说明向右滑动 </span> if ((mx + img.getWidth() / 2) < rightImageView_right) { // 右边跟左边同理,。。。 v.layout(mx - img.getWidth() / 2, top, mx + img.getWidth() / 2, bottom); Log.i(TAG2, " int l " + (mx - img.getWidth() / 2) + " int top" + (my - img.getHeight() / 2) + " int right" + (mx + img.getWidth() / 2) + " int bottom" + (my + img.getHeight() / 2)); }// 688 if (mx > rightImageView_left) { v.layout(rightImageView_left, top, rightImageView_right, bottom); right_flag = true; } else { v.layout(mx - img.getWidth() / 2, top, mx + img.getWidth() / 2, bottom); right_flag = false; } } break; case MotionEvent.ACTION_UP: if (right_flag) {// ③ 右边解锁 Toast.makeText(mContext, "解锁右边", 0).show(); Intent i = new Intent(mContext, MyService.class); i.setAction(MyService.UNLOCK_ACTION); // ***********************这里 是解锁的关键。 到服务里面详细说************* mContext.startService(i); } else if (left_flag) { Toast.makeText(mContext, "解锁左边", 0).show(); Intent i2 = new Intent(mContext, MyService.class); i2.setAction(MyService.UNLOCK_ACTION); mContext.startService(i2); } right_flag = false; left_flag = false; v.layout(left, top, right, bottom); // ③恢复圆环的初始位置 img.setImageDrawable(getResources().getDrawable(R.drawable.lock_slide_icon_normal_no_quick_launcher));// ③将圆环 换回原来的 大圆环 break; } return true; } });
MotionEvent.ACTION_DOWN
MotionEvent.ACTION_MOVE
<strong style="font-family: KaiTi_GB2312; background-color: rgb(255, 255, 255);"></strong><pre name="code" class="java" style="display: inline !important;">MotionEvent.ACTION_UP
<strong style="font-family: KaiTi_GB2312; background-color: rgb(255, 255, 255);"></strong><pre name="code" class="java" style="display: inline !important;">
<strong style="font-family: KaiTi_GB2312; background-color: rgb(255, 255, 255);"></strong><pre name="code" class="java" style="display: inline !important;"><span style="font-family: KaiTi_GB2312;"></span>
<strong style="font-family: KaiTi_GB2312; background-color: rgb(255, 255, 255);"></strong><pre name="code" class="java" style="display: inline !important;"><span style="font-family: KaiTi_GB2312;"> </span>
RawX()与getX() 区别。 一张图
public int onStartCommand(Intent intent, int flags, int startId) { if(intent!=null){ System.out.println("intent------------intent.getAction()-==null?????--"+(intent==null)); String action = intent.getAction(); if(TextUtils.equals(action, LOCK_ACTION)) // 这是屏蔽 home 键的关键 addView(); else if(TextUtils.equals(action, UNLOCK_ACTION)) // 这里是解锁屏幕 { removeView(); // stopSelf(); } } return Service.START_STICKY; }
Intent i = new Intent(mContext, MyService.class); i.setAction(MyService.UNLOCK_ACTION); // ***********************这里 是解锁的关键。 到服务里面详细说************* mContext.startService(i);传过去一个 Action 代表 是解锁
public void onCreate() { super.onCreate(); mContext = getApplicationContext(); mWinMng = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE); i = new Intent(mContext, MyService.class); i.setAction(MyService.LOCK_ACTION); zdLockIntent = new Intent(MyService.this , MyService.class); zdLockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); /*注册广播*/ IntentFilter mScreenOnFilter = new IntentFilter("android.intent.action.SCREEN_ON"); MyService.this.registerReceiver(mScreenOnReceiver, mScreenOnFilter); /*注册广播*/ IntentFilter mScreenOffFilter = new IntentFilter("android.intent.action.SCREEN_OFF"); MyService.this.registerReceiver(mScreenOffReceiver, mScreenOffFilter); }
//屏幕变暗/变亮的广播 , 我们要调用KeyguardManager类相应方法去解除屏幕锁定 private BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver(){ @Override public void onReceive(Context context , Intent intent) { String action = intent.getAction() ; Log.i(TAG, intent.toString()); if(action.equals("android.intent.action.SCREEN_OFF") || action.equals("android.intent.action.SCREEN_ON") ){ mKeyguardManager = (KeyguardManager)context.getSystemService(Context.KEYGUARD_SERVICE); mKeyguardLock = mKeyguardManager.newKeyguardLock("zdLock 1"); mKeyguardLock.disableKeyguard(); // startService(zdLockIntent); //Intent.FLAG_ACTIVITY_NEW_TASK. This flag is generally used by activities that want to present a "launcher" style behavior startService(i); } } };
//屏幕变亮的广播,我们要隐藏默认的锁屏界面 private BroadcastReceiver mScreenOnReceiver = new BroadcastReceiver(){ @Override public void onReceive(Context context , Intent intent) { Log.i(TAG, intent.getAction()); if(intent.getAction().equals("android.intent.action.SCREEN_ON")){ Log.i(TAG, "----------------- android.intent.action.SCREEN_ON------"); mKeyguardManager = (KeyguardManager)context.getSystemService(Context.KEYGUARD_SERVICE); mKeyguardLock = mKeyguardManager.newKeyguardLock("zdLock 1"); mKeyguardLock.disableKeyguard(); //Intent.FLAG_ACTIVITY_NEW_TASK. This flag is generally used by activities that want to present a "launcher" style behavior startService(i); } } };
<uses-permission android:name = "android.permission.DISABLE_KEYGUARD"/><!-- 屏蔽HOME键需要的权限 --> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.DISABLE_KEYGUARD"/> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:theme="@android:style/Theme.Translucent" android:name="com.example.mylock.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".BootCompletedReciever" android:permission="android.permission.RECEIVE_BOOT_COMPLETED" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </receiver> <service android:name=".MyService" > </service> </application>
原文地址:http://blog.csdn.net/u011733020/article/details/41863861