SharePreference之记录应用的使用次数

public class MainActivity extends Activity
{

    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sharedPreferences = getSharedPreferences("count", MODE_WORLD_READABLE);
        int count = sharedPreferences.getInt("count",0);
        Toast.makeText(this,"程序以前被使用了"+count + "次。",Toast.LENGTH_LONG).show();
        editor = sharedPreferences.edit();
        editor.putInt("count",++count);
        editor.commit();
    }
}

你可能感兴趣的:(Android)