Android 使用铃声振动手机,直到铃声停止

Android 使用铃声振动手机,直到铃声停止,android,Android,在我的应用程序中,我正在播放一个服务的铃声 protected boolean playRingtone() { Uri ringtoneUri = _savedValues.loadRingtoneUri(); _audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); _maxVolume = _audioManager.getStreamMaxVolume(AudioMa

在我的应用程序中,我正在播放一个服务的铃声

protected boolean playRingtone() {      
    Uri ringtoneUri = _savedValues.loadRingtoneUri();
    _audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    _maxVolume = _audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    _oldVolume = _audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    _audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, _maxVolume,AudioManager.FLAG_SHOW_UI);




    _mp = new MediaPlayer();
     try {
         _mp.setDataSource(this, ringtoneUri);
    } catch (Exception e) {
        return false;
    }

    _mp.setVolume(_maxVolume, _maxVolume);
    _mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
    _mp.setLooping(true);
     try {
         _mp.prepare();
    } catch (Exception e) {
        return false;
    }

    _mp.start();         
     return true;
}
当服务停止时

public void Stop() {
    //cancel ringtone playback if it's going on
    if (_ringAloud) {
        if (_audioManager != null) {
            _audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,_oldVolume, AudioManager.FLAG_SHOW_UI);
        }
        if (_mp != null) {
            if (_mp.isPlaying()) {
                _mp.stop();
            }
            _mp.release();
        }
    }
现在,我想让它也震动,直到服务停止。 但除了振动(时间)和振动(模式,重复)之外,可控震源类不提供任何方法。
所以,有没有一种方法可以使手机与铃声同步振动。

您可以使用此方法启动和停止振动

public static void onClickStartVibrate(Context c)
    {

        //Set the pattern for vibration  
        long pattern[]={0,200,100,300,400};

        //Start the vibration
        vibrator = (Vibrator)c.getSystemService(Context.VIBRATOR_SERVICE);
        //start vibration with repeated count, use -1 if you don't want to repeat the vibration
        vibrator.vibrate(pattern, 0);   

    }
停止振动

public static void onClickStopVibrate()
    {
        try{
            vibrator.cancel();
        }
        catch(NullPointerException e)
        {}
    }

您可以使用此方法启动和停止振动

public static void onClickStartVibrate(Context c)
    {

        //Set the pattern for vibration  
        long pattern[]={0,200,100,300,400};

        //Start the vibration
        vibrator = (Vibrator)c.getSystemService(Context.VIBRATOR_SERVICE);
        //start vibration with repeated count, use -1 if you don't want to repeat the vibration
        vibrator.vibrate(pattern, 0);   

    }
停止振动

public static void onClickStopVibrate()
    {
        try{
            vibrator.cancel();
        }
        catch(NullPointerException e)
        {}
    }