简体   繁体   中英

PreferenceActivity and BroadcastReceiver - Implement dynamic preferences

I'm going by the following code example to dynamically build a preference activity.

http://www.linuxtopia.org/online_books/android/devguide/guide/samples/ApiDemos/src/com/example/android/apis/app/PreferencesFromCode.html

The preference dialog shows, but I'm not able to see any changes after closing it.

Here's where I'm defining the activity in AndroidManifest.xml

    <activity 
          android:name="PreferencesActivity" android:label="@string/preferences_name">
    </activity>

Here's where I'm defining the receiver.

    <receiver
            android:name="FroyVisualReceiver"
            android:label="@string/app_name"
            android:exported="false">
            <intent-filter>
                <action android:name="com.starlon.froyvisuals.PREFS_UPDATE"/>
            </intent-filter>
    </receiver>

And here's the BroadcastReceiver. I never see the "WTF" in logcat. What am I doing wrong?

package com.starlon.froyvisuals;

import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.Context;
import android.util.Log;

public class FroyVisualsReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        Log.e("WTFWTF", "WTFWTFWTFW");
        String action = intent.getAction();
        if(action.equals("com.starlon.froyvisuals.PREFS_UPDATE"))
        {
            ((FroyVisuals)context).updatePrefs();
        }
    }
}

Oh here's onPause where I'm broadcasting the PREFS_UPDATE intent.I do see the logcat message. This method is part of my PreferenceActivity.

    /** another activity comes over this activity */
    @Override
    public void onPause()
    {
        Log.i(TAG, "onPause ================================ ");
        super.onPause();
        Intent i = new Intent(this, FroyVisualsReceiver.class);
        i.setAction("com.starlon.froyvisuals.PREFS_UPDATE");
        sendBroadcast(i);
    }

Edit: I think it may have to do with this line. 'this' points to my PreferenceActivity.

Intent i = new Intent(this, FroyVisualsReceiver.class);

Try a simple Intent :

 Intent i = new Intent();
 i.setAction("com.starlon.froyvisuals.PREFS_UPDATE");
 sendBroadcast(i);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM