googleAdsense_widever


안드로이드 잠금화면 앱 소스 Android / XCode
















Hi Everyone Here I am sharing some knowledge to make a “customize lock screen”. So we have to done some tasks-


  1. Disable some hard keys like Home ButtonBack Button & Override Power Button.
  2. Handling Screen OFF & Screen ON Intents to Override Power Button. So we have a receiver called lockScreenReceiverwith registering Intent.ACTION_SCREEN_OFF and Intent.ACTION_SCREEN_ON in an IntentFilter in your JAVA code. Important thing is that you CANNOT declare them in your Android Manifest! I’m not sure exactly why, but they must be registered your JAVA code. 
  3. Handling call states to handle the lock screen e.g. In which call state (ringing, off hook, idle) we should finish our customize lock screen activity. 


Now we have Receiver lockScreenReceiver to handle Screen OFF & Screen ON Intents -

package receiver;

import android.app.KeyguardManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import android.widget.Toast;

import com.lockscreen.LockScreenAppActivity;

public class lockScreenReeiver extends BroadcastReceiver  {
      public static boolean wasScreenOn = true;

      @Override
      public void onReceive(Context context, Intent intent) {

            if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
           
                  wasScreenOn=false;
                  Intent intent11 = new Intent(context,LockScreenAppActivity.class);
                  intent11.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                  context.startActivity(intent11);

            } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {

                  wasScreenOn=true;
                  Intent intent11 = new Intent(context,LockScreenAppActivity.class);
                  intent11.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
           
            }
            else if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
            {
     
                  Intent intent11 = new Intent(context, LockScreenAppActivity.class);
                  intent11.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                  context.startActivity(intent11);

                 
            }

      }

}

Now we have a service MyService for implementing lockScreenReceiver .

package com.lockscreen;

import receiver.lockScreenReeiver;
import android.app.KeyguardManager;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.WindowManager;
import android.widget.Toast;

public class MyService extends Service{
      BroadcastReceiver mReceiver;
      // Intent myIntent;
      @Override
      public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
      }

      @Override
      public void onCreate() {
            KeyguardManager.KeyguardLock k1;

                   //getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);

            KeyguardManager =(KeyguardManager)getSystemService(KEYGUARD_SERVICE);
            k1= km.newKeyguardLock("IN");
            k1.disableKeyguard();

            IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
            filter.addAction(Intent.ACTION_SCREEN_OFF);
            mReceiver = new lockScreenReeiver();
            registerReceiver(mReceiver, filter);
            super.onCreate();
      }
      @Override
      public void onStart(Intent intent, int startId) {
            // TODO Auto-generated method stub
            super.onStart(intent, startId);
}

      @Override
      public void onDestroy() {
            unregisterReceiver(mReceiver);
            super.onDestroy();
      }}

 
 Now we have an Activity  LockScreenAppActivity for rendering customize lock screen and disable some hardkeys like Home Button, Back Button & Override Power Button.

Power Button already overrides by handling Screen OFF & Screen ON Intents.  Some other keys override using this code-

@Override
            public void onBackPressed() {
                  // Don't allow back to dismiss.
                  return;
            }
   
            @Override
            public boolean onKeyDown(int keyCode, android.view.KeyEvent event) {

                  if((keyCode == KeyEvent.KEYCODE_HOME)){
     
                        return true;
                  }
                  return false;
            }



Important: 1-  Now we can’t disable Home Button by handling Key Event . So we should also use some extra code  using onAttachedWindow() method.

public void onAttachedToWindow() {
            // TODO Auto-generated method stub
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG|WindowManager.LayoutParams.FLAG_FULLSCREEN);

            super.onAttachedToWindow();
      }

2- We can’t disable Notification Bar & Home Button both in one time. If we trying to disable Notification Bar using theme-

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

<activity android:label="@string/app_name"
          android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
          android:screenOrientation="portrait"
            android:name=".LockScreenAppActivity"
           >

Note: Then it never works if you already disable Home Button .Because this must be considered a criminal offense in the android environment! Any app that enforces such constraints could be considered to be malware.
Now we also have to handle call states for handling the lock screen like in which call state (ringing, off hook, idle)  we should finish our customize lock screen activity. 

@Override
    public void onCreate() {
        super.onCreate();
            StateListener phoneStateListener = new StateListener();
            TelephonyManager telephonymanager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
            telephonymanager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
        }catch(Exception e){ }

    }
class StateListener extends PhoneStateListener{
            @Override
public void onCallStateChanged(int state, String incomingNumber)     {

                  super.onCallStateChanged(state, incomingNumber);
                  switch(state){
                  case TelephonyManager.CALL_STATE_RINGING:
                        break;
                  case TelephonyManager.CALL_STATE_OFFHOOK:
                        System.out.println("call Activity off hook");

            // Finish lock screen activity
                        finish();
                        break;
                  case TelephonyManager.CALL_STATE_IDLE:
                        break;
                  }
            }
      };


Important: While stopping service don't foget to remove the listener and add these permissions to your manifest file:
 <uses-permission android:name="android.permission.READ_PHONE_STATE" />

We have to make customize lock screen like this. In this screen we can unloack screen by dragging droid image upon home image.





So for moving droid image, we have to implement TouchListener  by using following code –

droid.setOnTouchListener(new View.OnTouchListener() {
                        @Override
public boolean onTouch(View v, MotionEvent event) {
                              // TODO Auto-generated method stub
     layoutParams = (LayoutParams) v.getLayoutParams();

 switch(event.getAction())
   {
     case MotionEvent.ACTION_DOWN:
     int[] hompos=new int[2];
     droidpos=new int[2];
     home.getLocationOnScreen(hompos);
     home_x=hompos[0];
     home_y=hompos[1];
                                   
     break;
     
     case MotionEvent.ACTION_MOVE:
     int x_cord = (int)event.getRawX();
     int y_cord = (int)event.getRawY();
   


if(x_cord>windowwidth-(windowwidth/24)){x_cord=windowwidth-(windowwidth/24)*2;}
                                   
if(y_cord>windowheight-(windowheight/32)){y_cord=windowheight-(windowheight/32)*2;}

layoutParams.leftMargin = x_cord ;
layoutParams.topMargin = y_cord;

droid.getLocationOnScreen(droidpos);
v.setLayoutParams(layoutParams);

if(((x_cord-home_x)<=(windowwidth/24)*5 && (home_x-x_cord)<=(windowwidth/24)*4)&&((home_y-y_cord)<=(windowheight/32)*5))
{
                                         
v.setVisibility(View.GONE);
finish();
}
else{
                                         
}

break;
case MotionEvent.ACTION_UP:
int x_cord1 = (int)event.getRawX();
int y_cord2 = (int)event.getRawY();

if(((x_cord1-home_x)<=(windowwidth/24)*5 && (home_x-x_cord1)<=(windowwidth/24)*4)&&((home_y-y_cord2)<=(windowheight/32)*5))
{
}
else{

layoutParams.leftMargin = (windowwidth/24)*10;
layoutParams.topMargin = (windowheight/32)*8;
v.setLayoutParams(layoutParams);
                                    }

                              }
return true;
                        }
                  });





We have a manifest file to define some service, activity, and receiver & to add some permission. Required permissions are given below –

    <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

Note - Every One can find Source Code on this location                                  https://www.dropbox.com/s/qzc3rawe5z5onbi/LockScreenApp.rar

덧글

댓글 입력 영역


공지

어서오십시오.
트위터 : @FCliver
기저심리학 : 네이버카페
카카오톡 : FCliver
페이스북 : Fredric Cliver

통계 위젯 (화이트)

123
50
288485

접속자 위치