I have a app that is started by a received intent (calendar alert).
It may be started multiple times just one after a other (with a few sec between them).
Now i want it to stack the screens from the top down..
So first start of my app gets the view...
if it's fired again while first is still shown..but this one below so when user has handled alert 1 it will show this one..
Thnks..
You may be trying to implement this the wrong way. I would implement a message queue that calls startActivityForResult on the pending intents. If you have more intents to start, add them to the queue. The queue will then show them once the user reacts to the current event.
You might want to take a look at IntentService. I've never personally used it, but it might help with what you're looking for.
IntentService is what you're looking for, it can put Intents into a queue.
My google-fu hasn't been very succesful however on finding a decent example.
Related
Writing a kotlin social media app. I want to do 3 things within the logout sequence:
logoutbutton.setOnClickListener() {
//1. db.logout()
//2. finish()
//3. go to login screen
}
Does the order of events matter here? This is the order I am going with right now, but I don't know where the best place to put finish() would be.
Call finish when you want the current activity to popped off the activity stack. You typically do that when the user has indicated they no longer want to interact with it. Other than that, the order of these things don't really matter.
A trap that has caught me a few times is that finish() is actually asynchronous, so can give the illusion that instructions following it are "safe"... until they take a little longer and the activity gets torn down before they complete.
That can make for very difficult bugs to find.
I suggest for safety, call finish last.
I'm assuming db.logout may take some time to complete and/or may have to run on a background thread so some caution may be needed there. Fortunately Coroutines can help a lot .
hello guys probably my question is pretty basic but since am a noob in android development have to ask it , i want to add an action inside a notification and am almost done with it by using this code
notification.addAction(R.mipmap.ic_launcher, "Decrease", pendingIntent);
but as you can see i added a pendingIntentin parameter and this intent is passing me to a particular activity (what it supposed to do so) but instead of passing the user to an activity i want to perform an action without opening the app how can i do this ?? and my action is for increasing a counterValue like performing Counter++
Pending intent can only used as a intent, without such ability to run only a piece of code.
You can use a dummy activity to be opened on notification click and exits immediately after doing the jobs.
However, using activities can be annoying for the users as it flashes in and out on screen. So you might want to use Services to not interrupt users and do your job simultaneously.
In your case IntentService seems to fit which can be used with ease.
Android document nicely describes a service's lifecycle and methods of it so it's worth a try.
I am new to android development and I did research on notifications using toast and status bar notification.
And I also managed to execute the code properly to make a notification work!!!
The problem is there are only methods like triggering a notification by clicking a button is available. Other wise I managed to directly call the codes within the method that is called by the button, to make it trigger automatically. But the problem is the view of the corresponding screen is showing up a tleast for a sec and then closing while this notification is triggered.
So how can i write a code that just triggers the notification without popping up the screen even for a second.
I need a result like the way the new SMS alert works...And I did a lot of research on this and all I got was about basic notification. So please help as I am new to this!!!
Using a Service would be the "right" way to do it - and if this is a professional app you are writing, then that's the way to go.
Bear in mind you still need some activities in your application, in order to trigger the service.
If you are just experimenting, then maybe what you need is a cheap hack...
Here's the cheap and nasty way to get your proof-of-concept done:
either - create a transparent Activity so that nothing is displayed when the activity code gets called.
or - create your notification from within the Activity.onCreate() method, and then call finish() at the end of that method. Your activity will never get shown to the user.
To me, it looks like you are just experimenting, and a transparent activity might get you further faster... ymmv
Legendary you need service and handler. Using service you can get data. and using handler you can modify the UI of your app.
here you can get more information on it.
http://developer.android.com/training/notify-user/display-progress.html
Working with handlers and threads in service, Thread.sleep makes the program to hang?
I have number of activities in my app. From front end I can start different different activities but I have back end as well, that means from server if i received a message than app has to take action on the message and start the activity based on the message.
My problem is that sometimes app received message from the server and app starts activity and at the same time user also performs click on UI and navigates to other activity. In this case one of my activity is not started as android can't start both activity at a time.
Is there any INTENT LAG which can help to resolve this issue?
Right now what i am doing is if i received message from server than I am using one global flag and using that flag i am avoiding such situation, but I am looking for better solution if anyone has any idea on this.
Your solution sounds fine. There really isn't a good way to do this because Android discourages this behaviour. If your application requires this behaviour you will need to create a workaround, which it sounds like you have done.
Inactivity is a very important EVENT. For many apps if the user does not interact with it for a certain number of seconds its time to reset the app and go back to main activity logout, or conserve power. So I would really like to get some feedback on the best way to detect this. In fact I think everyone will benefit from a good solution to this.
So my question is twofold:
1) Is there a better way to detect user inactivity than using a combination of
activity.onUserInteraction() to reset a CountDownTimer?
Note: One reported downside to this approach is that softkeypad interaction might not be
caught by this approach.
Note: Another reported downside is the CountDownTimer is off main thread and might not update
correctly. I am not sure how big an issue this is?
Note: CountDownTimer appears to have cancellation issues as well:
how to stop/cancel android CountDownTimer
2) Lets say that onUserInteraction()/CountDownTimer is the best/only solution to this problem
there are still some questions:
a) should each activity launch its own countdown timer?
b) Should a single countdown timer be restarted in the onCreate method of each activity?
c) lets say I want to dim the screen or goto main activity when the countdown expires where
should the timeout handler be located? In each activity? In a service?
Thanks
Just stumbled upon this question as I've answered something similar just now.
Personally, I'd opt for option 2 that you have suggested, and put a timer into a singleton so its available across all activities. Theres no need for a separate countdown timer unless you have a specific requirement to react different under different features of your application.
Why would you want to reset the timer in the onCreate? You should do that each time the user interacts with the application, such as in the activity.onUserInteraction() method.
To quote from my previous answer:
You'll need to invest a little thought into exactly what your
requirements are here, but from what I can tell, you want to keep
track of the user interactions and if a time limit expires since the
last interaction, perform some action, in your case logging them out
of your application.
Firstly, you'll need some place that you can track when the last
interaction occured, since you'll want this to be application wide you
could use a singleton to hold this, or override the Application class,
either way should do.
Next, you'll need to start tracking user interactions. From your
activities, you can override the onUserInteraction method, this gets
invoked anytime the user interacts with the application such as key
event. Each time you hit this method, update your singleton and let it
know something has happened, with a timestamp.
Finally, you'll need some kind of looping check to constantly check if
anything has happened recently. Theres various was of doing this, you
could have a continuous loop that compares current timestamp to the
last recorded event, a bit of draft code :
while(true)
{
if (timeLastEventRecorded < (now - 15))
{
//nothing has happened in 15 minutes, so take corrective action
}
}
Presumably you'll already have some code in your application that
takes care of logouts, such as when the user clicks "logout", you
should just be able to invoke that in the sample above.