0

How can I automatically run this method of an Activity while the App is in the background? The method contains a pop up dialog box which asks for user input before it sends data to an email.

public String TemperatureCatch()
    {
        Spinner reeferchoice = (Spinner)findViewById(R.id.optionselecti);
            String reeferChoicei = reeferchoice.getSelectedItem().toString();
            if (reeferChoicei.equals("Yes")) {
                final ToneGenerator tg = new ToneGenerator(AudioManager.STREAM_NOTIFICATION, 500);
                tg.startTone(ToneGenerator.TONE_CDMA_ABBR_ALERT);
                AlertDialog.Builder alert = new AlertDialog.Builder(this);
                alert.setTitle("Temperature");
                alert.setMessage("Input Temperature in F° (-20 to 65) ");
                final EditText input = new EditText(this);
                input.setInputType(InputType.TYPE_CLASS_PHONE);
                alert.setView(input);
                alert.setPositiveButton("Check-In", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        temperaturei = input.getText().toString();
                        Toast.makeText(getBaseContext(), "Updated", Toast.LENGTH_SHORT).show();
                        Updater(temperaturei);
                    }
                });
                alert.show();
            } else if (reeferChoicei.equals("No")) {
                temperaturei = "DRY";
                Updater(temperaturei);
            }
        return temperaturei;
    }

2 Answers2

2
  • Create a Service to use a Timer to send an Intent.
  • Let the intent contain some data so that the App knows it is supposed to run that method.

http://developer.android.com/reference/android/app/Service.html

http://developer.android.com/reference/java/util/Timer.html

http://developer.android.com/reference/android/content/Intent.html

Use putExtra to alert your app on what is happening.

You should also check out this previous question How to set a timer in android.

Community
  • 1
  • 1
Captain Giraffe
  • 14,407
  • 6
  • 39
  • 67
  • After reading the docs I see what you mean. Could you please provide a bare-bones example? just so i know what i'm doing and if im on the right track – Adam Praiswater Aug 18 '14 at 13:53
  • Not really, it quickly becomes a lot to cover. Check out the answers in the previous thread. It seems this is really a duplicate. – Captain Giraffe Aug 18 '14 at 13:56
0

Create a service, it runs in background and you have to hide it's name so it is not visible to others.

http://developer.android.com/reference/android/app/Service.html

Aditya
  • 371
  • 3
  • 8