开发者

Run a function over and over again

I'm new to writing app for android. I've got program writing experience in non object oriented program language but would like to learn this way of programming too.

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 until the button is pressed again

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?

Thank you very much for your help.

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);
 开发者_JAVA技巧   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.
    } 

}


As an extension of TVK-'s answer:

You would need to create a class that either implements Runnable or extends Thread to do this work in another thread to prevent the hang. You can do this with a second class in the same *.java file -- allowing it to access the variables from the other class in the file:

private class VibrateRunner implements Runnable
{
    public void run()
    {
       while(vibrate_on)
       {
           executeVibrate();
       }
    }

    private void executeVibrate()
    {
       vibrator.vibrate(pattern , -1);
    }
}

Your on click listener would need to be smart enough to start/stop the thread - ex:

// Make a Thread class variable
Thread bgThread = null;

onClick(View v)
{
    // You already started the thread - stop it
    if(bgThread != null)
    {
       vibrate_on = false;
       bgThread.join();
       return;
    }

    // Need to turn on the thread
    vibrate_on = true;

    Runnable runner = new VibrateRunner();
    bgThread = new BackgroundThread(runner);
    bgThread.setDaemon(true); // Run it in the background
    bgThread.start();
}

That should get you off in the right direction. Note: I cut out a lot of exception handling in here -- just read up some on Java threads to get the feel for it. Eclipse will also help you generate the right try/catch blocks.

-- Dan


Try this:

public void vibrate() {
    vibrator.vibrate(pattern , -1);
    if(vibrator_on) {
        vibrate();
    }
}

This method will repeat itself until vibrate_on is false. (Make sure it runs in its own thread, otherwise it will freeze up whatever thread it's running it for as long as vibrate_on is true.

Update: As discussed in the comments, this is no good. This should do better:

public void doVibrate() {
    while(vibrate_on) {
        executeVibrate();
    }
}

public void executeVibrate() {
    vibrator.vibrate(pattern , -1);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