0

Hello,

I've done some research, but to no avail. Also my previous question didn't work as planned.

So here is exactly what I want to do:

1.) On my MainActivity, the User enters a Numerical value in an EditText.

2.) When they press an "OK" Button, it starts a Service to run a Timer for how long they entered.

3.) When the the Timer time runs out, the Service completes some additional Actions.

Questions:

1.) Can I simply use the Button to start the Service via an Intent, then in the Service put the code?

1b.) Can't figure out exactly how to get the User input from the EditText, from within Service.

2.) Can I run the "additional Actions" when the Timer runs out from WITHIN the Service?

2b.) Or, when the Timer is up, do I need to fire a BroadcastReceiver to complete the Actions?

3.) Do I need to add anything special such as Intent-Filters in my Manifest?

Basically, I'm having trouble figuring out what code goes where.

Currently I'm using a combination of Alarm Manager, PendingIntent, and Intent.

Any help would be greatly appreciated, as I've been trying to do this correctly for nearly a month.

I may be somewhat new to programming Android, but I'm eager to learn, so thank you in advance!

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}


public void timerButtonClick(View view) {


    EditText timerValue = (EditText) findViewById(R.id.timerValue);
    assert timerValue != null;
    int timerInt = Integer.parseInt(timerValue.getText().toString());


    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);


    Intent timerIntent = new Intent(this, TimerService.class);

    PendingIntent timerPendingIntent = PendingIntent
            .getService(this.getApplicationContext(), 234324243, timerIntent, 0);

    alarmManager.set(AlarmManager.RTC,
            System.currentTimeMillis() + (timerInt * 1000), timerPendingIntent);


    finish();


}


}



NOTE: Keep in mind, I'm trying to put the Timer INSIDE my Service, NOT in my Main Activity.. So I assume much of this Code needs to be moved to my Service. But again, not sure exactly how to do this.

Thanks!

StudioB
  • 117
  • 9

1 Answers1

0

1.) Yes you can (see here).

1b.) No you can not. Service has no UI interactions at all, but you can create an activity for "after actions". (Edit: But You can pass EditText data from MainActivity to Service using Intent Extras) (see starting activity from service.)

2.) You can do almost anything or "additional Actions" when the Timer runs out from within the Service, without UI. (see services tutorial)

2b.) No, you dont need that.

3.) It depends on your timer logic, intent-filters are only needed when you are receiving any broadcasts. If you write timer logic by simply taking difference between time, there is no need, but if you do it with something like ACTION_TICK logic which triggers after each minute, you do need to declare intent filter. (see declare action tick intent filter)

You will also need to declare service in AndroidManifest.xml. If your java class name is MyBackgroundService, simply put this in AndroidManifest.xml:

<service android:name=".MyBackgroundService">
</service>

Do ask me if anything remains unclear.

Update:

I myself am in learning phase and one thing I learned is to look at the official docs first. There is a class CountDownTimer in android, which gives you countdown implementation.

  • use onFinish() of this class to perform whatever action you want to perform.
  • first argument of constructor is how long you want to run countdown, second is how much time you want for tick() event to occur.
  • start countdown by calling .start() method.

Update:

For putting extras and start service:

Intent serviceIntent = new Intent(this, MyBackgroundService.class);   
String time = editText.getText().toString();
serviceIntent.putExtra("TIME_INTERVAL", time);

startService(serviceIntent);

No need for pending intent. You can start with this:

  • Create an activity that has : EditText and a Button.
  • Create onClick button listener code, which will first check the numeric input is correct or not (number format, negative value etc.).
  • If the input is valid you create an Intent for Service class, add data to intent using intent.putExtra("key","numericValue")
  • Start service (calling start service method) with intent.
  • In service class receive data from intent, then do calculation in time difference code.
  • In service, start a while loop that will check time difference at each loop, and when desired time is elapsed start an activity that will do your action after.

Please note that service class has a bit different implementation than activity class.

If your minimum time difference is in minutes, you can use action tick logic link above to see how you implement a service that can run your logic code every minute. In your case logic is also given in above links (time difference). Also I have provided how you retrieve data from intents.

Please try to dig into some code, then comeback here if you get stuck, I (and we the SO community) will surely help, if you are still stuck, I can only write whole codes on weekends, so you will have to try yourself until then.

Community
  • 1
  • 1
Talha
  • 903
  • 8
  • 31
  • Thanks for the info.. I still have a few questions though. If you dont mind, what are the EXACT codes I would use to put the Extras from the user's input into the Intent (in the MainActivity), and also the code i would use within the Service to retrieve it? Furthermore, I'm assuming this means I DONT need to use a PendingIntent whatsoever? Keep in mind, I am simply taking the user's input, and starting the Alarm in the background, and when that alarm is up, perform an action.. In a moment, I will post what I have so far, up in my main Question.. – StudioB Aug 18 '16 at 10:20
  • Accept as answer if it solved your problem, ask if something else is confusing. – Talha Nov 28 '16 at 04:24