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?