• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to enable a function that keeps running

Hi People,

I'm new to writing app for android. I've got program writing experience in vhdl, gnu make and tcl but not in the java/android language. This is a bit different compared to this others.

I would like to start simple and do the following:
Press a button and a the vibrator of the phone will be triggered in a certain pattern.

I know that if you say: vibrator.vibrate(pattern , 0); that it will repeat the pattern. But I would like to turn on and off the screen in that pattern too for example.

What would be the right way to do this?


Code:
package com.trick-design.simple_prog;

import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.os.Vibrator;
import android.view.View;


public class simple_prog extends Activity {
	private long[] pattern = {100, 100, 300, 100, 900, 1050};
	boolean vibrator_on = true;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    @Override
    protected void onStart() {
        super.onStart();
        findViewById(R.id.vibrate_button).setOnClickListener(vibrate_click_listener);
    }
    
    View.OnClickListener vibrate_click_listener = new View.OnClickListener() {
        public void onClick(View v) {
        	
            Vibrator vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
            vibrator.vibrate(pattern , -1);
       }
    };
    
    @Override
    protected void onStop() {
        super.onStop();
        // The activity is no longer visible (it is now "stopped")
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // The activity is about to be destroyed.
    } 
    
}

Thanks a lot for you help!
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones