2012-12-24 36 views
0

我正在使用Java創建一個簡單的Click Counter Android應用程序。我是Java新手。我想保存在應用程序的退出計數的數量,是否如果按下後退按鈕或應用程序被關閉或崩潰,等等。這裏是我的代碼至今:共享首選項不保存Android的確切實例狀態應用程序

public class wazeefa extends Activity { 

//Count Button 
TextView txtCount; 
Button btnCount; 
int count; 
Button wmute; 
Button wreset; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.wazeefa); 

    //SAVE COUNT 
    SharedPreferences app_preferences = 
     PreferenceManager.getDefaultSharedPreferences(this); 

    count = app_preferences.getInt("count", 0); 

    txtCount = (TextView)findViewById(R.id.wcount); 
    txtCount.setText("This app has been started " + count + " times."); 

    SharedPreferences.Editor editor = app_preferences.edit(); 
    editor.putInt("count", ++count); 
    editor.commit(); 

    //Button SOUND AND COUNT 
    final MediaPlayer mpButtonClick = MediaPlayer.create(this, R.raw.bubble); 

    txtCount = (TextView)findViewById(R.id.wcount); 
    txtCount.setText(String.valueOf(count)); 
    btnCount = (Button)findViewById(R.id.wclick); 

    btnCount.setOnClickListener(new OnClickListener() { 
     public void onClick(View V) { 
     final ImageView image = (ImageView) findViewById(R.id.imageview); 
      count++; 
      if (count > 50) count = 0; image.setImageResource(R.drawable.duroodimage); 
      if (count > 0) image.setImageResource(R.drawable.duroodimage); 
      if (count > 9) image.setImageResource(R.drawable.zikrimage); 
      if (count > 39) image.setImageResource(R.drawable.duroodimage); 
      txtCount.setText(String.valueOf(count)); 
      mpButtonClick.start(); 

    //RESET Button 
    wreset = (Button)findViewById(R.id.wreset);  
    wreset.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      count = 0; 
      image.setImageResource(R.drawable.duroodimage);; 
       txtCount.setText("0"); 


    } 

我m有2個應用程序問題。

首先,計數並未保存到應用程序關閉然後再次打開時的狀態。例如,如果計數值爲'20',並且我點擊後退按鈕,則返回到計數器顯示爲'3'的同一頁面。 - 有趣的是,每次我嘗試了上面的內容並在點擊後退按鈕後重新回到應用程序中時,計數已經增加了1個計數。

第二個問題是,當我回到應用程序,例如計數顯示爲'5'時,重置按鈕不再起作用 - 它什麼都不做。但是,當我繼續計數,然後點擊重置按鈕時,它會將計數再次更改爲零。

請任何人都可以協助修復上述兩個問題?

建議後的新代碼:

public class wazeefa extends Activity { 

//Count Button 
TextView txtCount; 
Button btnCount; 
Button wmute; 
Button wreset; 
public static int count=0; 
SharedPreferences app_preferences; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.wazeefa); 

    //SAVE COUNT 
    SharedPreferences app_preferences = 
      PreferenceManager.getDefaultSharedPreferences(this); 

    count = app_preferences.getInt("count", 0); 

    txtCount = (TextView)findViewById(R.id.wcount); 
    txtCount.setText("This app has been started " + count + " times.");} 

protected void onPause() { 
    super.onPause(); 

    // save count value here 
    SharedPreferences.Editor editor = app_preferences.edit(); 
    editor.putInt("count", count); 
    editor.commit(); 

    //SOUND and COUNT 
    final MediaPlayer mpButtonClick = MediaPlayer.create(this, R.raw.bubble); 

    txtCount = (TextView)findViewById(R.id.wcount); 
    txtCount.setText(String.valueOf(count)); 
    btnCount = (Button)findViewById(R.id.wclick); 

    btnCount.setOnClickListener(new OnClickListener() { 
     public void onClick(View V) { 
     final ImageView image = (ImageView) findViewById(R.id.imageview); 
      count++; 
      if (count > 50) count = 0; image.setImageResource(R.drawable.duroodimage); 
      if (count > 0) image.setImageResource(R.drawable.duroodimage); 
      if (count > 9) image.setImageResource(R.drawable.zikrimage); 
      if (count > 39) image.setImageResource(R.drawable.duroodimage); 
      txtCount.setText(String.valueOf(count)); 
      mpButtonClick.start(); 

    //RESET Button 
    wreset = (Button)findViewById(R.id.wreset);  
    wreset.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      count = 0; 
      image.setImageResource(R.drawable.duroodimage);; 
       txtCount.setText("0"); 


    } 
我的代碼

最後一部分:)

protected void onPause() { 
     super.onPause(); 

    // save count value here 

     SharedPreferences.Editor editor = app_preferences.edit(); 
     editor.putInt("count", count); 
     editor.commit(); 


     }; 
    }); 
     }}); 
    };} 

回答

6

申報的一流水平計算爲靜態:

public static int count=0; 
SharedPreferences app_preferences ; 

,並使用爲SharedPreferences保存計數值爲:

protected void onPause() { 
    super.onPause(); 

    // save count value here 
    SharedPreferences.Editor editor = app_preferences.edit(); 
    editor.putInt("count", count); 
    editor.commit(); 
} 
+0

如果我想在按下後退按鈕時保存計數,我是否也需要使用onDestroy? – Mustafa

+1

@Mustafa:用戶使用onPause()而不是onDestroy()或按鈕點擊更好,因爲如果您在onPause()中保存值,則每次用戶按Home按鈕或Back按鈕時都會保存。並在按鈕點擊你只需要更新計數值它會自動插入最新的SharedPreferences,因爲計數是靜態的 –

+0

我已經改變了代碼建議,但通常顯示的計數,我現在得到的消息'這個應用程序已......「。計數不再顯示。 – Mustafa

1

如果你希望它在退出時保存,郵政的onDestroy(此代碼,而不是的onCreate的( )

SharedPreferences.Editor editor = app_preferences.edit(); 
editor.putInt("count", ++count); 
editor.commit(); 
+0

嗨SHREYA,謝謝。 – Mustafa