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.
Related
i'm trying to call a method in my main activity via a button of a widget.
the widget has different buttons with different values. i want the method to do its job without showing up the gui attached to the main activity. (send http request with button value)
so far i searched for some tutorials but i didn't quite understand them, my code got all clumsy and it barely worked. i think a mix of intents, services and broadcasts are needed to realize this?! i really don't know, can somebody post a understandable description or tutorial which covers all aspects of how to do this?
i think there is no code or pictures to provide because there is nothing look at right now.
thanks in advance for any answer.
Android AppWidgets don't need to have any relation at all to an Activity, although it is certainly possible to have a click or something start an activity.
As you probably know, the primary interface to AppWidgets is through your AppWidgetProvider (if you don't know what any of this is about, take a look at the official android guide).
You can tell a button to use a PendingIntent to do one of several things when clicked:
To start a service (probably your best option for time consuming tasks like http requests), using PendingIntent.getService.
To start an activity, use PendingIntent.getActivity.
To broadcast a message to a BroadcastReceiver, create it using PendingIntent.getBroadcast.
In all of these cases, you would tell the button to run the PendingIntent with the
setOnClickPendingIntent(int id, PendingIntent operation) method of RemoteViews.
The Intent you pass to any of these methods should typically be created using the Intent(Context, Class) constructor, where the second argument is the service, activity or receiver class you want the pending intent to be sent to.
I am really stuck. After test lots of different approaches. I'm asking this question.
I'm trying to make an app which should alert the users at the specific date and time, like lots of other apps that you have definitely seen before. I'm using BroadcastReceiver as it should. I register it in manifest to activate it the first time the app gets installed and after restarting the phone but the problem is in android 3.1- taskkillers can kill this receiver so I thought it would be better to activate it again each time the app gets opened but The problem is that I don't know how many instance I'm creating so the first question is:
How to get the active receiver?
so I can make a decision upon it. If it is not active so I can active it again.
What I'm doing within onReceive is: getting data from database and comparing the time and date to now. If the app is not open I want to notify the user in notification area and pass extra data to the app' which is working pretty well. But if the app is open I do not want to notify the user in notification area, instead I want to pass data to an activity and alert the user in my app. I made my activity singleTask and used startActivity to pass data to the activity also I used onNewIntent method to handle new data but the problem is what if the user is using another activity. The second and third questions are:
How to know if my app is open? (I used ActivityManager.getRunningTasks but I realized it is not a good solution because it is an api for Task Manger apps.)
how to pass data from receiver to my activity? (I used interface to pass data from fragment to activity so I thought it can be used here but it does not work here)
After a lot of exertion -Reading and Trying- I know i'm still doing wrong So please guide me.
I suggest that you start a ServiceIntent from the Broadcast reciever. and from there you can send an intent that you can catch in the activity
Okay no one answered my question but I got it myself so here is my own answer:
Every receiver has only one instance and you can just enable or disable it. So to ensure that it is working you can enable it in your activity every time it gets run.
Within onReceive instead of doing stuff there, start a service and in your activity bind the activity with this service so in onBind and onUnbind method you can change a Boolean value and make decision upon it. In onBind make the value True which means your app is open and vice versa in onUnBind.
And finally the best way to pass data from service to activity is using Interface.
Now my app is working just like I want. . Hope to be helpful and save someone's time.
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.
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.