Android的共享preferences不工作 [英] Android Shared Preferences not working
问题描述
我有一个服务,其中有 4全局静态INT变量和我有BOOT的接收器完全和呼叫事件。我所试图做的是保存这些4个变量,只要调用事件接收器被执行,并在执行BOOT接收器获取它们,但它们都没有工作(当我通过电话重新启动,当然)..另一件事的共享preferences也是有用的,当设备重新启动?的code下面给出
共享preferences saved_values = this.getShared preferences(
com.example.app,Context.MODE_PRIVATE);
。saved_values.edit()putInt(呼,MyService.callcount);
。saved_values.edit()putInt(callend,MyService.callendcount);
。saved_values.edit()putInt(网络,MyService.network_count);
。saved_values.edit()putInt(振铃,MyService.ringingcount);
。saved_values.edit()提交();
和检索
共享preferences saved_values = this.getShared preferences(
com.example.app,Context.MODE_PRIVATE);
MyService.callcount = saved_values.getInt(呼,-10);
MyService.ringingcount = saved_values.getInt(振铃,-10);
为MyService。 network_count = saved_values.getInt(网络,-10);
为MyService。 callendcount = saved_values.getInt(callend,-10);
我用这个和它的工作对我来说。
有关节能
共享preferences saved_values = preferenceManager.getDefaultShared preferences(getApplicationContext());
共享preferences.Editor编辑器= saved_values.edit();
editor.putInt(算,算);
editor.putInt(富,富);
editor.commit();
和检索
共享preferences saved_values = preferenceManager.getDefaultShared preferences(getApplicationContext());
数= saved_values.getInt(计数,-1);
I have a Service in which there are 4 global static int variables and i have a receiver of BOOT COMPLETE and Call event. what I am trying to do is save these 4 variables whenever Call event receiver is execute and Retrieve them when BOOT receiver is executed (of course when I restarted by phone) but Both are not working.. another thing is shared preferences are also useful when device restarts?? the code is given below
SharedPreferences saved_values = this.getSharedPreferences(
"com.example.app", Context.MODE_PRIVATE);
saved_values.edit().putInt("call", MyService.callcount);
saved_values.edit().putInt("callend",MyService.callendcount);
saved_values.edit().putInt("network",MyService.network_count);
saved_values.edit().putInt("ringing",MyService.ringingcount);
saved_values.edit().commit();
and for retrieving
SharedPreferences saved_values = this.getSharedPreferences(
"com.example.app", Context.MODE_PRIVATE);
MyService.callcount = saved_values.getInt("call", -10);
MyService.ringingcount=saved_values.getInt("ringing", -10);
MyService. network_count=saved_values.getInt("network", -10);
MyService. callendcount=saved_values.getInt("callend", -10);
I used this and it worked for me.
For saving
SharedPreferences saved_values = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor=saved_values.edit();
editor.putInt("count",count);
editor.putInt("foo",foo);
editor.commit();
and for retrieving
SharedPreferences saved_values = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
count = saved_values.getInt("count", -1);
这篇关于Android的共享preferences不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!