gpt4 book ai didi

android从广播接收器获取偏好

转载 作者:行者123 更新时间:2023-11-29 16:19:42 25 4
gpt4 key购买 nike

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

最佳答案

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

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

我启动的服务什么都不做,我只是启动一个,因为我认为它只是帮助广播接收器工作。此广播接收器也像这样在我的 list 中注册:

<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

附言欢迎来到 stackoverflow

关于android从广播接收器获取偏好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7855918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com