Changing android:updatePeriodMillis programmatically - android

OK, so I have done some research and the consensus seems to be that you can't update android:updatePeriodMillis programatically.
It seems that you have to use AlarmManager instead, which seems like using a sledghammer to crack a nut... odd that the API doesn't just let you update the core updatePeriodMillis.
There's always a slight risk with relying on what's out there on the web, since APIs tend to develop and old answers are no longer relevant.
So I'm just checking that this is still the case. For example, the guide at http://developer.android.com/guide/topics/appwidgets/index.html at least hints that it is possible to change the update period of the AppWidgetProvider. When discussing updatePeriodMillis it says:
"You might also allow the user to adjust the frequency in a configuration—some people might want a stock ticker to update every 15 minutes, or maybe only four times a day."
And then goes on to talk about using AlarmManager but apparently only in relation to avoiding waking the device rather than to changing the update period.
Thanks for any help on this.

So I'm just checking that this is still the case.
Yes, it is still the case that you cannot update updatePeriodMillis. Which is too bad, as I'd love to see an updateUpdatePeriodMillis() method. Particularly if this were done via some sort of builder or transaction object, implying that it too might be changed via an updateUpdateUpdatePeriodMillis() method.
:-)
It seems that you have to use AlarmManager instead
You could use JobScheduler on Android 5.0+ as well, though I suspect that you won't like that much either.
You could also allow the user to configure some multiple of updatePeriodMillis, then only do your work every N updates, though this isn't terribly efficient.
Or, you could not update your app widget periodically at all, instead updating it only as needed based on app functionality, rather than based on time. IMHO, this is what most apps should be doing.

Related

NotificationManager getActiveNotifications() for older devices

I want to be able to get active notifications from my Android app on demand. (actually I just need to know if there are any)
I've been searching for this behavior and it seems, like I have only two options: NotificationManager.getActiveNotifications() which is exactly what I need, but is only available from SDK 23 or using a NotificationService but I really dislike this solution as I have to provide the permission to my app to read all notifications which is definitely an overkill.
Does anybody know about any solution which would behave like NotificationManager.getActiveNotifications() and not require SDK >= 23?
Thanks in advance!
You know which notifications you raised, because your code raised them.
In theory, you can know which notifications were removed, by using setDeleteIntent() to register a PendingIntent to find out when the user clears that notification. I have not used this in quite some time, so it is possible that this does not work as well as it used to.
But, if you know which notifications you raised, and you know which notifications were cleared, knowing which notifications are still outstanding is a matter of bookkeeping.
To be honest, "what are the active notifications?" feels like a code smell. There may be very good reasons for asking that question. If we were teammates, and this was part of code review, I would be asking why we care whether any given notification is active or not.
And, at this point, API Level 23 (Android 6.0) is six years old. You really should be reconsidering whether you should be supporting older versions of Android than that, given all the security problems that would remain on Android 5.x and older devices.
In my project, I used a static array to save all pending notifications.
This array will be cleared when the app come onto foreground.
Thanks.

Get notified when an activity or task changes

Is there any way the android system can inform a service that an activity or a task in the system(not only my activities/tasks!) has somehow changed (e.g. another activity is brought to the front or is stopped)? This is to avoid polling the same information, of course.
There is an identical question here, but it was asked 3 years ago, so in the meantime, maybe someone has an answer to this? As a supplement to this question: If the answer is no, is there any way someone could enhance this feature by, let's say, low-level programming or something?
No, there is no general solution. You can only record this information in the lifecycle methods of your own activities. And starting in Android 5, even polling does not work. ActivityManager#getRecentTasks() was nerfed. From the docs:
This method was deprecated in API level 21. As of LOLLIPOP, this method is no longer available to third party applications: the introduction of document-centric recents means it can leak personal information to the caller. For backwards compatibility, it will still return a small subset of its data: at least the caller's own tasks (though see getAppTasks() for the correct supported way to retrieve that information), and possibly some other tasks such as home that are known to not be sensitive.
Since this is considered a security issue, you can bet that if there is a low-level way to do it, Google will eventually find out and break it.
Isn't what you want covered already by the activity lifecycle events?
Aren't onPause() and onResume() sufficient?
i am feeling lazy sorry, try this logic.
check this post. what you would want to achieve here is record all the Tasks running currently; and in a loop you keep checking, you will be notified when a task die by checking the overall running Task, loop through and you will know who died- but this is limited not all versions, and some people will tell you, it is not meant for that.

