1

I am trying to create a simple App Lock for Android App. So I created app lock service like this

package com.company.applock;

import android.app.ActivityManager;
import android.app.Service;
import android.content.Intent;
import android.content.Context;
import android.os.IBinder;
import android.util.Log;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import com.company.applock.LockscreenActivity;


public class LockService extends Service {
    private static boolean isLock = false;
    private Timer timer;


    // Static Getters and setters
    public static boolean isLock() {
        return isLock;
    }

    public static void setLock(boolean lock) {
        isLock = lock;
    }

    // Public static method to start service
    public static void start(Context context) {
        Intent intent = new Intent(context, LockService.class);
        context.startService(intent);
    }

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

    @Override
    public void onCreate() {
        timer = new Timer("LockService");
        timer.schedule(checkLockTask, 500L, 500L);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (null != intent) { }
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        timer.cancel(); timer = null;
    }

    // Custom methods
    private boolean isForegroundLocked() {
        ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);

        Log.d("topActivity", taskInfo.get(0).topActivity.getClassName());

        return taskInfo.get(0).topActivity.getClassName().equals("com.company.applock.LockscreenActivity");
    }

    private void showLockActivity() {
        Intent intent = new Intent(getApplicationContext(), LockscreenActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);

        Log.d("showLockActivity", "success");
    }

    private TimerTask checkLockTask = new TimerTask() {
        @Override
        public void run() {
            // TODO: Fix foreground bug
            if (isLock) {
                Log.d("isLock", "true");
                if (isForegroundLocked()) {
                    Log.d("isForegroundLocked", "true");
                } else {
                    Log.d("isForegroundLocked", "false");
                    showLockActivity();
                }
            } else {
                Log.d("isLock", "false");
                // TODO: Close lock screen
            }
        }
    };
}

As you see, this is very simple one. The problem is;

  1. When I press home button and get to launcher, it locks after a few seconds (generally 4 or 5 seconds)
  2. If I remove my main activity (this is not lockscreen activity) from recent tasks, the service stops (it doesn't stop when I press home button).

So, anyone who knows the solution for these problem, please help me. I am new to Android.

Thank you.

0 Answers0