I'm trying to check what app is in foreground and launch my app if user locked that app. To do so I fire alarm, start service to check for active app and set alarm to fire in one socound from now. But somehow the alarm is allways delayed to 5 secounds..
This is how Im setting it:
alarmMgr.setExact(AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + 1000, alarmIntent);
I've tried it on android 5.1 and also on 6.0. It's the same.
Do you know why it is delayed? or do you know about any better way to check for active app as soon as possible? because I think this way consume lot of battery.
Thanks.
So this is what I was looking for. Only disadvantage is that user has to enable accessibility service manualy from settings.
https://stackoverflow.com/a/27642535/4506191
Related
For my app I want to fetch data from a server in the background. When the user wants to be notified about specific changes, I want my app to keep fetching data even when the app was closed. I was researching about this topic and found out that the possibilities of running services continuously also after the app is closed changed with Android Oreo. What I don‘t see are the alternatives to handle running services in background with newer versions of android. The most proposed solution I saw is using a Foreground Service. But since I don‘t have any user interaction, I don‘t think this solution fits perfectly. Also I read about the WorkManager but unfortunately it has a minimum time interval of 15 minutes. Are there other alternatives to process data in the background continuously than a Foreground service and a WorkManager?
You don't need "user interaction" to have a Foreground Service, what you need is a Notification that cannot be removed while the service is in foreground.
If your notification Context is the service itself, tapping on the notification will take the user to the "settings" where he/she can force close the service for example).
You can start your FG service in a BOOT_COMPLETED broadcast receiver. You have to quickly (5 seconds or less) send it to Foreground or android will throw an exception. Unfortunately, there's a bug which Google thinks it's ok and closed as "won't fix" but you'll still need to work around.
Long story short, have an onCreate in your service:
#Override
public void onCreate() {
super.onCreate();
final Notification notification = createNotification();
startForeground("SOME_UNIQUE_ID", notification);
}
You get the idea (the createNotification() is whatever you want to create a Channel (if O+) + Notification.
HOWEVER, all this is nice, but it will have a service running all the time, which is probably not what you should do.
You can use an Alarm a trigger a more efficient (in terms of scope) Intent Service to do the job, schedule another alarm in, say 5 minutes, and do it again.
Or -as commented- you could use Firebase, and Notifications to react to a change.
All in all, those are the options.
I'd say that unless you truly need "real time" updating, the 15 ~ of WorkManager is usually fine, if you can deal with the fact that you have to design your code to be very independent, because WorkManager cannot receive much information and it's a pain to use for some things...
IN bound service when I lock the device after the 70- 80 seconds service stops I am testing on realme 3 pro and one plus 5 both having API 9 PIE. How to keep service running until the app is in recent activity. Even foreground services are also stopped. Thanks in advance.
Realme, Oneplus, Samsung, Xiaomi, Huawei and a few other manufacturers have their own layer of "Battery Saver" or "Security" that kills or restricts background running apps in order to improve security/battery backup, unless user white-lists your app in the relevant device settings.
What i found out about Xiaomi is that they white-list well known apps like Facebook, Whatsapp, etc. But you cannot request Xiaomi to white-list your app.
These manufacturer apps simply terminate your app process. So your app should behave in such a way that termination at any moment should not create inconsistent state.
Also, you can always show a popup to user after installation asking him/her to go and white-list your app in battery saver. In Xiaomi, the user will need to select 'Do not restrict background activity' and also enable 'Auto Start' for your app. As far as i know, there is no other solution.
Conclusion: Visit https://dontkillmyapp.com and see how the apps are whitelisted for your OEM. Once whitelisted, it will work as expected.
But out there in real world, it will get killed and users won't manually whitelist it.
Custom Roms have it's own security apps and it close the app service when u remove it from recent or after few minutes.
To keep running your app in background you have to enable autoStart from settings
Refer this : How to fix the issue that the activity is killed automatically after some minutes in newly version as like vivo and oppo
Facing same issue from long time, Try to add FCM in your application and start service from it directly or else set alarm and startservice.
In some cases above solution work. i have added above solution in 4 project but succeed in only 1.
Issues which I have found while overcoming problem with firebase.
Firebase Notification coming to app but Background service not starting
Firebase Notification Not coming to application
Firebase Notification coming and service starting with Broadcast Receiver (Sometimes
service not starting directly from onMessageReceived() method of Firebase so i
decided to set broadcast Receiver ) but sometime its stop working.
Issue which I have found without implementing Firebase
If we set background service to run after every 5 mins than for 1-2 hours its
running on 5 mins interval but after 2 hours time gap increased and service runs on
15 mins of interval and in some devices its run on 20-30 mins of interval(i found
this in right time because phone may goes in ideal mode).
As mention in your question, sometimes any of above implementation doesn't work.
Tip : What I found in system alarm log ?
I have set alarm for 2019-08-08
11:07:00 which not fired and current time is 2019-08-08 11:10:00 but its showing in
pending in system alarm log.
You can check system all Alarm set by your applications
D:\SDK\platform-tools>adb shell dumpsys alarm
How can I create a service which will push a notification at a specific time everyday? I've been trying many method on google but they did not work well. Anyone know a good way to do this? Someone say its better to use AlarmManager, while some others say its should be JobScheduler because AlarmManager is deprecated.
Anyone have a good reference?
If you want something to be executed on exact time then you should use AlarmManager.
Based on the documentation:
Standard AlarmManager alarms (including setExact() and setWindow())
are deferred to the next maintenance window.
If you need to set alarms that fire while in Doze, use setAndAllowWhileIdle() or setExactAndAllowWhileIdle().
Alarms set with setAlarmClock() continue to fire normally — the system exits Doze shortly before those alarms fire.
You cannot trigger something at specific time using JobScheduler. The execution of job is under OS control. The jobs will be deferred during doze mode hence trigger at exact time won't be possible.
Use JobScheduler when you want to ensure that a Job must be triggered within specific interval however the execution at exact time is not crucial.
Set up a backend server for Push notification . When Api hit request to FCM for push request with device id then device will get notification. For Automatic push from backend , use cron from backend .A background process run in backend api and will hit FCM in some time interval and device will receive PUSH Notification.
I read stuff about alarm manager on android studio and it seems to have a problem if the user intends to kill the app by means of swiping it on their respective task manager and also it is being destroyed if the phone is been rebooted. how am i going to retain the alarm then even if the app is destroyed? is there any other way how am i able to achieve this?
I'd recommend phasing out AlarmManager for JobScheduler, which has the ability to set it as a persistant alarm across reboots. It you're stuck supporting a version of Android without JobScheduler, you have to do it yourself by adding a BOOT_COMPLETE broadcast receiver and rescheduling the alarm on reboot.
I am working on an Android project that has a part which dose this:
1) the user enter a data from a data field and save it in a text file
2) the app should send notification even if the application is killed by the os, at that date, the one wrote by the user.
For example:
I write 31.01.2015
The app will notify my only on 31.01.2015 even if i don't open that app anymore.
The question is how do i have to do this?
Thanks!
It sounds like you want a notification to be posted to the notification bar.
If so I advise using an alarm.
However, chances are if this is days in the future, the phone may be shut off. So you should store when the alarm should go off, create a Broadcast receiver for the on boot complete event (this requires a permission), and re-setup the alarm when the boot is complete.
This should allow the notification to appear, independent of the apps life-cycle, as long as the app is not uninstalled.
Note: You will have to calculate the milliseconds between the date for the alarm, and the current time. Calendar should help.