Android Wear OS应用程序振动有时不起作用

时间:2018-05-27 16:31:32

标签: android wear-os android-vibration android-sms

我正在开发Android Wear OS 2.0应用程序。每当用户从给定号码获得SMS时,手表应该开始振动,并且应该出现具有给定文本的UI,其具有停止振动的按钮。它的工作方式如下:

SmsReciever.java我正在检查电话号码是否匹配,或者UI屏幕是否已激活。

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;
    }
}

我需要其他部分,因为有时当用户界面处于活动状态时,我已收到新消息,振动停止了。因此该部分启动AlarmActivity(处理振动)。

MainActivity.java我绑定一个新的侦听器,所以现在每次我在SmsReciever中收到正确的消息时,AlarmActivity都应该运行:

@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);
        }
    });
}

AlarmActivity.java应用程序唤醒屏幕,然后获取Vibrator,将onClickListeners设置为停止振动按钮,然后启动实际振动。我也在这里更改了活动状态:

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.
    }
}

如果用户按下按钮,则MainActivity将再次启动。问题是,在50个案例中,我可以发送多条短信,手表会振动,直到我按下停止按钮,但在1个案例中,它只会振动一次,然后它会停止振动。如果AlarmActivity已经激活并振动,或者之前已取消振动,通常会发生这种情况。我猜测我在使用意图或与Vibrator实例有关的东西上做了很多工作?或者当我得到一个新文本时,手表需要振动器,我的应用程序无法获得它?

这是我的第一个Android应用程序,我尝试了很多不同的实现,但仍然不完美。

0 个答案:

没有答案