2

I have task to start activity from service and I'm using the following code:

    KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE); 
    final KeyguardManager.KeyguardLock kl = km .newKeyguardLock("MyKeyguardLock"); 
    kl.disableKeyguard(); 

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); 
    WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
                                     | PowerManager.ACQUIRE_CAUSES_WAKEUP
                                     | PowerManager.ON_AFTER_RELEASE, "MyWakeLock");
    wakeLock.acquire();
    Intent i = new Intent(this, someActivity.class);
    i.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED + WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD + WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON + WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    i.addCategory(Intent.CATEGORY_LAUNCHER);
    i.putExtra("someName", theName);
    startActivity(i);

This works fine when application is set to background. But when the application is closed the service only unlocks the phone but doesn't start my activity. Any of you have idea how ot achieve needed functionality. Thanks in advance!

EDIT> The problem is somewhere in unlocking phase. On first event occurence the phone get unlocked but doesn't start the activity. On the second occurence if the phone is unlocked the activity starts.

Antek
  • 721
  • 1
  • 4
  • 27
Boris Pawlowski
  • 1,781
  • 4
  • 16
  • 22

1 Answers1

4

Use this code It will help you.

Intent i = new Intent(context,MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   
context.startActivity(i); 

//More Clarification Firstly check your app is running or not. If your Application is not running then call this code.

 Intent i = new Intent(context,MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   
context.startActivity(i); 

If running then use this code

Intent i = new Intent(context,MainActivity.class);    
context.startActivity(i); 
samsad
  • 1,241
  • 1
  • 10
  • 15
  • The problem is somewhere in unlocking phase. On first event occurance the phone get unlocked but dosnt start the activity. On the second occurance if the phone is unlocked the activity starts. – Boris Pawlowski Oct 27 '14 at 13:58
  • Ok..you can use flag for the locking/unlocking Scenario and save it into shared prefrence.In service class you have to check flag then use the my above concept.. – samsad Oct 27 '14 at 14:02