I am trying to display my application over the lock screen on android.
I have it working some of the time. But certain scenarios will not work.
I am starting my activity which extends a React Native Activity from a service like so:
Intent callIntent = new Intent(context, MainActivity.class);
callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(callIntent);
And I am setting the flags on the window like this in the onCreate method:
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
When it works:
If the activity has been started when the device is unlocked and the app is still in recents. The device will wake and the activity will be displayed over the lock screen.
Activity state changes witnessed:
onRestarted
onStarted
onResumed
onPaused
onResumed
When it doesn't work:
If the app hasn't been run since boot or the app has been removed from the recents. The device will sometimes wake and sometimes the app will flash for a second before returning to the lock screen. When the device is unlocked the app is running.
Activity state changes witnessed:
onCreated
onStarted
onResumed
onPaused
onStopped
I'm not sure if this is specific to React Native or not.
Related
I have an activity in my application that does a process and when finished, launches an activity with a normal intent:
Intent intent2 = new Intent();
intent2.setClass(anActivity.this, JustAnotherActivity.class);
startActivity(intent2);
finish();
The problem is that when you are doing the process, if I press the home button and take it to the background, when it finishes and calls the new activity, even if it is in the home of the phone, this new activity jumps to the foreground.
I am looking for the activity to be launched, but if the user is outside the application, it does not come to the foreground.
I guess the use of a FLAG is necessary, but the ones I have tried have not worked.
sorry for my bad english
Thanks.
what if you take care of ActivityLifecycle (OnPause, OnStop).
You can add a flag when the activity goes "OnPause/OnStop" depending what you want. And them perform the intent just if the activity is not "Pause/Stop".
Activity Lifecycle
My app has a service that runs in the background. It wakes up on intent from the user which is typically generated from another application (such as sharing to my app from the web browser). After the service is done performing a task I want it to open an Activity that displays some info about the task that was completed. When the user is done looking at this info, I want them to be able to hit 'Back' or 'Close' and return to what they were doing previously (not within my application).
I've only managed to get the activity to close leaving my App's main activity front and center on the screen. This only happens if the app is still running. If it's been killed then it correctly returns to the web browser where the user intent was generated from.
How can I make the 'Back' button close the activity and return to the previous app from which this entire workflow was started?
EDIT:
This is the code I am executing from within the android service:
Intent intent = new Intent(this, TestActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
When I tap the back button from the TestActivity it returns me to the main activity of my app, not the previous app I was in when the service started TestActivity.
In second line you are using Intent.FLAG_ACTIVITY_CLEAR_TOP which is clearing your previous activity
I have an app which when the device locks it starts an activity instead. By doing this the device doesn't lock but it shows an activity. This is OK. My problem is that if I am already in the activity and when I press the power button the device locks.
In the BroadcastReceiver I can see the screen is off:
if (action.equals(Intent.ACTION_SCREEN_OFF)) {
isScreenOff = true;
final Intent serviceIntent = new Intent(context, Screensaver.class);
serviceIntent.putExtra("screen_state", isScreenOff);
context.startService(serviceIntent);
Log.i(TAG, "SCREEN OFF.");
}
Then the onStartCommand() in my service is called. In it I start the activity. In the activity I see that the onResume() is called and then onPause() and that's it. The device is locked and the screen is off. If I unlock it manually, the activity is there, but it shouldn't be necessary to unlock it manually. Do you have any idea how I can fix that? (Do not hesitate to ask me if I need to post more code).
EDIT
Here is a video to get it more clear. In the end of it I am in my screensaver activity and when I press the power button the device locks and does not starts the screen again.
I figured it out! Since the activity started when I lock the device, calling Activity.recreate() in onStop() fixed my problem.
I have an activity. The launch mode is singleTask, and I implement onNewIntent. When I send an intent to launch this activity while the activity is open (with flags NEW_TASK and RESET_TASK_IF_NEEDED), the screen flashes before showing me the same activity.
Why could this be happening? I thought singleTask should be enough to ensure that new intents are delivered to onNewIntent, rather than the system spinning up a new task or anything for my activity. The flashing goes away when I do either of the following things:
change the launch mode to singleTop;
add the flag Intent.FLAG_ACTIVITY_CLEAR_TOP to the launch intent.
I'm curious to know what's going on and what, if anything, I could do to overcome the flashing (I'd really prefer not to change the launch mode or launch intent flags, if at all possible). Basically, is there any good reason why this should not work?
Note that I do get onNewIntent, even though the screen flashes.
I am facing issue in Activity which is calling from Broadcast receiver.
My application contains alarm system, so when time of alarm match, at that time, broadcast receiver calls one activity to get in front. This activity is not in full screen, it is one type of alert box using RegionSearchDialog as theme. (Don't be confuse, I am using activity only, my class extends activity, but the theme in xml set as RegionSearchDialog)
My platform of development is: 4.0.4
Now my issue is: if my device is on (unlock keygurad) either application is in front or in back, it works fine. But if power is off (sleep mode / device is lock), it will call same activity, onCreate() calls first then onResume() and then it will call onPause() as my device is in sleep mode.
I want to keep that activity running, don't want to get it sleep.
So, when alarm time match, it will buzz alarm and if its in sleep mode then user can unlock device and see popup of that alarm.
Thanks in advance to help me in that.
This is the way Activities in Android are supposed to work. You would be better using a Service or using the Alarm manager to launch an Activity at a certain time as these are more suited to what you are trying to do =).
Here is my code,i think it can help you:
In your activity on create():
super.onCreate(savedInstanceState);
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);
setContentView(R.layout.main);
and main xml is:
<activity
android:name=".main"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Wallpaper.NoTitleBar"
android:launchMode="singleInstance"
android:excludeFromRecents="true"
android:taskAffinity=""
android:configChanges="orientation|keyboardHidden|keyboard|navigation" />
pls test these code in your project. :)
In addition to the response by 'enjoy-writing':
It sufficed in my android app to set the flags as described in the onCreate code.
The effect was that the app would pause when going into sleep mode (e.g. through pressing the On/off button on the phone), and resume when returning from sleep mode.
By removing the FLAG_KEEP_SCREEN_ON flag you can still have the phone fall asleep if the view isn't used for a while.