android 仿三星I900滑动解锁

来源:互联网 发布:阿里云 按流量计费 编辑:程序博客网 时间:2024/05/04 22:57
  

三星I900滑动解锁看起来就像一层窗户纸,拉起纸来就能看到当前正在运行的activity程序,这是很有意思的。但其实现原理不可知,我们只能观测他的行为,根据掌握的可用API情况,推测他的实现代码。这里只是简单实现.算法原理呢,就是我们创建一个全透明的窗口,这样这个窗口就能全透顶层窗口下面的非激活窗口.然后在这个窗口上使用我们自定义的布局类,布局对象也行,布局成I900的样子,最后在我们的activity中移动这个布局对象.当然,有关滑动解锁还关系到大量的其他方面的技术,比如使用服务侦听锁屏广播,使用我们的窗口替换系统的锁屏窗口,屏掉按键,获取权限等一系列技术,这里不做详细解释,代码全都有.

MyLockActivity代码如下:

[java] view plaincopyprint?
  1. <span xmlns="http://www.w3.org/1999/xhtml" style="">package com.mylock;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.util.DisplayMetrics;  
  7. import android.view.KeyEvent;  
  8. import android.view.LayoutInflater;  
  9. import android.view.MotionEvent;  
  10. import android.view.Window;  
  11. import android.view.WindowManager;  
  12. import android.widget.FrameLayout;  
  13.   
  14. import java.lang.reflect.Method;  
  15.   
  16. public class MyLockActivity extends Activity {  
  17.     FrameLayout RLayout;  
  18.   
  19.     float o_X, o_Y, n_X, n_Y;  
  20.   
  21.     /** Called when the activity is first created. */  
  22.     @Override  
  23.     public void onCreate(Bundle savedInstanceState) {  
  24.         super.onCreate(savedInstanceState);  
  25.         Object service = getSystemService("statusbar");  
  26.         try {  
  27.             Class<?> statusBarManager = Class.forName("android.app.StatusBarManager");  
  28.             Method expand = statusBarManager.getMethod("disable"int.class);  
  29.             expand.invoke(service, 0x00000000);  
  30.         } catch (Exception e) {  
  31.             e.printStackTrace();  
  32.         }  
  33.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  34.         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
  35.                 WindowManager.LayoutParams.FLAG_FULLSCREEN);  
  36.         getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);  
  37.         getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);  
  38.         getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);  
  39.   
  40.         LayoutInflater inflater = getLayoutInflater();  
  41.         RLayout = (FrameLayout) inflater.inflate(R.layout.main, null);  
  42.         setContentView(RLayout);  
  43.   
  44.         Intent mService = new Intent(MyLockActivity.this, MyLockServer.class);// ��������  
  45.         mService.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  46.         startService(mService);  
  47.         // getWindow().setContentView(LayoutInflater.from(this).inflate(R.layout.main,  
  48.         // null));  
  49.         // setContentView(R.layout.main);  
  50.     }  
  51.   
  52.     @Override  
  53.     public boolean onTouchEvent(MotionEvent event) {  
  54.         final int action = event.getAction();  
  55.         DisplayMetrics dm = new DisplayMetrics();  
  56.         getWindowManager().getDefaultDisplay().getMetrics(dm);  
  57.         int w = dm.widthPixels;  
  58.         int h = dm.heightPixels;  
  59.         switch (action) {  
  60.             case MotionEvent.ACTION_DOWN:  
  61.                 o_X = event.getX();  
  62.                 o_Y = event.getY();  
  63.                 break;  
  64.             case MotionEvent.ACTION_MOVE:  
  65.                 n_X = event.getX() - o_X;  
  66.                 n_Y = event.getY() - o_Y;  
  67.                 RLayout.layout((int) n_X, (int) n_Y, (int) n_X + w, (int) n_Y + h);  
  68.                 RLayout.invalidate();  
  69.                 if ((Math.abs(n_Y) > (h >> 1)) && (n_Y < 0)) {  
  70.                     this.finish();  
  71.                 }  
  72.                 break;  
  73.             case MotionEvent.ACTION_UP:  
  74.                 n_X = 0;  
  75.                 n_Y = 0;  
  76.                 RLayout.layout((int0, (int0, (int0 + 480, (int0 + 800);  
  77.                 RLayout.invalidate();  
  78.                 break;  
  79.             default:  
  80.                 break;  
  81.         }  
  82.         return super.onTouchEvent(event);  
  83.     }  
  84.   
  85.     @Override  
  86.     public void onAttachedToWindow() {  
  87.         WindowManager.LayoutParams lp = getWindow().getAttributes();  
  88.         lp.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT; // 2010,2003,2004  
  89.         getWindow().setType(lp.type);  
  90.   
  91.         super.onAttachedToWindow();  
  92.     }  
  93.   
  94.     @Override  
  95.     public boolean dispatchKeyEvent(KeyEvent event) {  
  96.         // 返回true,不响应其他key  
  97.         return true;  
  98.     }  
  99.   
  100. }  
  101. </span>  

定义一个服务侦听系统广播,MyLockServer代码如下:

[java] view plaincopyprint?
  1. <span xmlns="http://www.w3.org/1999/xhtml" style="">package com.mylock;  
  2.   
  3. import android.app.KeyguardManager;  
  4. import android.app.Service;  
  5. import android.app.KeyguardManager.KeyguardLock;  
  6. import android.content.BroadcastReceiver;  
  7. import android.content.Context;  
  8. import android.content.Intent;  
  9. import android.content.IntentFilter;  
  10. import android.os.IBinder;  
  11. import android.util.Log;  
  12.   
  13. public class MyLockServer extends Service {  
  14.     KeyguardManager mKeyguardManager = null;  
  15.   
  16.     KeyguardLock mKeyguardLock = null;  
  17.   
  18.     MyLockBroadcastReceiver mMasterResetReciever;  
  19.   
  20.     Intent startIntent = null;  
  21.   
  22.     @Override  
  23.     public IBinder onBind(Intent arg0) {  
  24.         // TODO Auto-generated method stub  
  25.         return null;  
  26.     }  
  27.   
  28.     @Override  
  29.     public void onCreate() {  
  30.         // TODO Auto-generated method stub  
  31.         super.onCreate();  
  32.     }  
  33.   
  34.     @Override  
  35.     public int onStartCommand(Intent intent, int flags, int startId) {  
  36.         onStart(intent, startId);  
  37.         return super.onStartCommand(intent, flags, startId);  
  38.     }  
  39.   
  40.     @Override  
  41.     public void onStart(Intent intent, int startId) {  
  42.         // TODO Auto-generated method stub  
  43.         startIntent = intent;  
  44.         IntentFilter filter = new IntentFilter();  
  45.         filter.addAction(Intent.ACTION_SCREEN_ON);  
  46.         filter.addAction(Intent.ACTION_SCREEN_OFF);  
  47.         mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);  
  48.         mKeyguardLock = mKeyguardManager.newKeyguardLock("");  
  49.         mKeyguardLock.disableKeyguard();  
  50.         mMasterResetReciever = new MyLockBroadcastReceiver();  
  51.         registerReceiver(mMasterResetReciever, filter);  
  52.     }  
  53.   
  54.     @Override  
  55.     public void onDestroy() {  
  56.         // TODO Auto-generated method stub  
  57.         unregisterReceiver(mMasterResetReciever);  
  58.         if (startIntent != null) {  
  59.             startService(startIntent);  
  60.         }  
  61.     }  
  62.   
  63.     public class MyLockBroadcastReceiver extends BroadcastReceiver {  
  64.         @Override  
  65.         public void onReceive(Context context, Intent intent) {  
  66.             // TODO Auto-generated method stub  
  67.             try {  
  68.                 Intent i = new Intent();  
  69.                 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  70.                 i.setClass(context, MyLockActivity.class);  
  71.                 context.startActivity(i);  
  72.             } catch (Exception e) {  
  73.                 Log.i("Output:", e.toString());  
  74.             }  
  75.         }  
  76.     }  
  77.   
  78. }  
  79. </span>  
对应的XML文件AndroidManifest.xml内容如下:

[java] view plaincopyprint?
  1. <span xmlns="http://www.w3.org/1999/xhtml" style=""><span xmlns="http://www.w3.org/1999/xhtml" style=""><?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.mylock"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk android:minSdkVersion="10" />  
  8.   
  9.     <application  
  10.         android:icon="@drawable/ic_launcher"  
  11.         android:label="@string/app_name"  
  12.         android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" >  
  13.         <activity  
  14.             android:name=".MyLockActivity"  
  15.             android:label="@string/app_name"  
  16.             android:screenOrientation="portrait" >  
  17.             <intent-filter>  
  18.                 <action android:name="android.intent.action.MAIN" />  
  19.   
  20.                 <category android:name="android.intent.category.LAUNCHER" />  
  21.             </intent-filter>  
  22.         </activity>  
  23.   
  24.         <service android:name=".MyLockServer" />  
  25.     </application>  
  26.   
  27.     <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />  
  28.     <uses-permission android:name="android.permission.status_bar" />  
  29.     <uses-permission android:name="android.permission.disable_status_bar" />  
  30.   
  31. </manifest></span></span>  

漏了个XML文件,main.xml内容如下:

[java] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.     <ImageView  
  7.         android:id="@+id/imageView1"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:src="@drawable/qq" />  
  11.   
  12.     <TextView  
  13.         android:id="@+id/textView1"  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content"  
  16.         android:textSize="20dp"  
  17.         android:textColor = "#ff000000"  
  18.         android:text="@string/hello" />  
  19.   
  20. </FrameLayout>  

效果如下:



转载请注明出处:http://blog.csdn.net/blogercn/article/details/7489205#comments

原创粉丝点击