2012-07-11 180 views
-1

我已经尝试使用下面的代码,但我收到错误。 getSharedPreferences未定义。是否有其他方式将字符串值存储在Intent服务中并在活动中访问它。使用共享偏好来存储服务内的数据?

 SharedPreferences prefs = this.getSharedPreferences(
    "com.example.app", Context.MODE_PRIVATE); 
+0

请参阅本博客其对共享偏好优秀教程http://saigeethamn.blogspot.in/2009/10/shared-preferences- android-developer.html – Aerrow 2012-07-11 10:44:05

+0

@Aerrow感谢您提供优秀的资源。 – Rishikesh 2012-07-11 10:57:58

回答

2

尝试这样的:

SharedPreferences prefs = getApplicationContext().getSharedPreferences(
    "com.example.app", Context.MODE_PRIVATE); 

代替

SharedPreferences prefs = this.getSharedPreferences(
    "com.example.app", Context.MODE_PRIVATE); 
0

getSharedPreferences()是一个上下文对象的方法。这意味着你必须在Acitivity内调用它。如果您从View子类调用它,你必须做一些事情,如:

context.getSharedPreferences(PREFS_NAME, 0);