android从广播接收器获取首选项

时间:2011-10-21 22:21:08

标签: android android-activity broadcastreceiver preferences

好吧,我是一个Android新手。我正在尝试编写一个应用程序,其中包含一个启动另一个活动的活动(Preference Activity)和一个BoradcastReceiver。它们都有三个不同的文件。我的问题是:如何在这些组件之间共享首选项,例如如何读取广播接收器的首选项活动中设置的首选项?

1 个答案:

答案 0 :(得分:4)

之前我问过同样的问题,这里是我使用的代码:

public class BootupReceiver extends BroadcastReceiver {

private static final boolean BOOTUP_TRUE = true;
private static final String BOOTUP_KEY = "bootup";

@Override
public void onReceive(Context context, Intent intent) {

    if(getBootup(context)) {
        NotificationManager NotifyM = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification Notify = new Notification(R.drawable.n,
                "NSettings Enabled", System.currentTimeMillis());

        Notify.flags |= Notification.FLAG_NO_CLEAR;
        Notify.flags |= Notification.FLAG_ONGOING_EVENT;

        RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.notification);
        Notify.contentView = contentView;

        Intent notificationIntent = new Intent(context, Toggles.class);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
        Notify.contentIntent = contentIntent;

                    int HELO_ID = 00000;

        NotifyM.notify(HELLO_ID, Notify);
    }

    Intent serviceIntent = new Intent();
    serviceIntent.setAction("com.leozar100.myapp.NotifyService");
    context.startService(serviceIntent);
}

public static boolean getBootup(Context context){
    return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(BOOTUP_KEY, BOOTUP_TRUE);
}
}

我开始的服务没有做任何我只是启动一个,因为我认为它只是帮助广播接收器工作。此广播接收器也在我的清单中注册如下:

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

在需要权限android.permission.RECEIVE_BOOT_COMPLETED

的启动时启动

可以找到我的问题参考here

P.S。欢迎来到stackoverflow