1

我真的很想讓用戶挑選一個振動以用於我的應用程序,我已經在PreferenceScreen中使用了RingtonePreference,還有什麼類似的Vibrations或者一個類似的庫嗎?有沒有類似於RingtonePreference的VibrationPreference?

編輯: 或者,如果沒有圖書館或現有的類,那麼有沒有一種簡單的方法來擴展一個類來做到這一點? ListPreference看起來很不錯,但沒有「onclick」處理程序來播放所選的振動模式。

+0

您可能希望[檢查了這一點(http://stackoverflow.com/questions/13950338/how-to-make-an-android-device - 振動) – jayeshkv

回答

2

它很容易建立自己的,爲ListPreference的源代碼是在http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/android/preference/ListPreference.java

這裏就是我最終想出了爲我工作

VibrationPreference.java

package com.mypackagename; 

import android.app.AlertDialog.Builder; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.SharedPreferences; 
import android.os.Vibrator; 
import android.preference.ListPreference; 
import android.util.AttributeSet; 

public class VibrationPreference extends ListPreference { 
    private int clickedIndex; 


    // This example will cause the phone to vibrate "SOS" in Morse Code 
    // In Morse Code, "s" = "dot-dot-dot", "o" = "dash-dash-dash" 
    // There are pauses to separate dots/dashes, letters, and words 
    // The following numbers represent millisecond lengths 
    private static final int dot = 150;  // Length of a Morse Code "dot" in milliseconds 
    private static final int dash = 375;  // Length of a Morse Code "dash" in milliseconds 
    private static final int short_gap = 150; // Length of Gap Between dots/dashes 
    private static final int medium_gap = 375; // Length of Gap Between Letters 
    private static final int long_gap = 750; // Length of Gap Between Words 



    private static final long[] sos_pattern = { 
     0, // Start immediately 
     dot, short_gap, dot, short_gap, dot, // s 
     medium_gap, 
     dash, short_gap, dash, short_gap, dash, // o 
     medium_gap, 
     dot, short_gap, dot, short_gap, dot // s 
    }; 

    private static final int beat = 250; 
    private static final int interbeat = 100; 
    private static final int between_beat_pairs = 700; 
    private static final long[] heartbeat_pattern = { 
     0, // Start immediately 
     beat, interbeat, beat, // o 
     between_beat_pairs, 
     beat, interbeat, beat, // o 
    }; 

    private static final long[] jackhammer_pattern = { 
     0, // Start immediately 
     100, 100, 
     100, 100, 
     100, 100, 
     100, 100, 
     100, 100, 

     100, 100, 
     100, 100, 
     100, 100, 
     100, 100, 
     100, 100, 

     100, 100, 
     100, 100, 
     100 

    }; 


    public static final long[][] vibration_patterns = { null, sos_pattern, heartbeat_pattern, jackhammer_pattern}; 



    Vibrator vibrator;  

    public VibrationPreference(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); 
    } 

    public VibrationPreference(Context context) { 
     super(context); 
     vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); 
    } 

    @Override 
    protected void onPrepareDialogBuilder(Builder builder) { 
     super.onPrepareDialogBuilder(builder); 

     if (getEntries() == null || getEntryValues() == null) { 
      throw new IllegalStateException(
        "ListPreference requires an entries array and an entryValues array."); 
     } 

     clickedIndex = findIndexOfValue(getValue()); 
     builder.setSingleChoiceItems(getEntries(), clickedIndex, 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         clickedIndex = which; 
         vibrator.cancel(); 
         if (clickedIndex > 0) vibrator.vibrate(vibration_patterns[clickedIndex], -1); 
        } 
     }); 

     builder.setPositiveButton("OK", this).setNegativeButton("Cancel", this); 
    } 

    @Override 
    protected void onDialogClosed(boolean positiveResult) { 
     vibrator.cancel(); 
     super.onDialogClosed(positiveResult); 

     if (positiveResult && clickedIndex >= 0 && getEntryValues() != null) { 
      String value = getEntryValues()[clickedIndex].toString(); 
      if (callChangeListener(value)) { 
       setValue(value); 
      } 
     } 
    } 
} 

然後在我的首選屏幕xml:

<CheckBoxPreference android:title="Vibrate" 
      android:key="do_vibrate" android:defaultValue="true"></CheckBoxPreference> 



    <com.mypackagename.VibrationPreference 
     android:key="vibration_pattern_index" 
     android:dependency="do_vibrate" 
     android:title="Vibration Pattern" 
     android:defaultValue="0" 
     android:entries="@array/vibration_pattern_entries" 
     android:entryValues="@array/vibration_pattern_values" 
    /> 

,不要忘記在你設置體現:

<uses-permission android:name="android.permission.VIBRATE"/> 

,然後從首選項只是讓你「vibration_pattern_index」,並用它來獲得長[]出VibrationPreference.vibration_patterns的,你會有你的用戶選擇的振動!大聲笑我不知道是否有人會實際使用,甚至讀這:) :)

+1

我讀它,也將使用它:) –

0

如果你想使用android.support.v7.preference.ListPreference而不是android.preference.ListPreference,你需要擴大PreferenceDialogFragmentCompat

