Android 振动不使用SharedReferences

Android 振动不使用SharedReferences,android,sharedpreferences,Android,Sharedpreferences,我的应用程序中有一个AlarmActivity,在其中,我希望能够检查preferences.xml文件中键振动的值,然后在键返回true时启动AlarmActivity中的振动模式。我以为我已经走上了正轨,但显然我没有 AlarmActivity.java: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVi

我的应用程序中有一个AlarmActivity,在其中,我希望能够检查preferences.xml文件中键振动的值,然后在键返回true时启动AlarmActivity中的振动模式。我以为我已经走上了正轨,但显然我没有

AlarmActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_alarm);

    final Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

    SharedPreferences prefs = this.getSharedPreferences("com.j5tech.app", Context.MODE_PRIVATE);
    boolean alarmVibrate = prefs.getBoolean("vibrate", false);
    if (alarmVibrate){

        long[] pattern = { 0, 200, 500 };
        vib.vibrate(pattern, 0);

    }else{

    }
...
}
在我的preferences.xml中

<PreferenceScreen  xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">

<CheckBoxPreference
    android:key="vibrate"
    android:title="@string/vibrate_setting_title"
    android:summary="@string/vibrate_setting_summary"
    android:defaultValue="false" />

</PreferenceScreen>

在清单中设置振动权限:

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

我知道我不是在这里抽烟。有了SharedReference,我就不必经历一大堆这样和那样的事情了。它由全班负责。下面是我在AlarmActivity中为使设置正确使用振动方法而做的工作

preferences.xml中有一个UI对象,即CheckBoxPreference:

<CheckBoxPreference
    android:name="@+id/chkbxVibrate"
    android:key="vibrate"
    android:title="@string/vibrate_setting_title"
    android:summary="@string/vibrate_setting_summary"
    android:defaultValue="false" />
然后在AlarmActivity中,我想检查CheckBoxPreference对象的值:

final Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    long[] pattern = { 0, 200, 500 };

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    boolean vibrate = prefs.getBoolean("vibrate", true);
    if (vibrate == true){

        vib.vibrate(pattern, 0);
    }

就这么简单!我希望这对其他人有所帮助。

这是您的全部代码还是您写了其他东西?切换变量AlarmVibration并将其设置为真的部分在哪里?显然,我对所有这些比我最初想象的更困惑。我认为在UI中切换CheckBoxPreference可以设置布尔值。然后在我的AlarmActivity中调用该键的值,布尔值AlarmVimble=prefs.GetBooleanVimble,false;如果我错了,请纠正我。在更改复选框的状态时,您的代码中没有链接将布尔值alarmVibrate切换为true或false。我不能在这里给你模型的答案,因为你不会学习,顺便说一句,SharedReferences是android中存储简单值(如字符串或整数)的简单存储选项之一,但是CheckBoxPreference是UI中的一部分。你能确切地告诉我你想要做什么吗?也许我可以指导你学习一些教程。我只是想根据my preferences.xml中CheckBoxPreference的值,让振动有条件地工作。我想我错过了一些优先活动,这不是问题所在。我可以根据my preferences.xmloh中的CheckBoxPreference让vibrate工作,但不是有条件的,我知道了,但是您正在从com.j5tech.app文件中读取SharedReferences。您是否正在将PreferenceActivity中的首选项保存到该文件中?您必须调用PreferenceManager prefMgr=getPreferenceManager;prefMgr.setSharedPreferencesNamecom.j5tech.app;澄清:PreferenceActivity将把您的首选项保存在默认文件中,该文件内部将保存为com.j5tech.app_preferences.xml之类的内容。但是您是从一个名为com.j5tech.app的文件中读取的,该文件没有您的更改。要么让PreferenceActivity写入另一个文件,要么像这样读取AlarmActivity中的默认首选项:SharedReferences pref=PreferenceManager.GetDefaultSharedReferencesGetContext;
final Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    long[] pattern = { 0, 200, 500 };

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    boolean vibrate = prefs.getBoolean("vibrate", true);
    if (vibrate == true){

        vib.vibrate(pattern, 0);
    }