三星I900滑动解锁看起来就像一层窗户纸,拉起纸来就能看到当前正在运行的activity程序,这是很有意思的。但其实现原理不可知,我们只能观测他的行为,根据掌握的可用API情况,推测他的实现代码。这里只是简单实现.算法原理呢,就是我们创建一个全透明的窗口,这样这个窗口就能全透顶层窗口下面的非激活窗口.然后在这个窗口上使用我们自定义的布局类,布局对象也行,布局成I900的样子,最后在我们的activity中移动这个布局对象.当然,有关滑动解锁还关系到大量的其他方面的技术,比如使用服务侦听锁屏广播,使用我们的窗口替换系统的锁屏窗口,屏掉按键,获取权限等一系列技术,这里不做详细解释,代码全都有.
MyLockActivity代码如下:
- <span xmlns="http://www.w3.org/1999/xhtml" style="">package com.mylock;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.util.DisplayMetrics;
- import android.view.KeyEvent;
- import android.view.LayoutInflater;
- import android.view.MotionEvent;
- import android.view.Window;
- import android.view.WindowManager;
- import android.widget.FrameLayout;
- import java.lang.reflect.Method;
- public class MyLockActivity extends Activity {
- FrameLayout RLayout;
- float o_X, o_Y, n_X, n_Y;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- Object service = getSystemService("statusbar");
- try {
- Class<?> statusBarManager = Class.forName("android.app.StatusBarManager");
- Method expand = statusBarManager.getMethod("disable", int.class);
- expand.invoke(service, 0x00000000);
- } catch (Exception e) {
- e.printStackTrace();
- }
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
- WindowManager.LayoutParams.FLAG_FULLSCREEN);
- getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
- getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
- getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
- LayoutInflater inflater = getLayoutInflater();
- RLayout = (FrameLayout) inflater.inflate(R.layout.main, null);
- setContentView(RLayout);
- Intent mService = new Intent(MyLockActivity.this, MyLockServer.class);// ��������
- mService.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- startService(mService);
- // getWindow().setContentView(LayoutInflater.from(this).inflate(R.layout.main,
- // null));
- // setContentView(R.layout.main);
- }
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- final int action = event.getAction();
- DisplayMetrics dm = new DisplayMetrics();
- getWindowManager().getDefaultDisplay().getMetrics(dm);
- int w = dm.widthPixels;
- int h = dm.heightPixels;
- switch (action) {
- case MotionEvent.ACTION_DOWN:
- o_X = event.getX();
- o_Y = event.getY();
- break;
- case MotionEvent.ACTION_MOVE:
- n_X = event.getX() - o_X;
- n_Y = event.getY() - o_Y;
- RLayout.layout((int) n_X, (int) n_Y, (int) n_X + w, (int) n_Y + h);
- RLayout.invalidate();
- if ((Math.abs(n_Y) > (h >> 1)) && (n_Y < 0)) {
- this.finish();
- }
- break;
- case MotionEvent.ACTION_UP:
- n_X = 0;
- n_Y = 0;
- RLayout.layout((int) 0, (int) 0, (int) 0 + 480, (int) 0 + 800);
- RLayout.invalidate();
- break;
- default:
- break;
- }
- return super.onTouchEvent(event);
- }
- @Override
- public void onAttachedToWindow() {
- WindowManager.LayoutParams lp = getWindow().getAttributes();
- lp.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT; // 2010,2003,2004
- getWindow().setType(lp.type);
- super.onAttachedToWindow();
- }
- @Override
- public boolean dispatchKeyEvent(KeyEvent event) {
- // 返回true,不响应其他key
- return true;
- }
- }
- </span>
定义一个服务侦听系统广播,MyLockServer代码如下:
- <span xmlns="http://www.w3.org/1999/xhtml" style="">package com.mylock;
- 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 MyLockServer extends Service {
- KeyguardManager mKeyguardManager = null;
- KeyguardLock mKeyguardLock = null;
- MyLockBroadcastReceiver mMasterResetReciever;
- Intent startIntent = 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 int onStartCommand(Intent intent, int flags, int startId) {
- onStart(intent, startId);
- return super.onStartCommand(intent, flags, startId);
- }
- @Override
- public void onStart(Intent intent, int startId) {
- // TODO Auto-generated method stub
- startIntent = intent;
- IntentFilter filter = new IntentFilter();
- filter.addAction(Intent.ACTION_SCREEN_ON);
- filter.addAction(Intent.ACTION_SCREEN_OFF);
- mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
- mKeyguardLock = mKeyguardManager.newKeyguardLock("");
- mKeyguardLock.disableKeyguard();
- mMasterResetReciever = new MyLockBroadcastReceiver();
- registerReceiver(mMasterResetReciever, filter);
- }
- @Override
- public void onDestroy() {
- // TODO Auto-generated method stub
- unregisterReceiver(mMasterResetReciever);
- if (startIntent != null) {
- startService(startIntent);
- }
- }
- public class MyLockBroadcastReceiver extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- // TODO Auto-generated method stub
- try {
- Intent i = new Intent();
- i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- i.setClass(context, MyLockActivity.class);
- context.startActivity(i);
- } catch (Exception e) {
- Log.i("Output:", e.toString());
- }
- }
- }
- }
- </span>
- <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"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.mylock"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk android:minSdkVersion="10" />
- <application
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" >
- <activity
- android:name=".MyLockActivity"
- android:label="@string/app_name"
- android:screenOrientation="portrait" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <service android:name=".MyLockServer" />
- </application>
- <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
- <uses-permission android:name="android.permission.status_bar" />
- <uses-permission android:name="android.permission.disable_status_bar" />
- </manifest></span></span>
漏了个XML文件,main.xml内容如下:
- <?xml version="1.0" encoding="utf-8"?>
- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <ImageView
- android:id="@+id/imageView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/qq" />
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="20dp"
- android:textColor = "#ff000000"
- android:text="@string/hello" />
- </FrameLayout>
效果如下:
转载请注明出处:http://blog.csdn.net/blogercn/article/details/7489205#comments