2012-12-24 38 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