gpt4 book ai didi

java - SharedPreference 不包含广播接收器中的任何内容

转载 作者:搜寻专家 更新时间:2023-11-01 09:46:17 25 4
gpt4 key购买 nike

所以我是 android 的新手,我开发了一个简单的闹钟应用程序。问题是当我重启手机时我的闹钟会重置,所以我创建了一个在开机时启动的接收器。这个接收器应该从我的共享首选项中获取我的 quickAlarms 列表,循环遍历它,然后将它们发送到我的 AlarmService。问题是我无法获取 sharedPreference。我试过这些方法:

    sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
sharedPref = context.getSharedPreferences("my_pref", Context.MODE_PRIVATE);

这是在启动时调用的 broadcastReceiver:

 @Override
public void onReceive(Context context, Intent intent) {
Log.d("BootReceiver", "BootReceiver called");
sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
//sharedPref = context.getSharedPreferences("my_pref", Context.MODE_PRIVATE);
if(sharedPref.contains("alarm")){
Log.d("BootReceiver", "SharedPreference do contain alarms, trying to set them...");
alarmService= new AlarmService(context);
activate(context);
}else{
Log.d("BootReceiver", "SharedPreference does not contain any alarms.");
//I always get this message, so sharedPref never contains anything here!
}
}

/**
* Loops through the stored list of QuickAlarms, checks if the are set to on and then reshedules them.
* @param context
*/

public void activate(Context context) {
Gson gson = new Gson();
Type type = new TypeToken<List<QuickAlarm>>() {}.getType();
List<QuickAlarm> alarmList = gson.fromJson(sharedPref.getString("alarm", ""), type);
Iterator<QuickAlarm> alarmIterator = alarmList.iterator();

while(alarmIterator.hasNext()){
if(alarmIterator.next().isActive()){
alarmService.onHandleIntent(new Intent(context, AlarmReceiver.class).putExtra("time", alarmIterator.next().getTime()).setAction("CREATE").putExtra("repeat", alarmIterator.next().getRepeat()).putExtra("id" , alarmIterator.next().getID()).putExtra("desc", alarmIterator.next().getDesc()));
}
}
Log.d("BootReceiver", "All alarms are set again!");
}}

这是 MainActivity 中的代码,我声明了 sharedpreference 并在 Controller 类中使用它:

    private SharedPreferences sharedPref;
//...
sharedPref = this.getPreferences(Context.MODE_PRIVATE);
controller = new Controller(sharedPref, this.getApplicationContext());

然后它真的是使用它的 Controller :

 public Controller(SharedPreferences sharedPref, Context context) {
this.context = context;
this.sharedPref = sharedPref;
editor = sharedPref.edit();
}

//...
public void addQuickAlarm(QuickAlarm qa, int id) {
Gson gson = new Gson();
Type type = new TypeToken<List<QuickAlarm>>() {
}.getType();
List<QuickAlarm> alarms;
if (gson.fromJson(sharedPref.getString("alarm", ""), type) == null) {
alarms = new ArrayList<>();
} else {
alarms = gson.fromJson(sharedPref.getString("alarm", ""), type);
}
if (id == -10) {
alarms.add(qa);
Log.d("Controller", "QuickAlarm added to pref");
} else {
alarms.remove(id);
alarms.add(id, qa);
Log.d("Controller", "QuickAlarm edited to pref");
}
String jsonAlarm = gson.toJson(alarms); //converting list to json and saving back
editor.putString("alarm", jsonAlarm);
editor.commit();
}
public void activateAlarm(boolean on, int id) {
Gson gson = new Gson();
Type type = new TypeToken<List<QuickAlarm>>() {
}.getType();
List<QuickAlarm> alarmList = gson.fromJson(sharedPref.getString("alarm", ""), type);
QuickAlarm qa = alarmList.get(id);
qa.setActive(on);
int[] repeat = qa.getRepeat();
addQuickAlarm(qa, id);
if (on) {
alarmService.onHandleIntent(new Intent(context, AlarmReceiver.class).putExtra("time", qa.getTime()).setAction("CREATE").putExtra("repeat", repeat).putExtra("id" , id).putExtra("desc", qa.getDesc()));
} else {
alarmService.onHandleIntent(new Intent(context, AlarmReceiver.class).putExtra("time", qa.getTime()).setAction("CANCEL").putExtra("id" , id));
}
}

最佳答案

当你像这样设置警报警报时,你需要将“警报”键及其值添加到你的共享首选项中

sharedPref = context.getSharedPreferences("my_pref", Context.MODE_PRIVATE);

sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor=sharedPref.edit();
editor.putString("alarm", "value u want to keep");
editor.commit();

关于java - SharedPreference 不包含广播接收器中的任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37940437/

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