I am writing an Android app, API Level 10.
I have a main activity that starts a service and puts a parcel containing a class object as an extra for the service intent. Events that the service handles updates the information stored in the class object. How can I access the new class object from the main activity? Is there a way to send an intent or message to the main activity?
Thanks in advance. I have been kinda vague so if you need clarification please let me know.
Yes, Android provides Handlers to do what you need to do. Please read this documentation http://developer.android.com/reference/android/os/Handler.html
Keep in mind though, that a Handler will only be able to handle String messages so what you can do is have a copy of the class object in your main activity and then keep modifying that object using the messages received by the handler (do conditional checking for each type of message and modify the object accordingly). Does this make sense?
Related
I want to design an IntentSerive class in my Android app that receives an array that has objects of MyObject class from another app. The class MyObject is defined in both apps.
I just want to know the steps to make it happen...for example, I know that my service needs to define an intent-filter to communicate with the other app.
Is it even possible? does MyObject class have to be Parcelable?(I don't even know what it is, I read something about it...)
Thanks
Yes, It's a common task.
The main purpose of Intents is the communication between independent applications. You are not limited to intents for interaction between your own activity and the service within single application. Android has a special "binding" technique for direct communication between an activity and a background service.
There is an excellent brief tutorial on this topic.
I think you can serialize your object to String (by JSON) then pass it as an extra of Intent. Then your app read the String extra and deserialize it.
I have a class that extends service and the service basically fetches data from the cloud and lists ot in a listview..am getting an error when i try to use "findviewById" method to get the listview because the class doesn't extend Activity.does anyone know how i should go about it.
Your service cannot modify the UI directly. In fact, there may not be a UI at all, as the user may have pressed BACK and destroyed the activity while the network I/O is going on.
Instead, you need to send a message from the service to the activity to let the activity know, if it exists, that there is new data. For this, you can use:
LocalBroadcastManager from the Android Support package, or
a third-party message bus implementation, like Square's Otto, or
a Messenger tied to a Handler
etc.
Hi There in my opinion better to use the AsncTask in that you have to write the backend downloading data part in doinBackground method and setting the data to the list view in onPostexcute.
Better have a look into the asynctask
http://developer.android.com/reference/android/os/AsyncTask.html
mean while try to bind a service and make communication from service to activity for this have a look into how to bind a service.
for binding a service
http://developer.android.com/guide/components/bound-services.html
I'm messing around with one of the Android Samples Bluetooth chat, im relatively new to Android so I'm here to get users opinions, In the main activity class and instance of the class "BluetoothChatService mChatService" is created this does all the controlling of the bluetooth connectivity,
Now I have created a new Activity that launches a page of buttons, these buttons will send certain hardcoded messages depending on which one is pressed, aseen as "mChatService" has been initiated and is handling the connection I would like to make this class instance available in my newly created Activity so I can send messages straight away,
What is the best practices to make this available?, I have read about Serializing the class (which wont work in this instance) so I can pass it in with the Intent, and also singletons?
Can anyone advice what way this SHOULD be done?
Thanks!
If you can't make your class Serializable you could use Parcelable and do intent.putExtra() when sending and intent.getExtras().getParelable() on the receiving end.
Passing a reference to a Bluetooth class might get tricky. You would probably be better handling the message sending back in the original activity.
So I'm writing this app that will basically send a bluetooth message to another device when I receive an SMS. To send the bluetooth message, I use the Bluetooth sample chat app found here: main source file. In terms of getting the SMS, I follow this example.
Basically in order to receive an SMS I register a BroadcastReceiver.
For the Bluetooth chat, there is a private BluetoothChatService which is initialized and set up when the app starts. That object is used in the function that writes through Bluetooth.
Now the problem is that I'm not too sure how to access either the object or the write method. Also I'm pretty sure I explained my set up right, so please double check the links I posted just to make sure I've explained it right.
To call a main activity method from another class you need a reference to this activity in that class, or you need a handler (callback). You can either go with the Handler defined in main activity, and when you want to invoke some code, send a message to this handler (like the Bluetooth sample you provide works).
I need to share data between a local service and its hosting activity,
I am using sendBroadcast in order to send data to the hosting activity from the service, but what if I wanted to send data back to the service from the hosting activity?
So I though of creating a static method on the service, and call it from the activity, and through it send the parameter, but then I can't do operations like show a toast inside that static method (which is inside the service)...
This is because you can't use myclass.this inside a static method, I guess there are more limitations...
Maybe there is another solution? Maybe there is a proper way for this task?
Thanks,
moshik.
You could create a sharedpreferences file and store the data in there. That way both activity and service will be able to access and update the same data
I'm not too sure on your use cases, however another option (aside from using intents / shared preferences) is to extend the Application class and put your object onto that.
The benefit of this is that you don't need to worry about intents, but it may not be the best option for your scenarios.
I wrote a tutorial here, give that a try
*disclaimer : this is just one way to solve an issue, not THE way..