Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android BroadcastReceiver无法在启动应用程序之前获取SharedReferences_Android_Broadcastreceiver_Sharedpreferences - Fatal编程技术网

Android BroadcastReceiver无法在启动应用程序之前获取SharedReferences

Android BroadcastReceiver无法在启动应用程序之前获取SharedReferences,android,broadcastreceiver,sharedpreferences,Android,Broadcastreceiver,Sharedpreferences,因此,基本上在我正在编写的这个应用程序中,我有一个设置活动,可以让用户保存设置。其中一个设置是,当设备连接到电源时,用户是否希望应用程序启动。我使用以下代码将首选项保存在我的设置活动中: public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { Log.d("DEBUG", "Preference Changed-Settings"); if (key.equ

因此,基本上在我正在编写的这个应用程序中,我有一个设置活动,可以让用户保存设置。其中一个设置是,当设备连接到电源时,用户是否希望应用程序启动。我使用以下代码将首选项保存在我的设置活动中:

public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) 
{
    Log.d("DEBUG", "Preference Changed-Settings");
    if (key.equals("pref_onCharge")) {

        // Set summary to be the user-description for the selected value
        scanCharge = sharedPreferences.getBoolean(key, true);
    }else if (key.equals("pref_onAlways")) {

        // Set summary to be the user-description for the selected value
        scanAlways = sharedPreferences.getBoolean(key, false);
    }
}

@Override
public void finish()
{
    super.finish();
    SharedPreferences pref = getSharedPreferences("MySettings", Context.MODE_MULTI_PROCESS);
    SharedPreferences.Editor prefedit = pref.edit();
    prefedit.remove("onCharge");
    prefedit.remove("onAlways");
    prefedit.putBoolean("onCharge", scanCharge);
    prefedit.putBoolean("onAlways", scanAlways);
    prefedit.commit();
    Log.d("DEBUG", "Preference Saved-Settings");

}
因此,在我的BroadcastReceiver中,我检查SharedReferences,查看onCharge是否在开始活动之前设置为true

public class MyBroadcastReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent)
    {
        if(intent.getAction().equals(Intent.ACTION_POWER_CONNECTED))
        {

            if(context.getSharedPreferences("MySettings", Context.MODE_MULTI_PROCESS).getBoolean("onCharge", true))
            {
                Log.d("DEBUG","onCharge - Receiver");
                Toast.makeText(context,  "Connected to Power" , Toast.LENGTH_LONG).show();
                Intent i = new Intent(context, MainActivity.class);
                i.putExtra(context.getString(R.string.intent_source), context.getString(R.string.power_source));
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(i);
            }
        }
    }       
}
这是舱单:

        <activity
            android:name="com.hpconcept.miracastconnector.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
                <!--  <data android:scheme="pluggedin" /> -->
            </intent-filter>
        </activity>
        <activity
            android:name="com.hpconcept.miracastconnector.SettingsActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.SETTINGS" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <receiver android:name="MyBroadcastReceiver">
            <intent-filter>
                    <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
                    <category android:name="android.intent.category.HOME" />
            </intent-filter>

        </receiver>
问题是SharedReferences总是返回默认值,不管它实际保存为什么。我知道它正在保存,因为如果我打开和关闭应用程序,就会记住设置,并且可以看到保存设置的日志。我做错了什么

编辑:添加清单代码


谢谢

我注意到,在onSharedPrefsChanged中,您使用的键pref\ux。。。但如果你用其他钥匙,这可能是问题所在吗?也从我这里开始,总是使用静态的最终字符串作为键等等…我现在也看到了这一点。但在我的整个项目中,我找不到任何我命名为pref_u2;的地方。pref_always和pref_charge是返回的唯一密钥的日志输出。这些可以放在哪里?找到了。它们隐藏在设置布局xml中让我们看看这是否解决了问题。解决了问题:我的Settings.xml文件中的复选框与我预期的不同。我更改了复选框键的名称以匹配代码中的名称,现在可以使用了。我觉得自己像个白痴。谢谢