2012-07-11 197 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);