0

So right now I am trying to develop an Android App for my young children. I want to set a pin or passwords on selected applications for a particular amount of time to prevent them from opening the app. For example, let's say that my daughter wants to play angry birds for some time on my phone while I am doing work. I will select my important apps like messaging, gmail, etc and put a pin or password on it for 30 minutes while she plays angry birds. After 30 minutes, I get my phone from my daughter and I can open the app without a pin because the time limit expired.

I have done a ton of research and I was able to get a basic service class written.

package com.spicycurryman.getdisciplined10.app;

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

import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * Created by Spicycurryman on 8/21/14.
 */
public class SaveMyAppsService extends Service{

    public void onCreate(){
        super.onCreate();
        //UNABLE TO SETCONTENTVIEW HERE. METHOD DOESN'T WORK
        while(true) {
            Toast.makeText(SaveMyAppsService.this,
                    "Your Message", Toast.LENGTH_LONG).show();
        }

    }

    String CURRENT_PACKAGE_NAME = "com.spicycurryman.getdisciplined10.app.dev";
    String lastAppPN = "";
    boolean noDelay = false;
    public static SaveMyAppsService instance;

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub

        scheduleMethod();
        CURRENT_PACKAGE_NAME = getApplicationContext().getPackageName();
        Log.e("Current PN", "" + CURRENT_PACKAGE_NAME);

        instance = this;

        return START_STICKY;
    }

    private void scheduleMethod() {
        // TODO Auto-generated method stub

        ScheduledExecutorService scheduler = Executors
                .newSingleThreadScheduledExecutor();
        scheduler.scheduleAtFixedRate(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub

                // This method will check for the Running apps after every 100ms
                if(29==30 ) //check if the time is spent
                {
                    stop();
                }
                else{
                    checkRunningApps();
                }
            }
        }, 0, 100, TimeUnit.MILLISECONDS);
    }

    public void checkRunningApps() {
        ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager
                .getRunningTasks(1);
        ActivityManager.RunningTaskInfo ar = RunningTask.get(0);
        String activityOnTop = ar.topActivity.getPackageName();

        Log.e("activity on TOp", "" + activityOnTop);


// Provide the packagename(s) of apps here, you want to show password activity
        if (activityOnTop.contains("com.android.camera")  // you can make this check even better
                || activityOnTop.contains(CURRENT_PACKAGE_NAME)) {
            while(true) {
                Toast.makeText(SaveMyAppsService.this,
                        "Your Message", Toast.LENGTH_LONG).show();
            }

        } else {
            // DO nothing
        }
    }

    public static void stop() {
        if (instance != null) {
            instance.stopSelf();
        }
    }
}

I use this code to start it.

startService(new Intent(this, SaveMyAppsService.class));

Essentially I just want to create a custom lock screen and show it to the user so they have to enter the correct pin or password to get into the app. But I am unable to do that here.

How would I go about setting the content view of a password or pin screen for the app of my choice (given I have the packagenames) so I can protect my apps with my service class?

Rohit Tigga
  • 2,373
  • 9
  • 43
  • 81

1 Answers1

1

Take a look at Activity.runOnUiThread

I think part of the problem might be that you are trying to use setContentView() in a service, but it needs an activity/UI to actually set the content for. So you might do something like

YourActivity.runOnUiThread(new Runnable() {
     @Override
     public void run() {
         // your setContentView code
     }
});

EDIT 1:

