-2

I'm making a timer inside an app (in Studio), and when the timer reaches 0, it vibrates. I would like to make it so that the phone still vibrates when the screen is locked. Any suggestions?

Here's my main activity:

    EditText timerWrite;
    ImageButton power;
    ImageButton check;
    TextView minsLeft;
    CountDownTimer countDownTimer = null;

    Vibrator vibe;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_screen);
        vibe = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);



    timerWrite = (EditText) findViewById(R.id.timerWrite);
    power = (ImageButton) findViewById(R.id.power);
    check = (ImageButton) findViewById(R.id.check);

    minsLeft = (TextView) findViewById(R.id.minsLeft);


    power.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
            vibe.cancel();
            if(countDownTimer != null)
                countDownTimer.cancel();
            minsLeft.setText("stopped");
        }
        });


    check.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            vibe.cancel();
            if (countDownTimer != null)
                countDownTimer.cancel();
            String text = timerWrite.getText().toString();

                if (!text.equalsIgnoreCase("")) {
                    int mins = Integer.valueOf(text);

                    countDownTimer = new CountDownTimer(mins * 60000, 1000) {

                        @Override
                        public void onTick(long milliseconds) {
                            long minutes = milliseconds / 60000;
                            long seconds = (milliseconds / 1000) % 60;
                            minsLeft.setText(getString(R.string.minutes) + (int) (minutes) + "  seconds:" + (int) (seconds));

                        }

                        @Override
                        public void onFinish() {
                            minsLeft.setText("Are You OK?");
                            long[] pattern = {0, 1000, 2000, 100};
                            vibe.vibrate(pattern, 1);
                        }
                    }.start();
                }
            }

    });
}

Thanks!

  • Here is a sample from Android on Service on Alarm, you could add your logic and vibrate code https://developer.android.com/training/scheduling/alarms.html – alkathirikhalid Feb 21 '17 at 01:55

1 Answers1

1

That would be beyond the Activity life cycle as it would go to onPause() and onStop() hence your code/app wont work, what you are looking for is a Service that will run without these limitation.

https://developer.android.com/guide/components/services.html

Cheers.

alkathirikhalid
  • 887
  • 12
  • 18
  • Oh, okay. I'll mess around with that then. Thanks a bunch! – Molly Taylor Feb 21 '17 at 02:18
  • When I introduce the object "vibe", I call VIBRATOR_SERVICE: vibe = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); – Molly Taylor Feb 21 '17 at 04:03
  • Very unprofessional, just came back from lunch. Anyway you not only lack professionalism but also effort to research, I have pointed you to the right direction, what you are doing is not right. Cheers. – alkathirikhalid Feb 21 '17 at 05:38
  • I'm sorry, I don't know how I was being unprofessional. Also, I did research it for at least an hour, but I am just learning how to program, so I am having trouble understanding the Android API. This is my first app, and I just began learning Java last November. Here are the pages I've reviewed: https://developer.android.com/reference/android/content/Context.html#VIBRATOR_SERVICE, https://developer.android.com/reference/android/app/job/JobScheduler.html, https://developer.android.com/guide/components/bound-services.html, https://developer.android.com/guide/components/services.html – Molly Taylor Feb 21 '17 at 21:52
  • I pointed you to the right direction, even added a sample example provided by Android even though your question is not easily understood or lacks clarity but I took the time and liberty to be polite and straight. You accepted my answer and I got rewarded points only to revoke it and I got penalized that's really messed up :) anyway, feelings aside, since you stared don't rush into those links yet grasp the concept of OOP (1. Free PDF its a brilliant start) http://www.bluej.org/objects-first/ (2. All you need directly from the source) http://docs.oracle.com/javase/tutorial/reallybigindex.html – alkathirikhalid Feb 22 '17 at 02:09
  • For any programming language I will deeply express you always got to the source as most others don't really stay up to date especially some videos you would find in YouTube. To learn Android I will point you to (1. Android Developer) https://developer.android.com/training/index.html (2. Udacity Free Google Courses) https://www.udacity.com/google Happy Learning, Happy Coding, Cheers. – alkathirikhalid Feb 22 '17 at 02:16