I have a Service that is started from a pendingIntent.
This service gives either a 1 or 2 int (there's a notification that contains a button, that once pressed returns 1 then 2 over and over.) Need to transfer this int to my mainActivity to then say (in my mainActivity)- if (ServiceClass.getNum ==1) then mToggle.setChecked(true) else (false).
I heard a bunch of different suggestions on how to do this. Some of those suggestions include Binding to the activity, using a Local Broadcast receiver? Also my mToggle button needs to still be able to change states even if the Activity is in the background or closed.
Any suggestions on the best course of action is appreciated.
For communicating between Android activity and service we can use broadcast receiver.
you can see this https://developer.android.com/guide/components/broadcasts
Related
I want to send the user from application 1 to application 2 by pressing a button. In application 2, the user performs an action (for example, writes text), then he clicks the button and the text is sent to application 1.
I know that there is a certain "Getting a result from an activity", but I could not figure out how it works. I also found examples on YouTube where similar actions are performed, but inside one application. How can this be done using different applications?
You can launch an Activity using startActivityForResult(). The Activity that you launch does not necessarily need to be in the same application. When that Activity calls setResult() and then finish(), the information that was passed in setResult() will be returned to your original Activity in the method onActivityResult().
I'm sure that you can find some tutorials about how to use startActivityForResult().
I'm developing a simple Application. It has 3 buttons (start record, stop record, send info to the server). I want to record my voyage on the map, and I implemented it, but it works only when Activity is onResume(), so now I implementing sticky service, which will do exactly the same, but in the background. So service will fill ArrayList<MapPoint>, and then I have to receive it back somehow to my activity. It is not necessary to pass ArrayList to Service, but I have to receive it back to send it to the server in onDestroy() method. Please, help.
I thinks you can put the value in shared preferences and then pull it back in the activity
I have been poking around to figure out how to do this with Service or Activity, or Broadcast Receiver and I can't seem to get a definitive answer.
I want to be able to write the following data event(s) with timestamp to a file with an application that is running in the background.
Power button pressed on
Call being sent outgoing
SMS message being sent outgoing
It is an application that I am attempting to build for logging phone usage with the timestamp written to a file.
Went through many explanations and this is the only one that seemed to make sense:
https://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/
Any information or tutorials on this would be greatly appreciated.
I would do the following:
Create a broadcast receiver that based on the intent received determines the type of event. Have a look to the Command design pattern.
The command launches a new intent using the Broadcast receiver as a Context.
Create a service that is able to handle this intent, so it starts, sends some data or stores it wherever, and shutsdown. Have a look to the IntentService class.
Create an activity to show the content you have stored or to explain the user what your app does.
So, I'm both new to Java and to creating android apps, but not new to programming. I've read through most of the developer.android.com site, but I haven't been able to find this:
I want to make sure that a certain activity isn't running more than once at the same time. So we have a task somewhat like this:
Activity A) a TabActivity, which launches
Activity B) a ListView that, on-click, opens up
Activity C) which is the interface for a mediaplayer object
Right now, whenever somebody presses the back-button whilst in C (Which is a likely thing, because they're going to listen to a streaming 1-hour long mp3) and then presses another list item, instead of returning to C, C is opened a second time, and two streams are playing. Of course, I'd want only one instance of C running, and I want a second click on the list item to bring C back to the front. This could also be useful for notification intents.
I've been messing around with the flags (especially FLAG_ACTIVITY_NEW_TASK, FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_REORDER_TO_FRONT), but no success so far.
If someone could help me out here I could focus on my next challenge - making it a real feed reader :P
Thanks in advance,
You need to flag your actvity as either "singleTask" or "singleInstance" in your manifest. I can't remember the exact differences between the 2, but either should do what you want. SingleInstance just does something different with the stack.
Example :
<activity android:name="MainActivity" android:launchMode="singleInstance"></activity>
You can handle new calls to startActivity() from the same activity instance with onNewIntent()
I've got it!
For those reading this question and wanting to know the summary: I mistakenly thought more then one activity was running, but it appeared more MediaPlayer instances where running. I made my mediaplayer a class member and am now controlling it from the onStart() event.
I am using SharedPreferences to check if the stream needs to reset and change source, or continue running and just show the interface.
Thanks for all your reactions. Really helped me out.
Just Edit the package names in .xml
EX: com.org.MainActivity
change to
.org.MainActivity
It works for me.....yup
This has probably been asked before, but I can't find a good way of implementing it. I'm trying to write a program that manages a form of messages, and these messages are received from an external data source. This all works. However, the problem comes when I try to notify the user: I would like to have the notification jump directly to the message when it is touched, but this seems to mess up the back stack. This is probably best explained by example:
I open up the message list, the main activity, and browse for a while.
I hit home and go into another app (let's say Music).
A new message is received. A notification comes up, which I touch. The message detail view is displayed.
Now I hit Back. What I want to have happen is that I return to Music, but unfortunately, Back sends me to the message list, and then hitting Back will return me to music.
The both the list and the detail activities are marked as "singleTop", and the exact flags that I use for the notification Intent are:
FLAG_ACTIVITY_NEW_TASK
FLAG_ACTIVITY_CLEAR_TOP
FLAG_ACTIVITY_SINGLE_TOP
I figure if the Messaging application can do this, why can't I?
I have found one way to do this, but it's still not ideal:
Change the detail activity to have a different task affinity from everything else.
Add android:launchMode="singleTop" and android:excludeFromRecents="true" to the detail activity's manifest entry.
(Optional) Modify the list activity to open the detail activity with FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET. (This makes the task more like the built-in messaging app.)
The only fault to this scheme is that switching back over to the app will always go back to the list activity, but at least it is consistent. If someone else has a better way to do this, I'd love to hear it.