Calling main activity method from another class - android

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).

Related

Accessing a Bluetooth "ConnectedThread" from various activities

I have created a BluetoothManager much like the one in this example. This object is instantiated in a connection activity, reached from the main acitivty by clicking on a "Connect" button, which provides a ListView of selectable devices. Works great so far.
I am now connected and have a BluetoothManager.ConnectedThread running and the streams set up. I would now like to be able to send Bluetooth data from/to various other activities when they are running. For example, I will want to chart realtime values when the charting activity is running.
As far as I can tell, the pushing of the data out from the ConnectedThread will occur via a Handler, which is a new topic for me. What I am unclear on his how other activities might access the ConnectedThread's write() function.
First of all, even though a singleton could be a solution, android Service's are there for this purpose, since these are elements that can keep running when your UI is out. So my suggestion would be to create a sticky service an then you have two options:
Handle data using a handler between the activity and the Service. Maybe if you are not too familiar with the Handler api this will take some time to you. In this example of the official documentation you can also check how to use the handler.
Create a bound service, to which you can bind from the activities and send some data when required. Here you have the official information about bound services.
You can have a look to this tutorial to get more information about handlers.

Best approach to open a chat activity after friendlist activity at chat app

Let's say I'm making a chat application.
I have an activity FriendListActivity (that shows the list of friends)
this activity also has a Socket that listens to every message that comes from the server.
I also have a ChatActivity that's called whenever I click at a friend within FriendListActivity.
My questions about it:
what happens to FriendListActivity attributes when I call ChatActivity from it? Do they die?
How should I place those activities somehow they work together and the atributes don't die?
I assume that by 'called whenever I click at a friend within FriendListActivity' you mean that you will create an intent and start the new activity, ChatActivity. If this is what you plan on doing, then the answer to your first question is no - FriendListActivity attributes (and any state information) is maintained in that Activity's class (FriendListActivity). What happens is that ChatActivity now becomes the 'top-level' activity and is presented to the user, while the FriendListActivity will run in the background.
See http://developer.android.com/training/basics/firstapp/starting-activity.html for a good description of how to start new activities and communicate information between them.
Generally, for chat applications, you will create a Thread that handles incoming messages (usually by listening on a socket associated with a server). This thread will block on a socket read operation (the exact function name/API will depend on your particular implementation) and wait for the server to send it data. When data is received on the socket, your thread will put that data into some kind of queue (like an 'unread messages' queue) or call a function in the main application to handle the new message. Keep in mind that the UI can only be updated by your application's main thread, so you'll need a way to exchange information from your Socket to the UI handler in order to say, update a TextView object to display the message to the user.
For the Android-way to communicate information between threads, check out https://developer.android.com/training/multiple-threads/communicate-ui.html.
If you have any particular examples/code to share that you're having trouble with, update your question and we can try and provide more specific help.

Perform code when the Android application is closed

I'm making an application that detects incoming text messages.
When a message is received, the application must perform a certain action, depending on the sender and the content.
The problem is that the application must work at all times.
Detecting messages works through a BroadcastReceiver class. Via a Toast message I can see that the application (open or closed) receives the message.
The problem then is that I must perform certain actions, which are stored in a local database (DB40). But I can't access the database when the application is closed.
So, how can I perform database access and run other code (like making the phone vibrate, or play a ringtone) when the application is closed?
Thanks in advance
You can access the DataBase... because you are getting the context from the receiver, from the context create the instance of the Database and go further according to your requirement..
Check this...
You need to run a service to perform all the task even app is closed.
Just go through with few sample and study:
http://developer.android.com/guide/components/services.html
Do you know about Services?
You can use service as back-end to perform action on incoming messages or can use database.
Database need only 'Context' object for read/write and service provide 'Context' object by 'getApplicationContext()' method.
If you have any ambiguity about answer, just give me your little code, i will give you back with solution.

How to get updated information from a Service?

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?

Using a Handler in multiple Activities

I am developing an application which has around 8 Activities, and a class which is used to connect/receive data to/from an embedded Bluetooth chip. When I started, a Bluetooth object was initialized in my initial Activity, where there was a Handler which received messages from the Bluetooth object.
After poking around on the internet for a while, it seems like the best idea for me is to turn my class into an Application subclass. However, doing this removes the need for me to initialize an object in the MainMenu, which removes my ability to pass it the Handler used.
Does anyone know of a way to eliminate the need for a Handler, so that every time the Bluetooth Application changes it state or receives data, the current Activity can access it?
My main problem with this approach is that the Activity doesn't know when the Bluetooth Application will be sending it messages, the Application waits and listens, and then notifies the Activity when it happens.
OR
Is it bad practice for me to write the Handler into the MainMenu, have it handle messages for ALL the different activities, and then pass the Handler from Activity to Activity?
I'm going to assume that you're trying to achieve the following as it's a little unclear from your question your ultimate aim (sorry!):
Your application has several activities but only one Activity receives the data from the bluetooth device.
The other activities in in your application require the data from the bluetooth device but are not receiving it directly from the bluetooth device. Currently you're providing the data via the one activity mentioned above.
You want to NOT use a Handler to achieve this.
If my above assumptions are correct then you are going along the correct lines but you probably do not want to use a Handler.
You are quite correct in having one Activity handle all the interactions with the Bluetooth device. It simplifies things and provides a much better, cleaner way of handling the Bluetooth device. However you need to get the data from this one Activity to all the others and to achieve this you would probably want to use Broadcasts, BroadcastReceivers and Intents. See here for an overview.
However if you can you might want to take a look at using LocalBroadcastManager as this keeps any broadcasts within your own app's space. Broadcasts are global and should be avoided if you do not need to pass the data outside of your own app due to security implications.
Finally, have you considered using Fragments for your other Activities? Another disadvantage with Broadcasts is there is extra overhead associated with them. If you're keeping data within your app then you can create an interface to be implemented by each of your Fragments and your main activity just calls that interface on the Fragment that is currently selected.
You can use BroadcastReceiver class to send broadcast messages to your activities. see here http://developer.android.com/reference/android/content/BroadcastReceiver.html
When you get the data you need into the application class, you can send it to the activity you want.. just make sure that the activity has registered to receive that broadcast message..

Categories

Resources