I am writing code in Android to display an activity on the locked screen. The activity has an activity button which launches another new activity. For the new activity, I have added FLAG_DISMISS_KEYGUARD so that the activity can be showed on the lock screen. Ideally if the keyguard is currently active and is secure (requires an unlock credential) than the user can confirm it using the lock pattern or key code or whatever the user set and then the activity window should appear. It works as expected on all phones except the Xiaomi Phone - after clicking the button there is no prompt shown to the user to unlock. How can I resolve this issue?
Here is a snippet of my code:
I have this code in the onActivityResumed(Activity activity) method of the new activity:
final Window win = activity.getWindow();
win.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
The new activity will be resumed on the button click and hence I have it in the onActivityResumed API.
Related
I am opening my app from background when it receives a notification. For this I am using a service. Whenever my app is in background and a new notification comes, app will open automatically. It is working fine.
But when app is in background and phone screen is locked, now when a notification will come, it will show on screen(locked screen) and when user will open phone lock, my app's screen will be visible but screen will not responding, screen colour will also be like greyish layout on it.
To open app I am using a service, code is:
Intent serviceIntent = new Intent(getApplicationContext(), NewTaskService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(serviceIntent);
} else {
startService(serviceIntent);
}
In NewTaskService Class I am opening my app.
The resulted screen image when app is opened with notification from on lock screen is attached.
It seems like app is doing much work on main thread. Why don't you try to push your code in multi-thread or use Asynchronized class that handles everything in background
Basically, it was due to a dialog box. When app is opened and user unlocks mobile screen, dialog box disappears but screen is visible like there is a dialog box. Screen was not freezing or stucking, also the screen was greyish type due to that dialog box.
Because in such scenario dialog box view disappears.
I recently downloaded the ACDisplay lock screen app:
https://play.google.com/store/apps/details?id=com.achep.acdisplay
The application shows an overlay on my device while also at the same time detecting home button click on the activity so that i can't bypass the lock screen. It is also somehow able to completely hide the recent button on a non-rooted device. How is that possible?
I have went through the following links:
Detect home button press in android
How can I detect user pressing HOME key in my activity?
How can I detect user pressing HOME key in my activity?
and all of the solutions used to work for older versions of Android OR they say that the home button click cannot be detected since it can be used by malicious apps.
How is this application able to do so?
Can someone share sample code/app on how to prevent the user exiting the lock screen app until they have successfully authenticated themselves?
Thank you.
With Device admin permission
https://developer.android.com/guide/topics/admin/device-admin
you can pragmatically lock unlock device.
That app also use permission for "retrieve running apps" android.permission.GET_TASKS, so with that we can detect current foreground running app.
check answer.
https://stackoverflow.com/a/17756786/1025070
with that if user try to press home and leave we can instant check app is not in forground, and relaunch our activity again. (its workaround to detect if user leave from app with Home press).
Check my app https://play.google.com/store/apps/details?id=com.udayalakmal.applock&hl=en
that add overlay of lockscreen on any app that can not bypass. use same check foreground running app.
#user2511882
- Created sample app that simply load Activity when device locked, and another activity when device unlock
https://github.com/UdayaLakmal/LockScreenDemo
**This is only a demo, you have to use receivers with background service for continue monitor device lock state and handle memory leaks, .Tested with Android 6 API 23
no need to monitor running apps since this only use with device lock screen.
**Check how I get Home button press event in Lockscreen activity.
WindowManager windowManager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
}
layoutParams.gravity = Gravity.TOP | Gravity.LEFT;
layoutParams.x = 0;
layoutParams.y = 0;
layoutParams.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
View window = LayoutInflater.from(this).inflate(R.layout.activity_main, null, false);
windowManager.addView(window, layoutParams);
The following piece of code blocks the home, back and recents button as per the requirement.
Also requires the following permission in the manifest:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
You cant disable recent button and home button but you can achieve this by using Window Manager link, in one line its create an overlay over your android application screen.
I have a share button in the GCM notification. On click of the share button, I need to launch share intent. Everything works perfectly. Only problem that I'm facing is Lollipop lock screen feature. When I click share button from lock screen, my intent dialog appears below the lock screen and user has to unlock the screen to see the dialog. I want to unlock the screen programatically, when share button is clicked.
I tried with Power Manager, But all it's wakeClock flags are deprecated and WindowManager.LayoutParams.Flag_KEEP_SCREEN_ONis recommened to use. But I'm not using activity here. I'm using broadcastReciever context. and hence I cannot use getWindow()method.
I also tried with KeyguardManager. But even disableKeyguard() is deprectated.
I cannot use the Intent.ACTION_SCREEN_ON, as this should be used, if we want to perform any action after screen is unlocked.
i had used below intent to programmatically close the notification tray:
Intent it = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
mContext.sendBroadcast(it);
Is there a similar intent, that can be broadcasted to unlock the screen
Updated Code using DevicePolicyManager:
public static void handleShareBtnClick(Context context, String message) {
GcmHelper helper = new GcmHelper();
helper.shareMessage(context, message);
if(Utility.isLollypopAndAbove()){
helper.unlockLockScreen();
}
helper.launchShareforForAlert();
}
public void unlockLockScreen(){
DevicePolicyManager devicePolicyMngr= (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
ComponentName compName=new ComponentName(mContext, DeviceAdminReceiver.class);
if(!devicePolicyMngr.isAdminActive(compName))
devicePolicyMngr.removeActiveAdmin(compName);
}
Even after using DevicePolicyManager, It's not unlocking my screen
Step 1: Add below code in your activity before setContentView(R.layout.example);
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, then you will see activity in which you have added this code.
This will work even though your mobile is locked with pattern lock. This will work like a charm.
Say, I have got 5 notification for an app. Now my application is in closed state. If i click on one notification, the application is opening the needed activity. But How to make the notification to open the new activity every time i click the notification even the app is open.
The app has a splash screen and the needed activity is the third child of splash screen.
I have implemented as follows :
On clicking notification will launch the splash screen with some boolean set if it comes from notification. I am opening the need activity from the splash screen according to the boolean.
If i click the notification when the app is already open, it again starts from the splash screen.
How to implement opening a activity from notification as it happens in whats app.
What I am trying to do is replicate what the ToddlerLock app does. I have managed to clear the default launcher with
PackageManager localPackageManager = getPackageManager();
localPackageManager.clearPackagePreferredActivities("com.android.launcher");
and then open the launch select dialog with this
Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
this.startActivity(i);
As long as the user checks the "use by default for this action" the home key now sends the user to my app, thus essentially disabling it.
I then use "clearPackagePreferredActivities("com.my_application")" when I exit my app and the user has to choose a new default home app.
My question is how can I choose the default home application (essentially checking the "use by default for this action" check box in code for the "com.android.launcher" package. That way the user does not constantly have to see that dialog box every time they open and close my app.
I think ToddlerLock does this somehow without using clearPackagePreferredActivities
because if I look at the "clear defaults" in the application manager it is not cleared and you only have to go through the set as default dialog box one time on startup and once when you exit to set it back to the normal home screen.
Thanks for your help.
I have implemented the same functionality in different way.
Let's say you have 'LockScreenAcitivity' configured as Home Screen in Manifest.
Launch LockScreenActivity by sending Home Intent.
Android will popup a dialog, to select the default Acitivity
choose your LockScreenActivity from List as default Activity
.....
.....
While closing the Activity don't clear the prefered Activities.
Disable your LockScreenActivity alone by calling PackageManager.setComponentEnabledSetting()
After you disable your LockScreenActivity, android will rollback to previous Prefered Activity (that's your old home screen ).
Next time when you launch your app,
enable your lockscreenActivity again by calling PackageManager.setComponentEnabledSetting()
Launch LockScreenActivity by sending Home Intent.