Note that there are some explanatory texts on larger screens.

plurals
  1. POConfused by Android Services
    primarykey
    data
    text
    <p>I've read the documentation on Services thoroughly, but I'm still completely confused as to how to use/code a service.</p> <p>What I'm trying to accomplish: My main activity: The user selects all options for a timer and then clicks a button to start up a timer (in a service) with the click. I'm trying to pass the options to the service with putExtra.</p> <p>The service will collect the variables into new variables for the service's use.</p> <p>I have an onCreate section that calls my public counter which I've declared inside of the service, but not in the onCreate section. I've also declared all my variables that need to be enumerated from options being passed from the activity here. </p> <p>Do I need to bind to the service to truely pass the variables with putExtra?</p> <p>I have an onStart which is where I'm trying to enumerate the variable values by doing a series of gets. This is triggered by the button mentioned earlier in the activity. Inside of the activity - Ex:</p> <pre><code>mSet.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { //if toggled on if (mEnableDisable.isChecked()){ getCurrentTime(); //start passing our variables to the service Intent passvars = new Intent(getBaseContext(),TimerService.class); passvars.putExtra("mHour24", mHour24); //start the service now startService(new Intent(TimerService.class.getName())); } } }); </code></pre> <p>Inside of the service - Ex: </p> <pre><code>public void onStart(Intent intent, int startId) { super.onStart(intent, startId); //Collecting data from Activity for calcs Bundle getvars = intent.getExtras(); mHour24 = getvars.getInt("mHour24"); //start the counter now that I have my values countDown.start(); </code></pre> <p>}</p> <p>Also if I can get the service to start without crashing I need to pass a value back to the activity. So I imagine I am doing that with putExtra in the service and getExtra in the activity. Once again, will I need to bind to the service to obtain the new values?</p> <p>To bind if I need to I would do this on the button press. I would also add in a section to the main activity if the program exits, but then resumes to re-bind and collecting that value. The value I'm trying to pull is the time remaining from the counter. So inside of the counter service I would have an onTick put the remaining time into a variable and do a putExtra of that variable, where in the activity I want to collect that value and display it in the UI.</p> <p>This is my first service attempt and I'm just not sure how to create and use properly. Thanks in advance for all the help and sorry for such a long convoluted post.</p> <p><strong>UPDATE:</strong> So I have the service working now. I'm just having trouble getting information back from the service to the main activity.</p> <p>I have added a function to try and retrieve the data from the service which is launched after the button click launches the service:</p> <pre><code>startService(passvars); //Init. variable for getDisplayInf() function to update the display with counter strings Running = 1; getDisplayInf(); </code></pre> <p>And the getDisplayInf() ex:</p> <pre><code>public void getDisplayInf() { //Intent stIntent = new Intent(MainActivity.this,MainActivity.class); //Intent passvars = new Intent(MainActivity.this,Service.class); Bundle getvars = getIntent().getExtras(); String Remaining; String CountDown; do { Remaining = getvars.getString("Remaining"); CountDown = getvars.getString("CountDown"); mRemaining.setText(Remaining); mCountDown.setText(CountDown); Running = getvars.getInt("Running"); } while (Running == 1); }; </code></pre> <p>The timer will set the Running variable to 0 on finish. As soon as I click the button with this new function in place is crashes the app.</p> <p>In the service I'm doing this on the counter's onTick:</p> <pre><code> @Override public void onTick(long millisUntilFinished) { Running = 1; Remaining = "Time Remaining: "; CountDown = formatTime(millisUntilFinished); ticker(); } </code></pre> <p>ticker's code:</p> <pre><code>public void ticker () { Intent passvars = new Intent(Service.this,MainActivity.class); //this is for the activity to keep looping to grab the countdown values while running this timer //now send it to the activity passvars.putExtra("Running", Running); //String Values to be passed to the activity //now send it to the activity passvars.putExtra("mRemaining", Remaining); //now send it to the activity passvars.putExtra("mCountDown", CountDown); }; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload