0

I need to open a new lock screen while opening the other app... I have write service ... but which is not working out side of my app..

public int onStartCommand(Intent intent, int flags, int startId) {
         Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
    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!=null)
    {
        Toast.makeText(this,"activityOnTop"+""+activityOnTop,Toast.LENGTH_LONG).show();
        {
          Intent lockIntent = new Intent(this, LockScreen.class);
            lockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
           this.startActivity(lockIntent);


        }
    }

    return START_STICKY;
 }
Konrad Krakowiak
  • 12,285
  • 11
  • 58
  • 45

1 Answers1

0

Your service seems fine, maybe you want to look at mine which I wrote a few months ago.

Since Andriod L (Android 5.0), getRunningTasks() doesn't deliever the current activity anymore (due to security reasons). If you want to read more on this, take a look at this stackoverflow post: Alternative to getRunningTasks in Android L

However, it seems that there is a little workaround: In Android 5.0, you can still get all running tasks and then filter after their process importance. If the importance equals IMPORTANCE_FOREGROUND, it is likely that this app is currently in the foreground. An example can be found in this stackoverflow answer.

Community
  • 1
  • 1
Manuel Allenspach
  • 12,467
  • 14
  • 54
  • 76