0

"tgpref"라는 MainActivity에 값을 저장했습니다. 에서다른 활동에 환경 설정을로드 하시겠습니까?

SharedPreferences.Editor editor = preferences.edit(); 
      editor.putBoolean("tgpref", true); //value to save 
      editor.commit();" 

내에서 onCreate 나는 그래서 난 위젯 프로 바이더 클래스에서의 onUpdate에서 작성하려고 내 위젯의 값을 표시 할

public SharedPreferences preferences; 
--- 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
preferences = getPreferences(MODE_PRIVATE); 

이이

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); 
     boolean tgpref = preferences.getBoolean("tgpref", false) 
     if (tgpref == true) { 
     remoteViews.setTextViewText(R.id.battery, "Risp on"); 
     } else { 
     remoteViews.setTextViewText(R.id.battery, "Risp off"); 
     } 

경우] MainActivty의 토글 버튼을 클릭했습니다. 내 위젯에서 "Risp on", "Risp off"가 나타나기를 원합니다. 지금은 "Risp off"만 표시하므로 내가 어떻게 할 수 있는지 알지 못합니다. 어떤 도움? 아무 것도 내가 값을로드 할 수 없습니다.

+0

누구든지 나를 도와 줄 수 있습니까? 내 위젯에서 값을로드 할 수 없습니다. –

답변

0

좋아 보인다, 그래서 당신의 버튼이 가치 변화를 유발하지 않는 것 같아?!

MainActiviy

private Boolean mTgpref; 
private SharedPreferences prefs; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 

    //... 
    //get value from shared preferences and update ui 
    prefs = this.getSharedPreferences("com.example.app", Context.MODE_PRIVATE); 
    mTgpref = prefs.getBoolean("tgpref", false); 
    setYourText(); 

} 

private void setYourTextAndStoreToSharedPref(){ 
    Log.i("TEST","mTgpref -> " + mTgpref); //check value 
    if(mTgpref){   
     remoteViews.setTextViewText(R.id.battery, "Risp on"); 
    } 
    else 
    { 
     remoteViews.setTextViewText(R.id.battery, "Risp off"); 
    } 
    prefs.edit().putBoolean("tgpref", mTgpref).commit(); 

    //UPDATE YOUR WIDGET HERE 
}  

//function called on switch button click  
private void onSwitchClickButtonClick(){ 
    mTgpref = !mTgpref; //toggle Boolean 
    setYourText(); 
} 

편집 죄송합니다, 나는 당신의 주요 문제를 오해. SharedPreferences를 업데이트 한 후 여기에 설명 된 단계를 따라야합니다. http://developer.android.com/guide/topics/appwidgets/index.html#UpdatingFromTheConfiguration

+0

그리고 위젯에? –

+0

upps, 주된 문제가 sharedPreferences와 관련 있다고 생각하고 답변을 수정합니다. – longilong

관련 문제