1

在我的应用程序中,我正在播放来自服务的铃声。

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

现在,我希望它也能振动,直到服务停止。但是 Vibrator 类不提供除 vibrate(time) 和 vibrate(pattern,repeat) 之外的任何方法。那么,有没有办法让手机与铃声同步振动。

4

1 回答 1

1

您可以使用此方法开始和停止振动。

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)
        {}
    }
于 2013-09-16T09:31:46.847 回答