classInternalData

Last-modified: 2015-03-18 (水) 12:37:27
public class InternalData extends Activity implements OnClickListener{

	EditText  sharedData;
	TextView dataResults;
	FileOutputStream fos;
	String FILENAME = "InternalString";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO 自動生成されたメソッド・スタブ
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sharedpreferences);
		setupVariables();
	}
	private void setupVariables() {
		// TODO 自動生成されたメソッド・スタブ
		Button save = (Button) findViewById (R.id.bSave);
		Button load = (Button) findViewById (R.id.bLoad);
		sharedData = (EditText) findViewById (R.id.etSharedPrefs);
		dataResults = (TextView) findViewById (R.id.tvLoadSharedPrefs);
		save.setOnClickListener(this);
		load.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO 自動生成されたメソッド・スタブ
		switch(v.getId()){
		case R.id.bSave:
			String data = sharedData.getText().toString();
			// Saving data via File
			/*File f = new File(FILENAME);
			try {
				fos = new FileOutputStream(f);
				fos.close();
			} catch (FileNotFoundException e) {
				// TODO 自動生成された catch ブロック
				e.printStackTrace();
			} catch (IOException e) {
				// TODO 自動生成された catch ブロック
				e.printStackTrace();
			}
			*/


			try {
				fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
				fos.write(data.getBytes());
				fos.close();
			} catch (IOException e) {
				// TODO 自動生成された catch ブロック
				e.printStackTrace();
			}
			break;
		case R.id.bLoad:
			new loadSomeStuff().execute(FILENAME);
			break;
		}
	}

	public class loadSomeStuff extends AsyncTask<String, Integer, String>{

		ProgressDialog dialog;

		protected void onPreExecute(){
			// example of setting up something
			dialog = new ProgressDialog(InternalData.this);
			dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			dialog.setMax(100);
			dialog.show();
		}

		@Override
		protected String doInBackground(String... params) {
			// TODO 自動生成されたメソッド・スタブ
			String collected = null;
			FileInputStream fis = null;

			for(int i = 0; i < 20; i++){
				publishProgress(5);
				try{
					Thread.sleep(88);
				} catch (InterruptedException e){
					e.printStackTrace();
				}
			}
			dialog.dismiss();
			try {
				fis = openFileInput(FILENAME);
				byte[] dataArray = new byte[fis.available()];
				while(fis.read(dataArray) != -1){
					collected = new String(dataArray);
				}
			} catch (FileNotFoundException e) {
				// TODO 自動生成された catch ブロック
				e.printStackTrace();
			} catch (IOException e) {
				// TODO 自動生成された catch ブロック
				e.printStackTrace();
			} finally {
				try {
					fis.close();
					return collected;
				} catch (IOException e) {
					// TODO 自動生成された catch ブロック
					e.printStackTrace();
				}
			}
			return null;
		}

	protected void onProgressUpdated(Integer...progress){
		dialog.incrementProgressBy(progress[0]);
	}
		protected void onPostExecute(String result){
			dataResults.setText(result);
		}
	}
}



~
~<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <android.widget.EditText
        android:id="@+id/etSharedPrefs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         >
    </android.widget.EditText>
    <Button
        android:id="@+id/bSave"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save" />
    <Button
        android:id="@+id/bLoad"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Load" />
    <TextView
        android:id="@+id/tvLoadSharedPrefs"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Load your data" />
~</LinearLayout>