锁屏

引用:http://www.eoeandroid.com/forum.php?mod=viewthread&tid=69344

http://www.bangchui.org/read.php?tid=12665&page=2

我解锁的思路:在服务里注册一个监听系统锁屏的广播,监听到锁屏就解锁然后startActivity自己制作的所界面,这时要锁住键盘,然后再屏幕上解锁:




[code=java][/code]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.net.zyc"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="10" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Main"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        
        <service  android:enabled="true"   android:name=".Mserver"/>
        <activity android:name="LockView"/>
    </application>
   - <!--  获得锁盘权限--> 
  <uses-permission android:name="android.permission.DISABLE_KEYGUARD"/> 
</manifest>

java:
__________________________________________________________________________________________
public class Main extends Activity implements OnClickListener{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button = (Button) findViewById(id.button_sure);
        button.setOnClickListener(this);
    }

    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        if(arg0.getId()==id.button_sure){
            startService();
            this.finish();
        }
    }
    public void startService(){
        Intent intent = new Intent(this,Mserver.class);
        this.startService(intent);
    }
    
}


_____________________________________________________________________________
public class Mserver extends Service {

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onStart(Intent intent, int startId) {
        // TODO Auto-generated method stub
        
        //注册监听系统锁屏信息
        registerReceiver(br, new IntentFilter(Intent.ACTION_SCREEN_OFF));
        
    }
    
    private BroadcastReceiver br = new BroadcastReceiver() {
        
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
                KeyguardManager.KeyguardLock kk;
                KeyguardManager km = (KeyguardManager) context.getSystemService(KEYGUARD_SERVICE);
                kk = km.newKeyguardLock("");
                kk.disableKeyguard();
                showLockView(context);
                
                
            }
            
        }
    };
    
    public void showLockView(Context context){
        Intent intent = new Intent(context,LockView.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }
    

}

_______________________________________________________________________________


public class LockView extends Activity {

    public int sreenw,sreenh;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        Display display =getWindowManager().getDefaultDisplay();
        sreenw =display.getWidth();
        sreenh=display.getHeight();
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //失去焦点,除Home键其他键失效;但是好像和锁Home键的方法冲突;所以没用。
        //this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
        //可以实现覆盖锁的界面,不过好像在模拟器上还是会先跳出系统锁然后会很快的出现自己锁的界面
        //this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED, WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); 
        KeyguardManager.KeyguardLock kk;
        KeyguardManager km = (KeyguardManager) this.getSystemService(KEYGUARD_SERVICE);
        kk = km.newKeyguardLock("");
        kk.reenableKeyguard();
        //绘制锁滑动效果
        this.setContentView(new MView(this));
    }
    
    //封锁Home键
    @Override
    public void onAttachedToWindow() {
        // TODO Auto-generated method stub
         this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
        super.onAttachedToWindow();
    }

   @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
    return false;
    }

//锁的界面
   class MView extends View implements OnTouchListener ,Runnable{

     Bitmap bit_backGround,bit_lock;
     Paint p;
     public int l_x1,l_y1,l_x2,l_y2,lw,lh;
    public MView(Context context) {
        super(context);
        initImage(context);
        l_x1=sreenw-bit_lock.getWidth()>>1;
        l_y1=200;
        l_y2=100;
        lw =bit_lock.getWidth();
        lh=bit_lock.getHeight();
        p = new Paint();
        p.setColor(Color.RED);
        this.setOnTouchListener(this);
        new Thread(this).start();
        
    }
    public void initImage(Context context){
        InputStream  is= context.getResources().openRawResource(R.drawable.lockback);
        bit_backGround=BitmapFactory.decodeStream(is);
        is=context.getResources().openRawResource(R.drawable.lock);
        bit_lock=BitmapFactory.decodeStream(is);
    }
    
    float yy,num;
    
    public boolean onTouch(View v, MotionEvent e) {
          if(num>=100)
              finish();
    if(e.getAction()==MotionEvent.ACTION_DOWN)
       if(e.getX()>l_x1&&e.getX()<l_x1+lw&&
               e.getY()>l_y1&&e.getY()<l_y1+lh){
           yy=e.getY();
          
       }
    if(e.getAction()==MotionEvent.ACTION_UP){
        if(e.getX()>l_x1&&e.getX()<l_x1+lw&&
                   e.getY()>l_y2&&e.getY()<l_y2+lh){
               if(num<100)
                   yy=0;
                   num=0;
                   l_y1=200;
           }
        }
    if(e.getAction()==MotionEvent.ACTION_MOVE){
        num=yy-e.getY();
    }
    this.postInvalidate();
        return true;
    }
    @Override
    protected void onDraw(Canvas c) {
        drawbackGround(c);
        drawLock(c);
        c.drawText(""+yy+"num"+num, 100, 100, p);
        //c.drawRect(l_x, top, right, bottom, paint)
    }
    public void drawbackGround(Canvas c)
    {
          Matrix matrix = new Matrix();
           int width = bit_backGround.getWidth();//获取资源位图的宽
            int height = bit_backGround.getHeight();//获取资源位图的高
               float w = (float)sreenw/bit_backGround.getWidth();
               float h =(float) sreenh/bit_backGround.getHeight();
               matrix.postScale(w, h);//获取缩放比例
             Bitmap dstbmp = Bitmap.createBitmap(bit_backGround,0,0,
              width,height,matrix,true);//根据缩放比例获取新的位图
             c.drawBitmap(dstbmp,0,0, null); //在屏幕上画出位图
             c.restore();
    }
    
    public void drawLock(Canvas c){
        c.drawBitmap(bit_lock, l_x1,l_y1-num, null);
    }
    public void run() {
        // TODO Auto-generated method stub
        while(true){
            this.postInvalidate();
//            try {
//                Thread.sleep(200);
//            } catch (InterruptedException e) {
//                // TODO Auto-generated catch block
//                e.printStackTrace();
//            }
        }
        
    }
       
   }

原文地址:https://www.cnblogs.com/sode/p/2732988.html