5

I am developing an Android Wear OS 2.0 application. Every time a user gets an SMS from a given number, the watch should start vibrating, and a UI with a given text should appear, with a button, which stops the vibration. It works in the following way:

In the SmsReciever.java I'm checking if the phone number is matching, or the UI screen is already active.

public class SmsReceiver extends BroadcastReceiver {

    //interface
    private static SmsListener mListener;

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle data  = intent.getExtras();

        Object[] pdus = (Object[]) data.get("pdus");

        for(int i=0;i<pdus.length;i++){
            SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdus[i], "3gpp");

            String sender = smsMessage.getDisplayOriginatingAddress();

            String alertPhoneNumber = "301112233";
            if (sender.toLowerCase().contains(alertPhoneNumber.toLowerCase()))
            {

                String messageBody = smsMessage.getMessageBody();

                //Pass the message text to interface
                mListener.messageReceived(messageBody);

            } else if (AlarmActivity.active) {
                Intent intent1 = new Intent();
                intent1.setClassName("hu.asd.watchtest", "hu.asd.watchtest.AlarmActivity");
                intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent1);
            }
        }
    }

    public static void bindListener(SmsListener listener) {
        mListener = listener;
    }
}

I have needed the else if part, because sometimes when the UI was active, and I've received a new message, the vibration stopped. So that part starts the AlarmActivity (which handles the vibrating).

In the MainActivity.java I'm binding a new listener, so now every time I get the right message in the SmsReciever, the AlarmActivity should run:

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

    requestPermissions(Manifest.permission.RECEIVE_SMS, PERMISSIONS_REQUEST_RECEIVE_SMS);

    SmsReceiver.bindListener(new SmsListener() {

        @Override
        public void messageReceived(String messageText) {
            Intent myIntent = new Intent(MainActivity.this, AlarmActivity.class);
            MainActivity.this.startActivity(myIntent);
        }
    });
}

In the AlarmActivity.java the application wakes the screen up, then gets the Vibrator, sets the onClickListeners to the stop vibrating button, and then starts the actual vibration. I also change the active state here:

public class AlarmActivity extends WearableActivity {

    static boolean active = false;

    @Override
    public void onResume() {
        super.onResume();
        active = true;
    }

    @Override
    public void onStart() {
        super.onStart();
        active = true;
    }

    @Override
    public void onStop() {
        super.onStop();
        active = false;
    }

    @Override
    public void onPause() {
        super.onPause();
        active = false;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_alarm);
        PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
        final PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP|PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
            "MyWakelockTag");
        wakeLock.acquire();

        findViewById(R.id.stop_button).getBackground().setLevel(5000);

        final Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        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};

        final Button stopButton = findViewById(R.id.stop_button);
        stopButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                vibrator.cancel();
                wakeLock.release();

                new SendEmail().execute();

                Intent myIntent = new Intent(AlarmActivity.this, MainActivity.class);
                AlarmActivity.this.startActivity(myIntent);
            }
        });

        setAmbientEnabled();
        vibrator.vibrate(VibrationEffect.createWaveform(mVibratePattern, mAmplitudes, 0)); // 0 to repeat endlessly.
    }
}

If the user presses the Button, then the MainActivity starts again. The problem is, that out of 50 cases, I can send multiple text messages, the watch will vibrate until I press the stop button, but in 1 case, it'll only vibrate once, then it will stop vibrating. It usually happens, if the AlarmActivity is active already and vibrating, or vibration was cancelled before. I'm guessing that I do way to much work with intents, or something with the Vibrator instances? Or when I get a new text, the watch requires the Vibrator, and my Application can't get it?

This is my first Android application, and I tried a lot of different implementations, but still not perfect.

Paraskevas Ntsounos
  • 1,755
  • 2
  • 18
  • 34
adamb
  • 793
  • 1
  • 11
  • 20

0 Answers0