1

Im trying to make an app that Locks all applications with a password because "Kiosk Mode" cant be done as you can see in my question here ( disabling and hiding android navigation bar/notification menu permanently )

so what i did is i make a background service that checks if an application have been opened and i start a new activity that requests a password (i didn't implement the password yet, it can be done without checking a password and this is not my issue) .

The problem is my service isn't running and my LockScreen activity is not showing when i open an application.

i used this to write my background service (Blocking android apps programmatically @Amit Gupta answer) and (Using Service to run background and create notification @Muhammad Zeeshan Karamat Answer)

i found this answer but it didnt help me (How to always run a service in the background?)

Here is some of my code:

the main activity that start the service:

 public class LockAppsActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = (Button) findViewById(R.id.button1);
             button.setOnClickListener(new View.OnClickListener() {
                 public void onClick(View v) {

                     startService(new Intent(LockAppsActivity.this, MyService.class));

                 }
             });

    }...etc

my service class:

public class MyService extends Service{
@Override
public IBinder onBind(Intent arg0) {
    return null;
}


@Override
public void onCreate() {
    Toast.makeText(this, "Congrats! MyService Created", Toast.LENGTH_LONG).show();

}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    PackageManager packageManager = getPackageManager();
    Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    List<ResolveInfo> appList = packageManager.queryIntentActivities(mainIntent, 0);
    Collections.sort(appList, new ResolveInfo.DisplayNameComparator(packageManager));
    List<PackageInfo> packs = packageManager.getInstalledPackages(0);
    for(int i=0; i < packs.size(); i++) {
        PackageInfo p = packs.get(i);
        ApplicationInfo a = p.applicationInfo;
        // skip system apps if they shall not be included
        //apps.add(p.packageName);
    }

    ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager.getRunningTasks(1);
    ActivityManager.RunningTaskInfo ar = RunningTask.get(0);
    String activityOnTop = ar.topActivity.getClassName();


    if(!activityOnTop.equals("com.example.lock")){
        Intent lockIntent = new Intent(this, LockScreen.class);
        lockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        this.startActivity(lockIntent);
    }
    Toast.makeText(this, "My Service Running", Toast.LENGTH_LONG).show();
    return super.onStartCommand(intent, flags, startId);
}

and this is my LockScreen class which is not showing when i start any other application:

public class LockScreen extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);


    Button button = (Button) findViewById(R.id.okButton);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            LockScreen.this.onBackPressed();

        }
    });


    Button cancelButton = (Button) findViewById(R.id.cancelButton);
    cancelButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent startHomescreen=new Intent(Intent.ACTION_MAIN);
            startHomescreen.addCategory(Intent.CATEGORY_HOME);
            startHomescreen.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(startHomescreen);
        }
    });


}//onCreate

Edit: onStartCommand should work when i return START_STICKY; but it doesn't work too. Help would be appreciated,

Thanks

Community
  • 1
  • 1
Hamzeh
  • 167
  • 1
  • 4
  • 13

1 Answers1

0

First of all , if your service isn't even starting , then check in AndroidManifest.xml for this

<service android:name=".MyService" android:enabled="true" />

If it's starting then from what i understand , you are checking which apps are running when your service is started.

For now , just to test your code , implement an CountDownTimer , Which will check TOP Activity every 5 Second like

Protected CountDownTimer check = new CountDownTimer(5000, 5000)
        {

            @Override
            public void onTick(long millisUntilFinished)
            {
                //Do nothing here

            }

            @Override
            public void onFinish()
            {
                PackageManager packageManager = getPackageManager();
                Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
                mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                List<ResolveInfo> appList = packageManager.queryIntentActivities(mainIntent, 0);
                Collections.sort(appList, new ResolveInfo.DisplayNameComparator(packageManager));
                List<PackageInfo> packs = packageManager.getInstalledPackages(0);
                for(int i=0; i < packs.size(); i++) 
                {
                    PackageInfo p = packs.get(i);
                    ApplicationInfo a = p.applicationInfo;
                    // skip system apps if they shall not be included
                    //apps.add(p.packageName);
                }   

                ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
                List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager.getRunningTasks(1);
                ActivityManager.RunningTaskInfo ar = RunningTask.get(0);
                String activityOnTop = ar.topActivity.getClassName();


                if(!activityOnTop.equals("com.example.lock"))
                {
                    Intent lockIntent = new Intent(this, LockScreen.class);
                    lockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    this.startActivity(lockIntent);
                }
                this.cancel();
                this.start();
           }
};

Don't forget to start the countdowntimer from onCreate of service

check.start();
Sandip Fichadiya
  • 3,430
  • 2
  • 21
  • 47
  • my service was starting, i added that code but its now spamming LockScreen even when im at the homepage. – Hamzeh Aug 25 '14 at 08:48
  • Yeah , it will work every 5 second , u should cancel the countdowntimer once it starts the Lockscreen.Class , by using this.cancel().. i just told u initial logic for initiating the LockScreen.class when u open any other app (including Launcher ), modify that according to your need :) – Sandip Fichadiya Aug 25 '14 at 08:57
  • yes but if i cancel the countdowntimer when the lockscreen.class opens then i can open settings from the notification bar and skip the password checking.( i can remove the application from the settings). i think if i could know when another activity opens and then open the lockScreen is better than a timer – Hamzeh Aug 25 '14 at 09:42
  • not exactly, for example , if you want to lock facebook and whatsapp , then check everytime that if one of them is in top activity or not.for eg. if whatsapp is on top , then open lockscreen class & store that u locked whatsapp & continue timer , & make sure it shouldn't lock whatsapp again untill user re-opens it. There might be better way for this, check if there are any opensource app lockers.this is just my 2 cent :) best of luck – Sandip Fichadiya Aug 25 '14 at 10:32
  • @sam_0829 can u pls tell me how will i resolve issue ? – Erum Jan 17 '15 at 11:27