I am wringing a default phone app, and I need to unlock the device once a new call is coming in. I have been trying to do it like this:
PowerManager powerManager = (PowerManager) getApplicationContext()
.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
PowerManager.ACQUIRE_CAUSES_WAKEUP, getPackageName() + ":Call");
wakeLock.acquire();
And this is the definition I used in Manifest. My min API is 23
<activity
android:name=".call.CallActivity"
android:launchMode="singleTop"
android:noHistory="true"
android:showForAllUsers="true" />
Note that PowerManager.FULL_WAKE_LOCK was deprecated in API 17 and FLAG_KEEP_SCREEN_ON is suggested to be used instead, however with this setup my screen is not being waked, and when I turn it on manually, my activity is not shown on top of the lock screen as the flag in the manifest suggest.
From the showForAllUsers docs:
Specify that an Activity should be shown even if the
current/foreground user is different from the user of the Activity.
This will also force the
android.view.LayoutParams.FLAG_SHOW_WHEN_LOCKED flag to be set for all
windows of this activity
So what is the proper way to do it in API 23? My goal is to wake up the device and show my activity but do not unlock the lock screen.
Try adding android:showOnLockScreen="true" in the AndroidManifest.xml for your activity:
<activity
android:name=".call.CallActivity"
android:launchMode="singleTop"
android:noHistory="true"
android:showOnLockScreen="true"
android:showForAllUsers="true" />
Related
I want to kill / fully close an app so it doesn’t run even in the background when I press the screen turn on/off button or if the screen times out. I couldn’t find a solution anywhere on the internet. Can you guys help me out with a code snippet? Thanks
you can refer this link to detect screen turn off
Screen off Broadcast receiver and for killing the app you can use below code
int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);
first check if the screen is locked inside a service that runs in the background:
KeyguardManager myKM = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
if( myKM.inKeyguardRestrictedInputMode()) {
//it is locked
getActivity().finish();
System.exit(0);
} else {
//it is not locked
}
then you simply kill the app if the screen is locked.
hope this will help.
To make activity like a toast (appear-and-go) add following code into manifest:
<activity android:name=".YourActivity"
android:label="YourActivityLabel"
android:taskAffinity=""
android:clearTaskOnLaunch="true"
android:excludeFromRecents="true"
android:finishOnTaskLaunch="true"
android:noHistory="true"
android:launchMode="singleTask">
</activity>
I have Activity1 which is a list screen of items. Each item can be viewed in a separate Activity2 which is displayed as a popup. Activity1 can be launched from the background and displayed even when the screen is locked. Activity1 may also choose to automatically display the contents of an item in the list screen by starting Activity2. We can bypass the lock screen because both activities have the WindowManagerFlags.DismissKeyguard enabled in the OnCreate method.
Before Android Lollipop everything worked as expected. But now the popup Activity2 is not visible unless the device is manually unlocked. If I change Activity2 to be a full screen Activity then everything seems to work (Except transitioning from one activity to another will briefly display the lock screen). Any ideas on how to fix this cleanly?
Also, I have only tried the Galaxy S6/S6 Edge devices which have this new Knox security feature on them.
Edit I have changed Activity2 to be a DialogFragment instead of an Activity. This worked for me best because the suggested answer used code that is deprecated or obsolete depending on the target sdk. Activity1 is using the following flags to bypass the lock screen when needed.
getWindow().addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
getWindow().addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
Since Activity2 is now just a DialogFragment, it uses the window flags of the parent Activity1. I also remove those flags on the "android.intent.action.SCREEN_OFF" action so that the activity bypasses the lock screen only when launched as a notification and not every time the activity is at the top of the stack. Permissions mentioned in the answer are required.
For sure this snippet should help You (works with Lollipop):
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "My Tag");
wl.acquire();
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
keyguardLock = keyguardManager.newKeyguardLock("TAG");
keyguardLock.disableKeyguard();
and when leaving Your activity (i.e. onStop(), onPause() and onDestroy()):
keyguardLock.reenableKeyguard();
Also, don't forget about permissions:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
I'm trying to start an activity from a service I had already acquired the lock for as follows:
Intent i = new Intent(context, MyActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION);
startActivity(i);
The activity manifest is declared as follows:
<activity
android:name=".MyActivity"
android:configChanges="orientation|screenSize|keyboardHidden|keyboard|navigation"
android:excludeFromRecents="true"
android:launchMode="singleInstance"
android:screenOrientation="nosensor"
android:showOnLockScreen="true"
android:taskAffinity=""
android:theme="#style/MyTheme" />
And finally, on onCreate() or on onAttachedToWindow() (I tried on both), I add the following flags:
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
| WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
The problem is that the first time I call startActivity() from my service, the screen turns on but the activity itself does not show up. It shows the lock screen instead. Every subsequent call of startActivity() works properly but I can't find a reason for this odd behavior.
I tried already suggestions to get a full wakelock instead of partial, change the flags and values in the manifest according to the following SO answers:
Android Activity Not Showing When Screen is Woken Up and Lock Screen Not Disabling
how to unlock the screen when BroadcastReceiver is called?
Programmatically turn screen on in android
Android Galaxy S4 -- Activity that is visible over lock screen
Note that my theme is not a dialog but a fullscreen activity.
Any other ideas?
I'm facing the same problem, after a lot of searching here and google, found this which unlocked the screen and popped my activity but it only works for me when the app is running (foreground/background).
import android.view.Window;
import android.view.WindowManager.LayoutParams;
Window window = this.getWindow();
window.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
window.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
window.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);
i'm trying to start an activty when app is closed... (using broadcast receiver)
in the docs (for example here) and most of the answers on SO the flags are added this way:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
but when i tried the way it is like in the example it unlocked the screen instead of just turning on the screen.
hope this help . it still didn't solve my problem completely.
EDIT:
found this post which solved my problem.
there is a comment there on NOT using a dialog theme which solved it for me
Step 1: Add below code in your activity before
setContentView(R.layout.activity_about_us);
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);
Step 2: Lock your mobile than you will see activity in which you have added this code.
You can implement this if you want to open particular screen by notification occurrence like skype call.
Since my application already includes a Service, this is what I do: if the screen is locked, I register a broadcast receiver (a bit simpler than this one, since it reacts only on unlocking) that starts the Activity as soon as the screen gets unlocked.
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.
I'm making my alarm app. There is an Activity to show alarm infomation.
I want turn on the screen and unlock it. I wrote these code
AlarmActivity.java:
public class AlarmActivity extends Activity {
......
void onCreate(Bundle bl) {
.....
final Window win = getWindow();
win.requestFeature(android.view.Window.FEATURE_NO_TITLE);
win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
LayoutInflater inflater = LayoutInflater.from(this);
setContentView(inflater.inflate(R.layout.alarm, null));
}
......
}
AndroidManifest.xml
{activity android:name="AlarmTaskActivity"
android:excludeFromRecents="true"
android:theme="#android:style/Theme.Wallpaper.NoTitleBar"
android:launchMode="singleInstance"
android:taskAffinity=""
android:configChanges="orientation|keyboardHidden|keyboard|navigation"/}
It's ok,but when I change
android:theme="#android:style/Theme.Wallpaper.NoTitleBar"
to
android:theme="#android:style/Theme.Dialog"
the screen did not turn on nor unlock, I am really confused....
Can you please tell me how to make the screen turn on and unlock when I use "#android:style/Theme.Dialog"?
Thanks
by the way,I have android 2.0 in my test device.
In order to activate the screen and force it to stay on, you need to use a WakeLock. See the documentation for more information. Changing the theme along won't cause the screen to wake up. You'll need to use the ACQUIRE_CAUSES_WAKEUP flag in order to do that.