android 从广播接收器获取偏好

发布于 2024-12-11 10:11:03 字数 141 浏览 0 评论 0原文

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

Well, i am an Android newbie. I am trying to write an application which has one activity which starts another activity (Preference Activity), and one BoradcastReceiver. They are all in three different files. My question is: How to share Preferences betweens these components, e.g. how to read preferences that are set in Preference Activity from Broadcast Receiver?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回忆追雨的时光 2024-12-18 10:11:03

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

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

可以找到对我的问题的参考此处

PS 欢迎来到堆栈溢出

I have asked this same exact question before and here is the code I used:

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);
}
}

The service that I start does nothing I just initiate one because I think it just helps the broadcast receiver work. Also this broadcast receiver is registered in my manifest like so:

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

Which starts on bootup requiring the permission android.permission.RECEIVE_BOOT_COMPLETED

Reference to my question can be found here

P.S. Welcome to stackoverflow

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文