Intent localIntent = new Intent("android.intent.action.VIEW");
localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
localIntent.setClassName("com.example.applockerservice", "com.example.applockerservice.AppLockerScreen");
startActivity(localIntent);
Brent McFerrin
  • 528
  • 4
  • 9
  • I see, but why did you include CameraActivity? – Rohit Tigga Aug 23 '14 at 04:24
  • You had `activityOnTop.contains("com.android.camera")`, so I just used CameraActivity as an example. I made an edit to avoid confusion. – Brent McFerrin Aug 23 '14 at 04:29
  • not sure why but still unable to set it...? unable to be resolved. – Rohit Tigga Aug 23 '14 at 04:49
  • Could you update your code above with your setContentView method? – Brent McFerrin Aug 23 '14 at 04:51
  • http://stackoverflow.com/questions/18948251/not-able-to-call-runonuithread-in-a-thread-from-inside-of-a-service – Rohit Tigga Aug 23 '14 at 05:04
  • Yes, that's what I was saying -- you need an activity. So you might use the ActivityManager to see which activity is in the foreground, apply some logic (i.e. check if it is an app on your blacklist) and then have your LoginActivity set the view to your login screen to accept a pin. Essentially do your checkRunningApps() and inside this method you launch your login activity or set the content view (show your login screen). – Brent McFerrin Aug 23 '14 at 05:27
  • Thx. I see. I am starting to understand a little more and seeing where you are going with this. But I am having some trouble implementing this. How should I go about this in implementation? – Rohit Tigga Aug 23 '14 at 06:41
  • I am asking because I see where you are going with the runOnUiThread method etc. but I am unable to do the in my service class. I have to use a handler right? – Rohit Tigga Aug 23 '14 at 06:51
  • I would like to point you to [AppLock](https://play.google.com/store/apps/details?id=com.domobile.applock&hl=en) for now, until I have some time to write up some code. It may not help with your implementation, but it should show you the basic flow of control. Essentially, I see this as a kind of redirect -- your service checks to see if a specific app is running in the foreground, invoke your lock screen activity, and then your lock screen starts the desired app's activity. I'll try to code up an example soon because this might be useful code to have around (I, too, have children :) ). – Brent McFerrin Aug 23 '14 at 14:24
  • I appreciate your help, Brent. But yeah I am familiar with AppLock that is similar to what I am aiming to achieve. I guess I have to look at more examples of this or even try to get in contact with those devs. You too,that is nice. :) Originally this was a project only to protect my phone for a specific time from my daughter while she plays on it, but I see a great need for it for other people too. :) Let me know what happens. Thanks! – Rohit Tigga Aug 23 '14 at 17:11
  • 1
    I had a bit of time to hack together some simple code real quick. It's a WIP, but should get you going. You can find it [here](https://github.com/BMcFerrin/AndroidExamples/tree/master/AppLockerService). There's still much to do, but it shows how it might work for the camera app. The PIN activity doesn't work, you can just click "Test (skip)" button. Other than that, the main thing missing is a way to select/store the apps that you want to lock and then traverse them to unlock when you put in a password. Hope this helps! – Brent McFerrin Aug 25 '14 at 02:37
  • This is definitely a step in the right direction. It'll take a while but hopefully I will be able to make it into what I want! Thanks for the help and taking the time to produce this, Brent! – Rohit Tigga Aug 25 '14 at 17:30
  • Hey, just trying to run it after I imported it on Android Studio. It's able to run successfully for you, right? Just asking because of some persistent errors. – Rohit Tigga Aug 25 '14 at 18:44
  • I've never used Android Studio, but yes, it does work for me on my Galaxy S5. Although, you may have to build the support library (appcompat) -- look [here](https://developer.android.com/tools/support-library/setup.html). They have examples under "Adding libraries with resources" for both Eclipse ADT and Android Studio. Essentially it's just a library that is referenced by the project. I think it's something new in Android 4.4 that you have to build it yourself (used to be included?). Do you mind sharing which version of Android you are running, btw? – Brent McFerrin Aug 26 '14 at 00:56
  • I see. I knew it was something I was doing wrong on my way. I apologize! Luckily I may have found a way to kind of do it. http://pastebin.com/emrj1WbW The problem,however, is that I lock my own app in the process and I am trying to figure out how to successfully use the startforeground method to keep in running alive in the background. Whenever, I use it the notification bar disappears and lags my whole phone. – Rohit Tigga Aug 26 '14 at 03:07
  • I'm not sure how you are locking your own app, lol. But for your other problem of keeping the service alive, one suggestion might be to use this way of starting an activity with an intent (refer to post) – Brent McFerrin Aug 26 '14 at 03:26
  • Oh okay I see thx. Yes, I started in with that in my MainActivity onCreate, but I guess I am trying to keep it running even if the task manager kills it or RAM clears – Rohit Tigga Aug 26 '14 at 03:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/59971/discussion-between-here-is-the-helpful-link-and-brent-mcferrin). – Rohit Tigga Aug 26 '14 at 03:33