Android 非同期処理

手順
1.AsyncTaskのクラスを作成
2.doInBackground()に処理を記載
3.元クラスでAsyncTaskのクラスをnewしexecuteで実行
方法は三つ
非同期処理用のスレッド AsyncTask<型1,型2, 型3
 型1 doInBackground に渡す型
 型2 onProgressUpdate 等進捗で渡す型
 型3 doInBackground やonPostExecuteの戻値不要な時はVOIDを指定

AsyncTaskの記載 MyAsyncTask

public class MyAsyncTask extends AsyncTask<String,Integer,String> {
第一引数 onPreExecute() doInBackground の引数
第二引数 onProgressUpdate d の引数
第三引数 doInBackground やonPostExecuteの引数


/*
 * onPreExecute() 初期処理
 */
protected void onPreExecute() {
 super.onPreExecute();
 prg = new ProgressDialog(context);
 prg.setMessage("読み込み中...数分かかります");
 prg.setOnCancelListener(new OnCancelListener() {
 @Override
 public void onCancel(DialogInterface dialog) {
	cancel(false);
	}
 });
	prg.show();
}

/*
 * doInBackground 実行処理
 */
protected String doInBackground(String... params) {
	String str =  params[0];
	//仕入れデータ
	Integer cnt = 0;
	//ファイルのオープン
	BufferedReader br = null;
	String sdpath = "";
	String ifilena = "";
	File fs;
//処理結果を戻す
 return str;
****
@Override
/*
 * onPostExecute 終了処理
 *
 */
protected void onPostExecute(String result) {
	prg.dismiss();
	AlertDialog.Builder alertDialog=new AlertDialog.Builder(context);
//タイトルを設定する
	alertDialog.setTitle("データ取り込み(削除)処理");
	//メッセージ内容を設定する
	alertDialog.setMessage(result);
	//確認ボタンん処理を設定する
	alertDialog.setPositiveButton("OK",new DialogInterface.OnClickListener() {
	public void onClick(DialogInterface dialog,int whichButton)			}
		});
		alertDialog.create();
		alertDialog.show();
	}
	@Override

protected void onCancelled() {
		super.onCancelled();
//キャンセルされたときの処理
	}

呼び出す側の記載
task.execute(dat,””,””)でAsyncTask内のdoInBackgroundが実行される

	private static final String HACH = "HACHDATA";
	private static final String PICK = "PICKDATA";


	/*---------------------------------
	 * loadTASK
	 * なぜか中に記載さすとエラーになるので
	 * --------------------------------
	 */
	public void loadTASK(String dat){
		MyAsyncTask task = new MyAsyncTask(this);
		task.execute(dat,"","");
	}