Start activity from background when screen is locked - android

I'm working on VOIP application and recently encountered a problem. Some devices are not waking up when screen is locked and app is in background. I've made some tests and found out that:
If app is closed with back button(task destroyed) then screen was locked and startActivity() is called - the screen is unlocked and activity works fine
If app wasn't started in a while(task wasn't created yet) then screen was locked and startActivity() is called - the screen is unlocked and activity works fine
If app was in foreground then screen was locked and startActivity() is called - the screen is unlocked and activity works fine
If user pressed the HOME button, thus putting the task in background and then screen was locked and startActivity() is called - IT DOESN'T WORK, the screen is off and activity's onCreate is not called
So, I guess it's somehow relevant with home button or the fact that application was in background when screen gets locked. The app calls startActivity() when firebase message received or JNI triggered some callbacks
Here is how I unlock the screen:
if (powerManager != null) {
wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP, "App:wakeuptag");
wakeLock.acquire(1000 * 60);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
setShowWhenLocked(true);
setTurnScreenOn(true);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
} else {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
}
And here is how I start the activity:
Intent intent = new Intent(context, VCEngine.appInfo().getActivity(ActivityType.CALL));
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NO_ANIMATION);
if (!(context instanceof Activity)) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mIsCallFromBackground.set(!AppUtils.isAppOnForeground());
}
intent.putExtra(
context.getString(R.string.extra_conference_join_or_create),
msg);
intent.putExtra(context.getString(R.string.extra_is_audio_call), isAudioCall);
intent.putExtra("isConference", isConference());
context.startActivity(intent);

Related

Unable to start call Activity while device is lock

I'm trying to open the activity on lock screen when I receive the notification form twilio. But I'm facing issue as it called onDestroy method when screen is locked on call receiving and it doesn't open the required activity.
I tried many solution but none of them worked.
I also tried following code to open activity on Lock screen but it doesn't work.
Window window=getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);

XIaomi device does not dismiss lockscreen when opening Activity from background (alarm event)

I have an application that opens an Activity on a certain event.
Just like alarm application. I use the following flags:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON
);
The application creates a wake lock for that purpose.
On most devices it works fine, but on Xiaomi Redme 2(Android 6) it does not.
When the application launches the Activity, the screen is turned on and the lock screen displayed. My Activity is NOT displayed.
After I enter password I see my Activity.
Then I have changed the package name, and Activity displayed successfully
without a lock screen.
It looks as though Xiaomi has black listed our original app for some reason.
I wonder if anyone has encountered this behavior and has a solution?
This is a Xiaomi/MIUI specific issue.
You need to grant special permission to an app to unlock the screen during alarm.
Go to System Settings > Permissions > Advanced Permissions > select the app and give it permission to access Lockscreen.
Source
https://sleep.urbandroid.org/faqs/?Display_FAQ=22281
FLAG_SHOW_WHEN_LOCKED is deprecated. You can also use this in old api,
final Window win= getWindow();
win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
And new usage, try this
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
keyguardManager.requestDismissKeyguard(this, null);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1){
setShowWhenLocked(true);
setTurnScreenOn(true);
}

Open the activity when device was locked

i'm running an API in service class. When i get the particular response from that api i'm opening the AutoPlay activity. For calling the activity i wrote
Intent intent = new Intent(BackgroundService.this, AutoPlay.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
and inside of onCreate in the AutoPlay activity i wrote the fallowing code to open the activity when device was locked
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
or
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
but above code was not opening the activity when device was locked. So, please any one help me to achive this

Android activity over lock screen specifically

Trying to show my Overlay activity over device lock screen - Its Working fine. The problem when my phone ringing | Alarm ringing that time I can see the overlay only. Cancel my overlay then only i can see.. how to avoid that
My code for display activity
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

How to launch the app from the start when started from home button

I'm creating an application that uses alarms.
That's how I start alarm activity (in broadcast receiver)
Intent myIntent = new Intent(context, AlarmActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(myIntent);
When alarm activity is closed, and the application is started again from home button, this activity is launched each time instead of my splash screen, and I have to remove last launched apps from recent apps menu, only then the splash screen is started when the application is launched.
By the way, the above flags are needed to unlock the screen if locked when the alarm is fired, and I have this in my alarm activity:
#Override
public void onAttachedToWindow() {
this.getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
WindowManager.LayoutParams.FLAG_FULLSCREEN
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
}
Any one has an idea?

Categories

Resources