多次调用Android Vibrator振动功能会停止振动

时间:2018-05-30 18:16:44

标签: android android-vibration android-wear-2.0

当数字发送短信时,我的应用会对振动作出反应。振动应该持续到用户触摸按钮。在测试应用程序时,我会从不同的设备发送多条短信,包括带有选中号码的设备。我总是设法发送文本,这将停止振动,虽然记录显示,振动应该已经开始,但它没有继续。它可能会在2次之后或40次消息之后随机发生。

我在想,设备的通知振动会以某种方式干扰我的振动,同时访问Vibrator

或者因为这是我用android做的第一件事,我做错了。

这是我的AlarmActivity.java类,它处理振动,当收到新消息时,它总是通过Intent从MainActivity调用:

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

在再次呼叫cancel()之前,我还尝试vibrate()振动器,但是在没有按下停止按钮的情况下从被检查的号码发送第二个文本后立即停止振动。 如何在用户按下按钮之前简化此操作或确保其工作和振动,即使我在两者之间收到100条消息?

0 个答案:

没有答案