My app should react with vibration, when a number sends a text message. The vibration should go on until the user touches a button. While testing the application, I'm sending multiple text messages from different devices, including the one with the checked number. I always manage to send texts, that will just stop the vibration, although the logging shows, that the vibration should have started, but it didn't continues. It could happen after 2, or after 40 messages, randomly.
I was thinking, that the device's notification's vibration could somehow interfere with my vibration, accessing the Vibrator
at the same time.
Or since this is the first thing I'm doing with android, I'm doing something wrong.
Here is my AlarmActivity.java
class, which handles the vibration, it's always called from the MainActivity
via Intent, when a new message is received:
public class AlarmActivity extends WearableActivity {
static boolean active = false;
static boolean isVibrating = false;
private Vibrator vibrator;
final long[] mVibratePattern = new long[]{0, 400, 800, 600, 800, 800, 800, 1000};
final int[] mAmplitudes = new int[]{0, 255, 0, 255, 0, 255, 0, 255};
private PowerManager.WakeLock wakeLock;
@Override
public void onResume() {
super.onResume();
active = true;
}
@Override
public void onStart() {
super.onStart();
active = true;
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP|PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
"MyWakelockTag");
if ((wakeLock != null) && // we have a WakeLock
(!wakeLock.isHeld())) { // but we don't hold it
wakeLock.acquire();
}
vibrate();
}
@Override
public void onStop() {
super.onStop();
active = false;
if (wakeLock != null && wakeLock.isHeld()) {
wakeLock.release();
}
}
@Override
public void onPause() {
super.onPause();
active = false;
if (isVibrating) {
vibrate();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm);
findViewById(R.id.stop_button).getBackground().setLevel(5000);
final Button stopButton = findViewById(R.id.stop_button);
stopButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
vibrator.cancel();
isVibrating = false;
if (wakeLock != null && wakeLock.isHeld()) {
wakeLock.release();
}
if (isOnline()) {
new SendEmail().execute();
}
Intent myIntent = new Intent(AlarmActivity.this, MainActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
AlarmActivity.this.startActivity(myIntent);
}
});
setAmbientEnabled();
}
public boolean isOnline() {
ConnectivityManager cm =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
return netInfo != null && netInfo.isConnectedOrConnecting();
}
public void vibrate() {
if (vibrator == null) {
System.out.println("--------- CREATING VIBRATOR ------------");
vibrator = (Vibrator) getApplicationContext().getSystemService(VIBRATOR_SERVICE);
}
vibrator.vibrate(VibrationEffect.createWaveform(mVibratePattern, mAmplitudes, 0)); // 0 to repeat endlessly.
isVibrating = true;
}
}
I have also tried to cancel()
the vibrator right before calling vibrate()
again, but that stopped the vibration immediately after sending a second text from the checked number without pushing the stop button.
How can I simplify this, or make sure it work and vibrates until the user pushes the button, even if I receive 100 messages between the two?