SharedPreference不包含广播接收器

时间:2016-06-21 09:23:27

标签: java android broadcastreceiver sharedpreferences

所以我是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中的代码,我声明了共享首选项并在Controller类中使用它:

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

然后它真的是使用它的控制器:

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

2 个答案:

答案 0 :(得分:1)

当你设置类似这样的警报提醒时,你需要将“警报”键及其值添加到你的共享偏好

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

答案 1 :(得分:1)

无法启动接收方mysql,因为在com.example.user.alarmclock.Local.StartMyServiceAtBootReceiver: java.util.NoSuchElementException之后您正在呼叫另一个alarmIterator.next().isActive(),这样next()这意味着您要求alarmIterator.next().getTime()下一个项目对象(List<QuickAlarm >)不存在,只有你得到错误。将代码更改为此,因为即使您的意思是此QuickAlarm} if中的QuickAlarm为真,也请从此对象(isActive())中提取其他信息,例如{{1} },QuickAlarm等。

getTime()