How to pass data to an activity started by a service

I have to develop an application that gives a popup message at a certain time which is set by the user.

I will use the android AlarmManager, which starts a service, and that service will start the activity containing the popup interface with an OK button.

I want to pass data to that activity, how can this be done (otherwise i will have to query the database)?

  • How to pass data from the activity to the service while setting the starting time using AlarmManager?
  • How to later pass that data to the activity that is started by the service?

Also if a service starts an activity in my application (not the main activity), and database handler functions are in a separate class, is the database accessible?

-------------Problems Reply------------

When you pass a PendingIntent object to the AlarmManager.set method, you can put any extras you want in the Intent object wrapped in the pending intent. For example:

Intent intent = new Intent(getApplicationContext(), YourService.class);
intent.putExtra("someKey", someData);
intent.putExtra("anotherKey", anotherData);
PendingIntent pi = PendingIntent.getService(getApplicationContext(), REQUEST_CODE, intent, FLAGS);

And then, as the service starts you receive the same intent object:

public void onStartCommand(Intent data, int startId, int flags) {
int data1 = data.getExtra("someKey");

And you can do the same when starting the activity from your service:

Intent activityIntent = new Intent(getApplicationContext(), YourActivity.class);
intent.putExtra("key", data1);

In fact, if you don't do anything special in your service but launching the activity, why won't you just set an intent for the activity in your alarm?

Category:android Views:0 Time:2011-11-04

Related post

  • Passing data from an Activity to its View 2011-05-12

    This is a newbie question. What is the best way to pass an object from an Activity to its View? I know the View has access to its context. Are Intents still the way to go? Or is there something else? EDIT: Did not know the details about the data matt

  • Android: How to pass data of one activity with the button click in another activity 2011-07-06

    I have two classes like and In firstactivity I have a button(submit) when I click the button(submit) I want to pass the data of to server. How can I do this? Thanks in advance. --------------

  • Pass data from one activity to another, BUT go to the other activity later 2012-04-09

    My work flow is as follows: LoginActivity -> ActivityB -> ActivityC -> ActivityD I'd like to pass data from LoginActivity to ActivityD, but not go directly to ActivityD. ie, I'd like to pass data from LoginActivity to ActivityD, but go to Ac

  • Passing data from New activity to old activity? 2010-04-22

    I am trying to receive data from new Activity to old Activity. In other words, newer Activity must send intend data to old activity when it finishes. I've use setResult(resultcode, NewActivity.this.getIntend); on newer activity and @Override protecte

  • How to pass data to the activity that is a member of a TabHost 2011-03-11

    My application contains a tabhost, added a tab with by passing an Intent object as content. And it is working fine, but when i pass data to this intent object using putExtra() method, i'm getting the following exceptions: ERROR/AndroidRuntime(1078):

  • Passing data from one activity to another in Android 2011-03-31

    Possible Duplicate: How to pass a value from one Activity to another in Android? I have an activity with a list of titles and their bodies(content) (list6 example from ApiDemos). And I have an activity, where I add a note. When I click on "Add" butto

  • Passing data from tabhost activity to its inner activity 2011-05-20

    I have a tab activity that every of my applications tabs are opening the same activity lets say SecondActivity.So the code is as shown below. TabSpec firstTab = tabHost.newTabSpec("tid1"); firstTab.setIndicator("First Tab Name").setContent(new Intent

  • Pass data from one activity to another 2011-07-11

    This question already has an answer here: How to pass a value from one Activity to another in Android? 4 answers In Activity1, I input some data like name and address. When I click the next button, there will be another input form. What I want to do

  • android - pass data from one activity to a list in a second activity 2011-11-03

    new to droid programming. im having a small problem that im sure is simply fixed but ive done some searching and a bunch of tutorials but cant seem to find just what i need so i figured id ask. My app has 2 activites, the first activity is just a sim

  • How to pass data from one activity to another in tab layout in android 2012-03-12

    I am using a tab layout in which i have four part.first part showing the list of all visitors and second part showing the list of operators and third part is chat part and fourth part is controlling window.the image is given below ![the image given b

  • pass data to new activity from adapter class 2013-05-10

    I made one custom listview with image and text.When u click on image it ll lead you to new activity.Now i want to pass image and text to new activity.i know with putExtra method you can pass data but i don't know how can i use it here.. thanks in adv

  • Android: Pass data to new Activity 2011-04-17

    I'm new to android programming, and I'm pretty sure this is what I need. I've got a page that loads on start that has a spinner, some edit text boxes, and two buttons. One button clears out the boxes and resets the spinner. The other button should lo

  • error while passing data from one activity to other and displaying in a tablelayout 2011-08-24

    hi i am passing the latitude,longitude and address using a bundle to another activity. in another activity i am retrieving data from the bundle. but when i click the button on which bundle is used the app crashes. i cant post the logcat as nothing co

  • Pass data from the Activity to SurfaceView 2011-06-05

    I'm not sure where the problem is. I thought if the activity would pass information to the surfaceview it implements. Basically I'm trying to make it so that when someone selects their choice of game layout from the main menu (passes it with Intent)

  • Need to pass data from one activity to another referencing database 2011-08-31

    In my application I want to save some string data into an array. This data should then be fetched by the another activity in a ListView. I am using a database to store the data. But every time I'm storing data in different tables. Now as per my knowl

  • how to pass data from one activity to another activity with th use of Application class 2011-12-28

    import; public class MyData extends Application{ private String name; private String phone; public String getName() { return name; } public void setName(String name) { = name; } public String getPhone() { return phon

  • passing data from an activity to BROADCAST receiver 2012-04-07

    i want to pass some data from a edittextpreference to my broadcastreciever that is recieving SMS.... how can i do that...the following code represents what i have been doing but that does not works.... public class AppPreferences extends PreferenceAc

  • passing data from one activity to another 2011-11-11

    I have an activity which have a method in it. In this method I write Intent intent=new Intent(); intent.putExtra("geoLat", geoLat); // latitude intent.putExtra("geoLng", geoLng); // longitude There is another activity which have the following line of

  • Passing Data from Delphi Client to / from Web Service 2011-11-13

    If I create a web service in Delphi XE as CGI executable, what's the best way for it to return data to a client? Say the server uses ADO to access SQL server and the data is stored in a TADODataSet component - what's the best way to get it back to th

Copyright (C), All Rights Reserved.

processed in 0.070 (s). 11 q(s)