向AlertDialog.Builder添加振动

时间:2015-05-28 21:27:09

标签: android

我正在从服务类启动Dialog窗口,并试图将振动附加到我的AlertDialog.Build窗口,但是当前状态下应用程序一直在保持振动状态。当窗口出现时,我希望它振动一次。我怎样才能做到这一点?

我感谢任何帮助。

private void stop_popup(final ArrayList<Integer> routeList) {

    int routeListSize = routeList.size();
    flag = false;

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            flag = true;
        }
    }, 2 * 1000 * 60);

    if (routeListSize > 0) {

        String[] charSequence = new String[routeList.size()];
        for (int i = 0; i < routeList.size(); i++) {
            charSequence[i] = String.valueOf(routeList.get(i));
        }
        vibration();

        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setTitle("Has this route arrived the stop? ");

        builder.setMultiChoiceItems(charSequence, null,
                new DialogInterface.OnMultiChoiceClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which,
                            boolean isChecked) {
                        if (isChecked) {
                            route_number = routeList.get(which);

                        }

                    }
                });

        builder.setPositiveButton(android.R.string.ok,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {

                    }
                });
        builder.setNegativeButton(android.R.string.cancel,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {

                    }
                });

        final AlertDialog alert = builder.create();
        // To launch the dialog window from the TrackingService class.
        alert.getWindow().setType(
                WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        alert.show();

        if(alert!=null && alert.isShowing() ){
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    alert.dismiss();
                }
            }, 1 * 1000 * 60);
        }
    }
}

public Vibrator vibration() {

    Vibrator v = (Vibrator) getSystemService(VIBRATOR_SERVICE);

    long[] pattern = { 0, 3000, 3000 };

    v.vibrate(pattern, 0);
    return v;

}

2 个答案:

答案 0 :(得分:3)

如果希望它停止振动,您应该致电v.cancel()

答案 1 :(得分:0)

要使用一次模式实现振动,您应该这样做:

v.vibrate(pattern, -1);

repeat参数指示重复开始的模式数组的索引。因此,当您使用0时,您实际上要求它永远振动。

当然,您可以随时v.cancel(),但是如果没有v.cancel(),您就可以解决问题。