TheJadav

Lock own app functionality for android

The simple demo to create lock screen activity for your android app:

  1. Create one activity which ask for password or pin. It is may be like
  2. Create MyApplication class which  extends Application class.
  3. Set application name in Manifest.xml
    android:name=”.MyApplication” ………>

Now you need two static variable in MyApplication file.

public static boolean locked;
private static long time = 0;

Now define two methods to track app state

public static void activityPaused() {
    time = Calendar.getInstance().getTimeInMillis();}

public static void activityResumed() {
    if (Calendar.getInstance().getTimeInMillis() - time > 60000) {
        locked = true;    } else {
        locked = false;    }
}

And finally override OnResume and OnPause of every activity

@Overridepublic void onResume() {
    super.onResume(); 
    CustomApplication.activityResumed(); 
    if (CustomApplication.locked) {
        startActivity(new Intent(this, LockScreenActivity.class));     
    }
}

@Overrideprotected void onPause() {
    super.onPause(); 
    if(!CustomApplication.locked) 
        CustomApplication.activityPaused(); 
}

Now run app and enjoy security of your app.

Share this content:

Share:

More Posts

Introduction to Kotlin: A Versatile and Modern Programming Language

Kotlin, a versatile and modern programming language, offers developers a concise, safe, and interoperable coding experience. With features like null safety, extension functions, and coroutines, Kotlin enhances productivity and readability, making it an attractive choice for Android development and beyond. Whether you’re new to programming or an experienced developer, exploring Kotlin opens up a world of possibilities for building robust and expressive applications.

Mastering the Android Activity Lifecycle: A Comprehensive Guide

The Android Activity Lifecycle is a fundamental concept for Android developers. It defines how an Activity behaves during its lifecycle, from creation to destruction. Understanding the Activity Lifecycle is crucial for managing resources efficiently and delivering a smooth user experience. In this blog post, we’ll briefly introduce the different states an Activity can be in and the main callback methods associated with each state. Let’s dive in and explore this important aspect of Android app development!

Table of Contents

Send Us A Message