SharedPreferences:轻量级的储存,故名思意是做存储用的,当我们需要保存用户的某些settings值,需要轻量级的记忆操作等时使用!
write(index,context);//传一个index值过来!
。
。
private void write(int key,Context context){ SharedPreferences preferences = context.getSharedPreferences("index", Context.MODE_PRIVATE); Editor editor = preferences.edit(); editor.putInt("indexKey", key); Log.i("TAG", "MyReceiver's key="+key); editor.commit(); }
系统会生成一个index.xml文件储存在内存里!
private static boolean getDataFromSharePre(){//此时可以拿到存储的值进行判断了 SharedPreferences preferences = context.getSharedPreferences("index", Context.MODE_PRIVATE); return (preferences.getInt("indexKey", 0)==5)?true:false; }
当机器关机后,该文件依然存在,重启时可拿到这个文件进行对应判断操作了!
附:if ("android.intent.action.BOOT_COMPLETED".equals(action)) {//开机广播