I am new to android dev. Our requirement is to run a background service in android. Which is unkillable or it is capable to handle KILL signal from taskmanager..
Is there is any way to do it?
I have heard about addShutdownHandler, But it wont fired when user kills it.
Thanks in advance
Is there is any way to do it?
Only by building your own custom version of Android, with your code in a standard C/C++ daemon, and then distributing a ROM mod that contains that customized version of Android.
If you are trying to create an ordinary SDK app, the user or Android itself can get rid of your process at any time, for any reason.
In general: not possible.
I should add that trying to impose such a requirement makes you sound nefarious -- there's a reason the user is allowed to kill processes.
From man 7 signal:
The signals SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.
This is fundamentally by kernel design. Think about it: if a process could ignore all signals whatsoever, then how would you stop it if it ran away uncontrolled? No, what you'll have to do is see to it that the KILL signal is not sent. Running your program under its own userid should achieve this, as long as no one with administrative privileges generates the signal -- but even this requirement may be practically impossible to meet. By design, the system provides the basic ability to kill a process, on the theory that the user or sysadmin might (for whatever reason) want the process killed.
Related
I'm trying to create an app that manages a BLE connection in the background (receives notifications, reconnects and subscribes to a characteristic when connection is lost and the phone is moved near the BLE device again).
Right now I have the app somewhat built - everything that I need works relatively well but only when the app is open. Although the code isn't up to standard as this is my first app and I don't have any previous experience with Java/Kotlin/Dart (this is also why I'd rather not touch anything outside of flutter).
I need the app to send me local push notifications when tabbed out of the app or when screen lock is on and as far as I understand one could do this with isolates. I've searched a lot but found one guide which is kind of hard to understand as a beginner and also uses Kotlin.
Can I get away with only dart code for background code execution (described above) and is it suitable for a beginner?
Also, let me know if you have any alternative solutions since all I need is to get a notification on my phone whenever it receives a notification from a BLE device (and also auto connecting). I've already tried this with kivy since I'm more familiar with python but with no luck.
Talking about BLE there's this plugin by the way: https://pub.dev/packages/flutter_reactive_ble
As for the isolates, they are not ideal for this even though they do run separately, they'll get killed eventually. There's also a little helper package that makes working with isolates very straightforward if you want to try it: https://pub.dev/packages/computer
And so actually for the task at hand (background execution) I would suggest researching/using this one: https://pub.dev/packages/background_fetch
I would not encourage you to use any timer-based solution that tries to wake up your phone every 15 minutes or so, since that uses unnecessary CPU time.
Now I'm not that much into Flutter, but at least on Android, the "correct" way to have an ongoing connection (auto-connect) is to have a Foreground Service in the process running, so the process does not get killed. Then subscribe to characteristic notifications as usual, and handle the values as they arrive, either in Java or Flutter (by using some Java <-> Dart bridge).
I would like to check the user activity an app is tracking. The Audible app specifically.
e.g. does it log every tap? Does it log how long each session is etc.
Thank you.
This is not possible, no. Computers are not capable of this sort of sophisticated reasoning about a program's possible behaviors (see, for example, the Halting Problem, which shows that an algorithm can't even determine if a given program will finish running). Only a human reverse-engineering the software by hand could possibly make this determination, which would be a very time-consuming process for an app of even moderate complexity.
I am currently working on an Android project that monitors what applications a user is running and cross-checks the corresponding processes with a whitelist stored internally on the device.
In order to make this work, I need to know what the default or system processes for the device are so I can add them to the whitelist. That being said, I have a few questions I was hoping you might be able to answer:
Is there a way to differentiate between a default/system process that MUST be running, and a process that belongs to an app on the device?
Are there different default/system processes depending on what phone/version of android the user is running?
If so, are those process names available somewhere for developer use? Or is there some other way to obtain them?
If I need to elaborate more please let me know, thanks for the help.
Let's say that you try ActivityManager and getRunningAppProcesses(). Iterate over that array of RunningAppProcessInfo objects and find those with importance of IMPORTANCE_FOREGROUND. If the docs are correct (haven't tried this), there should only be one process that is IMPORTANCE_FOREGROUND -- the one that is truly in the UI foreground.
(services can call a startForeground() method to get foreground priority, but I am guessing they have IMPORTANCE_PERCEPTIBLE)
You could then examine the pkgList of that foreground process and compare that against your whitelist.
However, this breaks down if:
Something pops up asynchronously (alarm clock app, incoming phone call, etc.)
An app that is logically in your whitelist has changes that affect its package name (e.g., developer released a "pro" app that a student paid for, and the whitelist only has the free app)
if the device has multiple visible items (e.g., Samsung's multi-window capabilities), if all visible apps are not IMPORTANCE_FOREGROUND
This at least gets rid of the problem of pure background stuff that the student cannot control, including your "default/system processes".
However, it requires you to continuously poll, which will be a serious detriment to battery life. That, plus the privacy implications, means to me that this app should, at best, only be used for exams, and should be something that the student can install shortly before the exam and remove shortly after the exam.
Well I think my team and I have come up with the best solution so far. After reading the Android Docs, we found that by using ActivityManager.getRunningServices() we can use the constant FLAG_SYSTEM_PROCESS to determine what processes are core system processes. Then all we would do is cross-check that with the total list of running processes to differentiate between them.
I am studying Android driver/kernel. I find that there are 2 set of watchdogs:
kernel/watchdog.c
/drivers/watchdog/tegra_wdt.c
Both them are used to recovery the system as system hang on an event (e.g. infinite loop).
The first one is enabled in system boot time by default, but many people still implementing their watchdog in /drivers/watchdog/ folder for specific driver. I confuse about this. Why should we need multiple watchdog? Is any one know about the "philosophy" about this?
Because Watchdog is used for the secure thread, with the help of this we can stop the thread so it will not be able to perforrm any longer.
I'm trying to make a stopwatch & countdown app for WindowPhone 7 using Silverlight for WindowPhone SDK and trying to make it run in background when it's tombstoned. In Android, I can use android.app.Service to run it in background. According to MS AppHub Quickstart, "The Windows Phone operating system doesn't allow any third-party applications to run in the background". Please help me if you have any idea for keeping the countdown running when a phone call is received or the phone goes to sleep. Thank you.
At the moment, once your app is tombstoned, your app cannot continue with any custom processes such as having your countdown continue. The Mango SDK coming out this month allows for a bit more freedom in this respect.
If, however, you want to run your app under a lock screen, this is possible. You can simply do this:
PhoneApplicationService.Current.ApplicationIdleDetectionMode = IdleDetectionMode.Disabled;
Jaime Rodriguez has a great post on running under a lock screen which you can read here.
Until Mango comes out, there's no way to have your program run in the background.
As keyboardP notes, you can't continue your process on the phone.
However you could use a server and Toast message popups to still notify the user. It requires more setup and a server to run against, but it will meet your requirements.
One thing I have seen with other people making apps like this is to store the time that app was tombstoned and then, when the app is reactivated to look at the difference between the current time and the saved time and deduct that from the countdown.
This may or may not be appropriate, depending on your requirements but may be something to consider.