Android to always run activity - java

I'm making an Android app that act as an IOT host (I use serial communication and send it to Arduino). Is there a way to always set Android to run this activity (never sleep & always inside the activity)? It is important for it to not leave the activity since all the control and db access is done through it. So far what I've found already available is kiosk app in which it act as a launcher with limited app and such but none of the lock the android to an activity.
Thanks for answers & comment!
If you're curious :) The reason I use Android is that it needs a control panel UI and connection to a database, using RPI and other stuff would just get it to be more expensive.

there a way to always set android to run this activity (never sleep & always inside the activity)?
Not really. You are somehow looking for kiosk mode, or you can make your app acting as launcher (but it all depends on use case - if your app is only one on the device then this is all fine, otherwise you would need to put a lot of efforts to jail user)
it is important for it to not leave the activity since all the control and db access is done through it.
It sounds like your app architecture is just designed wrong.

Related

Application restart

I have been running into a problem with my application, which I have no idea why. The following is:
the application is a large, commercial project, which makes several connections to the database, with a login system and everything else
I noticed that if I leave the app in the background for about 10 minutes, for example, it terminates my connection and restarts everything again, forcing me to log in again.
The only guarantee I can give is that they are not connection problems with my server, as in my tests it never failed.
Anyone who might have any idea why?
(I know that the explanation of the problem was a little vague, but the situation is very vague even for me)
How did I find my problem?
The user of my application was using a bluetooth barcode reader, which was programmed to automatically turn off after a period of inactivity. I noticed that whenever the bluetooth device turned off, my application would lose its previous state if it was in the background. Using the LogCat tool, I realized that my process was being killed by the system itself. The messages always looked like these:
W/ActivityManager: Force finishing activity
my.project/.view.activities.MyActivity
I/ActivityManager: Process my.project (pid 12984) has died
After much research, I found that the Android system interprets some external events as configuration changes, eg screen rotation change, Bluetooth device connection/disconnection, etc.
When such a change happens, Android, by default, kills your app's process and restarts it completely again, so that the app adapts to the new behavior. In my case, there was a NullPointerException in the code, which I hadn't handled correctly, so the application went back to the beginning, losing its state data.
However, in other application screens the mentioned Exception didn't occur (so it doesn't go back to login when starting), but even so I lost some screen data, like something that was typed in an EditText, for example.
How did I solve it?
On researching again, I found that you can let Android handle these configuration changes itself, telling it not to restart its process. To do this, just add in your Manifest, in the desired activity, the line:
android: configChanges = "keyboard | keyboardHidden | navigation"
As in my case the problem was with a bluetooth keyboard, I added these options keyboard | keyboardHidden; some keyboard models, for some reason, also change Android navigation, so I added navigation. After this change, done! No more problems!
P.S. 1: Unfortunately, not everything always works out. Adding android: configChanges won't work if your activity has fragments (I'm still trying to figure out how to solve this).
P.S. 2: This is not a good practice, I need to make that clear to you. For me, it's okay to do it this way, as my application responds well to changes. After all, my app is simple. Only use this feature that I explained if it is your last option or, if like me, your application is not so complex. Remember: this is not a magic solution to problems; in my specific case it worked fine, but for you, it might break your application.
P.S. 3: I recommend taking a look at https://developer.android.com/guide/topics/manifest/activity-element#config, in the android subtopic: configChanges. Listed are all the configuration changes a device can make.

Is it possible to know the app which is opened currently?

I am a beginner in android development. I want to know that is it possible to know the app which is opened currently. I came to know that finding the apps which are running currently through Activity Manager (getRunningTasks()) is now removed from the Android studio. So I want to know is there any other way to know? I just want to know the app which is opened and running currently on the mobile but not the apps running in the background Could somebody please help me in this case?
You can use AccessibilityService to get event notification. But AccessibilityService are specifically for accessibility uses. If you use the service for other purposes, then the application will more likely to be downed/removed for PlayStore due to policies. Another viable option is UsageStatsManager, but with some limitations.
UsageStatsManager is not push event based system. You have to poll in few mills(depends upon the use-case)
Usage Access Permission grant/deny is not straight forward. You have to start Setting Activity with Settings#ACTION_USAGE_ACCESS_SETTINGS action and have to rely on the users understanding of how to grant permission(Since permission list may contain other applications).

Is it possible to programmatically ask Android to SaveInstanceState?

I am new to Android and making my first real app and it uses the socket.io Java Client for communicating with my Node.JS server instance. I have recently discovered while testing that the OS can kill background apps whenever it feels necessary calling onSaveInstanceState() on the active Activity then when you switch back to the app it recreates the previous state using the Bundle passed as an argument in the onCreate() method.
So now that I know that, I need to implement a solution that would keep the app working as it should so I expect I will have to test a few solutions before I get the right one. And since it would be so time-consuming to just lock my screen and watch the server logs waiting for the
User disconnected
(which can take a lot of time for each try), I wondered if anyone here ever was in the same situation and figured a way to make the OS kill the app as soon as I lock the screen or hit the Volume Down button or something like that.

How to block application launch in Android

I want to create an application in Android that will show the devices list of applications and then allow the user to select which apps they want to be restricted access to for a certain period of time.
I am aware there is an Android Application named "AppBlock" but i don't know how this works.
You select the app, the period of time and then it doesn't allow you to open the app.
You cannot actually block start of another application.
The only way that I see it is possible: you need to save the list of applications Info in your app that need to be blocked with the time when it need to be blocked. Implement a service that runs "forever" and detects started applications.
Refer to this answer about how to do it. On each detection you should check if application present in your database and if the time now says it need to be blocked. If it is - close the application. Refer to this answer for learning how to do it.
That is global architecture I think you should follow.
To Build App Block u need the following things
1-Accessibity services (AS)2-Forground Services (FS)
BY AS you will be able to stop activity that you have in your bloker list
FS will alive your application context that help to find block app

Get State of Toggle Button in Another Running Android App

I am writing an App (MyApp) in Tasker that would need to read the State of a Toggle Button in another App (AppB) that I haven't written. It's for reporting purposes. I am a seasoned programmer, but are new to Android. Please bear that in mind:-)
Tasker have support for Java code, and have access to the full SDK of my Android 4.3 Phone.
The app will run on Android 4.3+.
I am already using AutoInput that is perfectly capable of getting the ID of widgets (Buttons etc) in other Apps, and AutoInput is also capable of Pressing these Buttons (AutoInput Accessibility must be turned ON). AutoInput will have support for what I am asking later, but I badly need it now!
So the question is:
How can MyApp get the State of a Toggle Button (with ID "toggle") in AppB using Java?
I am afraid this will not be possible by easy means. The applications in Android are isolated and cannot ask for others' applications controls due to the security policy. Without the access to the AppB code and possibility to change it this will not be possible.
The AppB would have to share the state of the toggle to your app: either via content provider or simple intent each time the toggle is changed. This requires changes in AppB code which I assume is not possible in your case. I see no other way to get around this.
I am writing an App (MyApp) in Tasker that would need to read the
State of a Toggle Button in another App (AppB) that I haven't
written
In Android, by default each of the application runs in its own process and each app is Sand-Boxed.
Therefore, you cannot read the data from other application until it is shared explicitly by the particular application.

Categories

Resources