How to pass an object with an Intent - android

I have an alarm(repeatingalarm) and BroadcastReceiver to handle it. the alarm is set in my activity with pending intent referring to BroadcastReceiver class. I have a sqlite database in my activity. i want to update my sqlite database in my alarm so i need sqlite object from my activity. it is needed for my asynctask too which is executed in my alarm too. how can i do that?
in short terms, i want to pass my activity object to my broadcast constructor so i can use my sqlite instance.
EDIT: I noticed that if i define BroadcastReciever inside my activity, i can refer to it as simple as MyActivity.this. is there any other way rather than this.

you can not do too much work in 'broadcastreceiver' .
Intend you must create one service class to do this and call service from 'broadcastreceiver' . and then write your DataBase code in Service .
In Service you can create 'sqlite' DataBase object through getApplicationConext().
Also within Service must create Seperate Thread for your work or use AsyncTask to do your DataBase Operations.

Related

Is it good create notification inside service or main activity

I eveyone,
I have this doubt, I am creating a mediaplayer and I can create the notification with buttons in both, if I create in the MainActivity it will be more simple because many variable is over there, but if I create inside service I need call onStartCommand() many times and also need pass data through of intents.Then my Question is: what is the best option?

Can a service access a variable within an Activity?

I have an Activity called MainActivity that starts a Service called MainService. It is also binds the Service, so MainActivity can access methods and public variables within MainService. Is it possible to do it the other way round, i.e. the Service is able to access the Activity's methods?
I wish to implement it this way because I have a variable in MainActivity that is set upon onResume(), and on first startup the service has not yet started by the time onResume() runs, so at that point in time the service is still null.
This answer assumes that the Service in question runs in a different process:
Yes, it is possible. The general idea is that not only your Activity binds the remote Service through some AIDL defined interface, but it also implements additional AIDL interface which the Service is aware of, and sets itself as a callback target of the remote Service.
You'll have to have 2 AIDL files: the first one describes the interface of the Service, and the second one describes the interface of the Activity.
The implementation of such a scheme is very similar to "remote Service callbacks" described in this answer, though "callback" method would no longer be void, but return the value you're interested in.
Design considerations:
The above scheme will allow you to get values from Activity, but I don't think you should take this path. From the description of your use case, it looks that you only want to pass some value to the Service when Activity gets resumed. Since your Service is bound anyway, you can simply add a method setSomeValue(int value) to its AIDL definition and call this method from onServiceConnected() callback.
Yes it's possible.
You have to prepare method in your service to return back your activity just after service is bound:
public void bindActivity(MyActivity activity){...}
Then after service is bound to activity just call this method with MyActivity.this as parameter.
However...
You probably should not do it. Much more clear solution is using LocalBroadcastManager to pass events and data or use some more efficient solutions like Otto to do this same, but still - without direct access to one component's fields / methods from another.

How to Access the Instance of the Service from an Activity Other Than the Activity Starting the Service

I have a service that is being started in Activity B using:
private Intent streamPlayerServiceIntent;
.
.
.
streamPlayerServiceIntent = new Intent(getSherlockActivity(), StreamPlayerService.class);
streamPlayerServiceIntent.putExtra("url", result);
getSherlockActivity().startService(streamPlayerServiceIntent);
and I would like to access the service instance from Activity A. How can I access the service, which method would be the most suitable way? The service plays a stream from an URL.
I don't want access the service by declaring the intent as static or convert service to be a singleton since I intent to create another instance of the service later.
What you need to do is bind to the service. It's a little long to explain here (and I would probably end up pasting the manual).
In short, when you bind to the service you provide a callback connection object. This connection object (probably inside your activity) gets notified (similarly to a control's onclickListener) when the binding has been successful and you can retrieve a pointer to the service's instance. Once with a pointer to the service in your hands you can do whatever you want.
Check the documentation, there's a very good example.

Getting a reference to data collected by a Service from an Activity for visualization purposes

Hello fellow developers,
I am trying to achieve something in Android and I would like some advise on the best practice.
I have created an Activity which can start and stop a Service which collects data.
Instead of simply starting and stopping the Service, the Activity should also display the data collected by the Service.
Here in lies the problem. The data could be quite large so I would like to avoid Serializing and sending it via an Intent. Is it possible to simply get a reference to the data stored in the Service from the Activity?
Simple Example
1) Activity starts
2) Activity starts Service to collect data
3) Activity exists
4) 24 hours pass
5) Activity starts
6) Activity wants to display data collected by Service, but data is quite large.
My question again is simply this. Can the Activity get a reference to the data stored in Service or does the data have to be Serialized and sent from the Service to the Activity using an Intent?
Kind regards,
Cathal
Research android.app.Application class. Essentially you can use this for global state.
http://developer.android.com/reference/android/app/Application.html
Also, any long term data should be stored to a local database. I would try to limit the SELECT to certain number of records to reduce memory footprint.
I've heard that the Application class should not be used in this way necessarily (via Google Team). Instead you could make a separate Singleton class (For example call it "MyData") that holds a public static reference to a let's say ArrayList (For example called "list_data").
So when he activity starts it will call
MyData.list_data = new ArrayList<MyDataObj>();
Then in your Service you can add all of your data objects to this static arraylist like so:
MyData.list_data.add(new MyDataObj());
This allows you to work off of the same ArrayList without passing it around using Intents and etc. Although I would suggest doing some null checking whenevr

Getting data from the Service to the Activity

In my application I have a service in which rss feeds are read. After the service finishes the reading I need to update my application and populate a list view with the downloaded information. Using a broadcast receiver, and intents would not work, because the data is stored in a custom object. What is the best way to do that?
Using a broadcast receiver, and intents would not work, because the data is stored in a custom object.
Then don't store the data in "a custom object".
What is the best way to do that?
Step #1: Use a database.
Step #2: Alert the activity that there is new data in the database, via a broadcast Intent, or via a Messenger passed into the service, or via a PendingIntent created by createPendingResult() passed into the service, or via a ContentProvider facade and a content observer, etc.
Step #3: Have the activity load the data out of the database, perhaps simply by calling requery() on an outstanding Cursor.
1) The simplest way to get Custom class data in Intent is, Get the data to primitive types then put this data by putString or whatever data type, then add a constructor to corresponding sent data to activity from set in custom object in activity.
2) Other way of doing this is extend your custom class by parcelable and override corresponding methods then you will be able to send parcelable class object with intent.
go to this link How to send an object from one Android Activity to another using Intents? for details

Categories

Resources