ON/OFF screen detecting (in Android API 22-27) - java

I'm making an app that tells you how much did you work with your Android device in your day. So I set something like stopwatch but I don't know how to stop it when the Screen goes OFF and how to resume it when Screen turns ON. (even if the app is completely closed)

Related

Is there a way to know when the screen is getting ready to switch off on an Android device using Xamarin.Android/java

I'm trying to figure out when a device is getting ready to switch off it's screen.
For example, I have a Samsung Galaxy S22 - when I press the power button the phone screen is turned off. I can capture this using a BroadcastReceiver which is ok.
However, before the screen is turned off, the screen actually fades to black. I am building an app that needs to seamlessly show itself for 5 seconds before the screen is turned off. I don't want it to first fade to black, then switch the screen back on and then show my app - which is currently what is happening. Is this possible? I'm just looking for a better user experience. Any other suggestions are welcome.
I have tried to google around and I'm not able to find anything.
EDIT: It's also worth noting that I have an AccessibiltyService running. So my application may not have an active Activity on the screen. The Activity is launched once the screen goes off - but I do want it to launch before the screen actually goes off.

Restart App Overlay after unlocking Phone

I have an Android application written in Java that uses an Overlay Service to display a search bubble similar to Facebook chat-heads or One notes Overlay.
The problem I'm facing is that on my new phone (Samsung) (Android 9) overlays get terminated after around 5 minutes of the lock screen. The OneNote Overlay visibly restarts.
I tried to implement the same by checking if was terminated by the system, not the user.
I also found a Broadcast Receiver that calls a function when the phone is unlocked even when my application is in the background.
Here I get the following error:
Not allowed to start service Intent { cmp=com.[...]/.[...] }: app is in background
What can I do to go around that?
Do I have to "push" my app to the foreground, start the overlay, and move it back in the background? is that even possible?
I guess you are using Android 10. (Not sure though). In Android 10 there's a new restriction that prevents you from starting an activity from a background service. More information here: https://developer.android.com/guide/components/activities/background-starts

Creating a custom android shutdown system

So recently my phone was stolen and besides all the trouble I had with the lost files I was pretty angry about the fact that it didn't came to my mind to create an app for android that prevents easy stealing or to be precise that will act as a Trojan horse inside of the thief's pocket. So what i thought about is an application that kicks in whenever you're holding your standby button or whatever you usually do to shutdown your phone. This application then looks like the real shutdown menu and asks you to shutdown with a pin or without one and whenever you shutdown without the right pin the phone will actually fake shut down in a way where it is still transmitting GPS and connecting to the next transmission tower so it is easily locatable (by fake shutdown I mean a black screen and silent mode until you are repeating the same method you used to shut your phone down). So is there any way of creating an app that has a higher priority than the android shutdown system so a custom menu pops up whenever you're trying to shutdown your phone?
Your idea isn't entirely impossible for Android.
There is no way for an app to have a higher priority than the Android Shutdown menu. However, that doesn't mean there's no hope.
If you remember popular Alarm apps, they typically have a feature that prevent you from turning off your phone when the alarm rings. This is usually done by detecting that a Long Press of the Power Button is pressed and then dismissing the Power Off Menu.
You can look at this answer for how to do this: https://stackoverflow.com/a/53679477
However, this is just so you can dismiss Android's Power Off Menu so you can show your own Power-Off Menu.
Afterwards, with your idea of displaying a Black Screen and Silent Mode... there's actually four three problems.
Screen Backlight for non AMOLED screens
Status and Navigation Bars
Can't block a Forced Shutdown
Personally, during the early days of Android and my early years of Android development, I released an app with a similar idea (have the phone give off the illusion of being turned off, so there's a higher possibility of phone remaining on if stolen), but due to those four problematic areas, the app has been retired.
So I speak with experience in saying that:
A black screen doesn't play nicely with screens that require a backlight. Even at lowest brightness settings, a completely black screen will still easily be seen as on.
Android doesn't have a way permanently hide the Navigation and Status bars. The best you'll get is Immersive Mode, but this mode will cause the bars to temporarily appear if you touch the screen. Even if you use a Full-Screen Activity, swiping down or swiping up will still show these bars. I believe your only option for hiding both of these bars is Root.
There's absolutely no way to block someone from doing a force shutdown. This completely bypasses the Power Off Menu and it's simply unblockable. So while your app might block some thieves, it might not block the more paranoid ones.
Hopefully, my answer is enough to nudge you in the right direction for your app.

Android: Re-open app after X minutes of idling?

I need my app to open itself back up after a certain amount of minutes that the device has been idle. In other words, instead of the device going into sleep mode and going to a black screen, my app will open back up (assuming it's running in the background). Think of it as an "idle screen" app.
It's fairly simple in theory, but I just can't figure out if it's even possible.
Assuming you have some service running, you can reopen the application by using one of the methods described here Launch an application from another application on Android

Receive data of multitouch events from laptop's touchpad

I'm working on writing a program that controls Android device (via USB cable and the Android-SDK toolset).
The program is written in Java, and is addressed for Windows platforms.
The idea is that I get a screenshot of the device's screen every few seconds and show it in a Frame, and then I can simulate taps and swipes on the device's screen using the PC mouse.
So far everything is working as expected.
The thing is I now want to enable the ability of simulating multitouch events on the device, and I was thinking - why can't I receive such events from a multitouch device such as the touch-pad every modern laptop comes with.
How can I do it? is that hardware dependent? Platform dependent? Would I need to implement some sort of interface with drivers for every touch-pad I want to support, or is there a way to do it simply like I write a regular mouse listener?

Categories