Possible to cancel an Android notification after a few minutes?

I realize that cutting off a notification after an amount of time is odd and unexpected, but I am creating a Phonegap application for a user study group in which this functionality is necessary. I have tried several methods of achieving this through Phonegap background mode plugins but each has there own quirks and would not work reliably due to Android memory management.
What I am thinking now is that I will modify the Android side of this plugin:
https://github.com/katzer/cordova-plugin-local-notifications
To cancel a notification after three minutes since it has triggered. I have talked to the developer of this plugin and he says it's not possible on his end but I don't mind trying to fix it myself. I don't have a lot of Android experience however, and am not sure if I would be wasting my time in trying. Is there any way this would work?
Edit:
I can go into further detail about what I have tried in the past, however I have arrived at the conclusion that these pursuits are dead ends-- They are all Phonegap plugin oriented and do not address the fundamental problem of Android's memory management system killing my running Javascript components that limit notification duration. Because of this, I figured I would change the Android side of my code.
Unfortunately, I have no experience in Android and have so far found learning Android daunting and the framework complex in my early research. It would be a waste of time to research an involved topic like the Android framework if it turns out that I cannot even do what I need to do. This is why I asked if it was possible or not; it is much easier and quicker for someone to tell me 'no', and obviate the need for me to research, than it is for me to research and understand a topic to the point that I can come to the conclusion myself.
It is very easy, there is a cancel method for notifications:
cancel() : - Cancel a previously shown notification
cancelAll () : - Cancel all previously shown notifications.

Android VPNService - how do I bring up the 'system-managed dialog' from my activity?

According to this tutorial the only way to disconnect the vpn service is to use the 'system-managed dialog'. This can be done manually by dragging down the notification bar and selecting it from there, however this is not a good solution for my problem.
I need to be able to bring this dialog up from within my app when a 'disconnect' button is pressed, however I can't find any documentation on how to do this.
Can anyone help?
Right now the only public and recommended way to disconnect is unfortunately just through the notification. The functions for disconnecting or even bringing up the dialog are otherwise hidden unfortunately.
Of course, that doesn't necessarily mean that they can't be accessed, but that way lies much pain. I haven't tried this in this specific case, but I have done this in the past, way back, with the music player to find out what was playing (for audio scrobbling). So if you absolutely must find a way to do this, no matter how hacky, then you could try this, but keep in mind that 'ere be dragons - this involves accessing a private API that you are not supposed to. That API could change on a whim between any two given builds of android (not just platform versions either), because it is supposed to be private and internal. You would need to make your code very resilient to failure here, and frankly it'll never be anything more than hacky and a PITA.
Right, all that said, see line 171 in this
That's what you want to do. Except you can't see the things you need, right? So you need this AIDL for the service (and possibly a bit of reflection too, I forget - haven't done this in a long time).
This blog post describes something similar, though not in much detail. It's hard to find things talking about, since it is an incredibly discouraged practice.
I think it is fine just to close tun descript and let the Service end. This will effectlivy end the VPN (but the app in question will still hold the permission to open a VPN again). The notification (key symbol) will also go away. If you are trying the VPNService of another app that might be tougher. Perhaps you should ask the author of that app for an API.

android alarms incompability?

Get all alarms saved in the alarm application here I have read there is no common interface to get all alarms in the system, due to different implementations of diverse clock apps.
Is this REALLY true ?! I think it is not possible to have no standard for such a crucial things like alarms ?
Is there no possibility for creating a universal widget that displays 5 next alarms regardles of the clock aplication ?
Is this REALLY true ?!
Yes.
I think it is not possible to have no standard for such a crucial things like alarms ?
If you are aware of any OS ever written that forced alarm clock developers to implement some common API, please point it out. AFAIK, this has never been a requirement since the dawn of the computer.
Is there no possibility for creating a universal widget that displays 5 next alarms regardles of the clock aplication ?
You could threaten all alarm clock application developers at gunpoint to implement your desired API. This may be illegal in some jurisdictions.
Or, you can offer some sort of reward to alarm clock application developers to get them to implement your desired API, such as in the form of cash (directly or indirectly, such as by helping them promote and monetize their apps).
I'm not saying that having such a common API is a bad thing, but developers have the freedom to write what they want. You either need to use carrots, sticks, or both to arrange for common standards, such as an API.

Categories

Resources