1

I read a lot about this problem on Stackoverflow but not of the Helps her works at my problem. My problem is that my app always stops on the phone. I wanna have a switch button and if its turned on, it should vibrate indefinitely every 10 seconds.

package com.example.myapp;

import android.os.Bundle;
import android.os.Vibrator;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Switch;

public class MainActivity extends Activity {

    private Switch mySwitch;
    public // Get instance of Vibrator from current Context
     Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

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

        mySwitch = (Switch) findViewById(R.id.myswitch);

        //set the switch to off
        mySwitch.setChecked(false);
        //attach a listener to check for changes in state
        mySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {

               @Override
               public void onCheckedChanged(CompoundButton buttonView,
                 boolean isChecked) {

                 if(isChecked){
                    // Start without a delay
                     // Vibrate for 100 milliseconds
                     // Sleep for 1000 milliseconds
                     long[] pattern = {0, 100, 1000};

                     // The '0' here means to repeat indefinitely
                     // '-1' would play the vibration once
                     v.vibrate(pattern, 0);
                 }else{
                     v.cancel();
                 }
               }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

Thank for your help!

xxjaylxx
  • 79
  • 1
  • 8

3 Answers3

1

Your error is at the following line: public // Get instance of Vibrator from current Context Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

The system services are not available to your Activity before the method onCreate() is called. So you have to initialize your vibrator in onCreate() method.

GEM
  • 58
  • 5
0

You could start a Thread where you would put something like this in it:

while (true) {
  v.vibrate(500);  // Half a second
  sleep(10000);    // Wait 10 seconds
}

But as this will run indefinitely, you'll need also to have some kind of condition or event in your Thread to stop it under certain circumstances (for example, if an action happens, or if a BroadcastReceiver signal triggers. More info here.

More info on stopping a Thread here.

Community
  • 1
  • 1
nKn
  • 13,691
  • 9
  • 45
  • 62
  • The problem with your suggestion is that I stuck in the while loop. I can not use the switch anymore. I need to have a method in the while loop that check the switch in every run. – xxjaylxx Jan 19 '14 at 18:52
  • As far as you declare that `Thread` within the `Activity` you're using, you'll be able to reach the `Check` to see its state everytime you want. That would probably need to decrease the sleep time, though - or separate the `Check` tasks within a different `Thread`. – nKn Jan 19 '14 at 19:06
0

My suggestion is

  1. U need not use the UI thread(Activity) which you are using currently but background service.
  2. Using Alarm Manager you can put a reccuring alarm every 10 second.

it should work with screen off.

App Permission:

<uses-permission android:name="android.permission.VIBRATE"/>
Abs
  • 3,902
  • 1
  • 31
  • 30
  • Thanks for your answer. I already added a permission. I'm not sure what you mean, I'm at the very beginning with Android programming. – xxjaylxx Jan 16 '14 at 17:27
  • 1
    I read a very nice introduction on stack about how everything in Android is either main UI thread or a Background Thread . http://stackoverflow.com/questions/3875184/cant-create-handler-inside-thread-that-has-not-called-looper-prepare read mjosh answer so in think in your case you need not block you main app as you want to do something that is not UI related. Hope it helps. – Abs Jan 16 '14 at 17:33