VibrationPreferenceDialogFragmentCompat.java:

public class VibrationPreferenceDialogFragmentCompat extends PreferenceDialogFragmentCompat 
{ 

    // This example will cause the phone to vibrate "SOS" in Morse Code 
    // In Morse Code, "s" = "dot-dot-dot", "o" = "dash-dash-dash" 
    // There are pauses to separate dots/dashes, letters, and words 
    // The following numbers represent millisecond lengths 
    private static final int dot = 150;  // Length of a Morse Code "dot" in milliseconds 
    private static final int dash = 375;  // Length of a Morse Code "dash" in milliseconds 
    private static final int short_gap = 150; // Length of Gap Between dots/dashes 
    private static final int medium_gap = 375; // Length of Gap Between Letters 
    private static final int long_gap = 750; // Length of Gap Between Words 
    private static final long[] sos_pattern = { 
     0, // Start immediately 
     dot, short_gap, dot, short_gap, dot, // s 
     medium_gap, 
     dash, short_gap, dash, short_gap, dash, // o 
     medium_gap, 
     dot, short_gap, dot, short_gap, dot // s 
    }; 

    private static final int beat = 250; 
    private static final int interbeat = 100; 
    private static final int between_beat_pairs = 700; 
    private static final long[] heartbeat_pattern = { 
     0, // Start immediately 
     beat, interbeat, beat, // o 
     between_beat_pairs, 
     beat, interbeat, beat, // o 
}; 

    private static final long[] jackhammer_pattern = { 
     0, // Start immediately 
     100, 100, 
     100, 100, 
     100, 100, 
     100, 100, 
     100, 100, 

     100, 100, 
     100, 100, 
     100, 100, 
     100, 100, 
     100, 100, 

     100, 100, 
     100, 100, 
     100 

}; 

    public static final long[][] VIBRATION_PATTERNS = {sos_pattern, heartbeat_pattern, jackhammer_pattern}; 

private int mClickedIndex; 
private Vibrator mVibrator; 
private ListPreference mPreference; 

@Override 
protected View onCreateDialogView(Context context) { 
    mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); 
    return super.onCreateDialogView(context); 
} 


@Override 
protected void onPrepareDialogBuilder(AlertDialog.Builder builder) { 
    super.onPrepareDialogBuilder(builder); 

    mPreference = (ListPreference) getPreference(); 

    if (mPreference.getEntries() == null || mPreference.getEntryValues() == null) { 
     throw new IllegalStateException(
       "ListPreference requires an entries array and an entryValues array."); 
    } 

    mClickedIndex = mPreference.findIndexOfValue(mPreference.getValue()); 
    builder.setSingleChoiceItems(mPreference.getEntries(), mClickedIndex, 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        mClickedIndex = which; 
        mVibrator.cancel(); 
        if (mClickedIndex >= 0) mVibrator.vibrate(VIBRATION_PATTERNS[mClickedIndex], -1); 
       } 
      }); 

    builder.setPositiveButton("OK", this).setNegativeButton("Cancel", this); 
} 

@Override 
public void onDialogClosed(boolean positiveResult) { 
    mVibrator.cancel(); 

    if (positiveResult && mClickedIndex >= 0 && mPreference.getEntryValues() != null) { 
     String value = mPreference.getEntryValues()[mClickedIndex].toString(); 
     if (mPreference.callChangeListener(value)) { 
      mPreference.setValue(value); 
      mPreference.setSummary(getResources().getStringArray(R.array.vibration_pattern_entries)[mClickedIndex]); 
     } 
    } 
} 

}

SettingsFragment.java:

public class SettingsFragment extends PreferenceFragmentCompat { 
... 
    @Override 
    public void onDisplayPreferenceDialog(Preference preference) { 
     DialogFragment dialogFragment = null; 
     if (preference.getKey().equals("pref_vibrate_pattern")) 
     { 
      dialogFragment = new VibrationPreferenceDialogFragmentCompat(); 
      Bundle bundle = new Bundle(1); 
      bundle.putString("key", preference.getKey()); 
      dialogFragment.setArguments(bundle); 
     } 

     if (dialogFragment != null) 
     { 
      dialogFragment.setTargetFragment(this, 0); 
      dialogFragment.show(this.getFragmentManager(), "android.support.v7.preference.PreferenceFragment.DIALOG"); 
     } 
     else 
     { 
      super.onDisplayPreferenceDialog(preference); 
     } 
    } 
... 
} 

的settings.xml:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto"> 
... 
     <ListPreference 
      android:key="pref_vibrate_pattern" 
      android:title="Vibration pattern" 
      android:entries="@array/vibration_pattern_entries" 
      android:entryValues="@array/vibration_pattern_values" 
      android:summary="Select the vibration pattern"/> 
... 
</PreferenceScreen> 

的strings.xml:

<resources> 
    <string-array name="vibration_pattern_entries"> 
     <item>SOS pattern</item> 
     <item>Heartbeat</item> 
     <item>JackHammer</item> 
    </string-array> 

    <string-array name="vibration_pattern_values"> 
     <item>0</item> 
     <item>1</item> 
     <item>2</item> 
    </string-array> 
</resources> 
相關問題