Friday, 6 February 2015

GESTURE UNLOCK

XML:

    In this Xml file I have used "android.gesture.GestureOverlayView "(layout) function for gesture detection we can either use it otherwise "Relative Layout"....

SAMPLE CODE:

 <?xml version="1.0" encoding="utf-8"?>
 <android.gesture.GestureOverlayView      xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gestures"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#80000000"
    android:eventsInterceptionEnabled="true"
    android:gestureStrokeType="single"

    android:orientation="vertical" >

</android.gesture.GestureOverlayView>

JAVA:
MAIN ACTIVITY.java:

import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;

import android.gesture.Prediction;

public class MainActivity extends Activity implements OnGesturePerformedListener {

 

GestureLibrary gLibrary;

GestureOverlayView mView;
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
        
        makeFullScreen();
        startService(new Intent(this,LockScreenService.class));

        
        gLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
        if(gLibrary != null)
        {
        if(!gLibrary.load())
        {
            Log.e("GestureSample", "Gesture library was not loaded…");
            finish();
        }
        else
        {
                mView = (GestureOverlayView) findViewById(R.id.gestures);
                mView.addOnGesturePerformedListener(this);      
        }
        }

    }

@SuppressWarnings("unused")
@Override
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
// TODO Auto-generated method stub
ArrayList<Prediction> predictions = gLibrary.recognize(gesture);
 

        if (predictions.size() > 0) {
Prediction prediction = predictions.get(0);

if (prediction.score > 5.0) {

Toast.makeText(this, prediction.name,Toast.LENGTH_SHORT).show();
                             // unclock method: here u can use your own methods to unlock
                                android.os.Process.killProcess("mention process id");

}

LockActivity:


public class LockScreenService extends Service {

BroadcastReceiver receiver;

   @Override
   public IBinder onBind(Intent intent) {
       return null;
   }

   @Override
   @SuppressWarnings("deprecation")
   public void onCreate() {
       KeyguardManager.KeyguardLock key;
       KeyguardManager km = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);

       //This is deprecated, but it is a simple way to disable the lockscreen in code
       key = km.newKeyguardLock("IN");

       key.disableKeyguard();

       //Start listening for the Screen On, Screen Off, and Boot completed actions
       IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
       filter.addAction(Intent.ACTION_SCREEN_OFF);
       filter.addAction(Intent.ACTION_BOOT_COMPLETED);

       //Set up a receiver to listen for the Intents in this Service
       receiver = new LockScreenReceiver();
       registerReceiver(receiver, filter);

       super.onCreate();
   }}}

RECEIVER:

public class LockScreenReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();

        //If the screen was just turned on or it just booted up, start your Lock Activity
        if(action.equals(Intent.ACTION_SCREEN_OFF) || action.equals(Intent.ACTION_BOOT_COMPLETED))
        {
            Intent i = new Intent(context, MainActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
        }

ANDROID MANIFEST:

<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" />
Final output will look like this.

you can add your own "gestures" by saving your gestures as mentioned in this tutorial:

Refer:
In this way u can enjoy your own gestures with unlock enjoy.....

post ur comments for further improvement......

Next Part will comprises with "Shake unlock"... (Contd...)

No comments:

Post a Comment

Previous Page Next Page Home
Blogger Widgets