質問するログイン新規登録

質問編集履歴

1

コードの追加

2019/08/01 01:02

投稿

uhsi
uhsi

スコア57

title CHANGED
File without changes
body CHANGED
@@ -21,4 +21,52 @@
21
21
  }
22
22
  }
23
23
  ```
24
- 上記のコードのコメントがある部分に記載をすればよいと思うのですがここからMainActivityの処理を呼び出す方法がわかりません。
24
+ 上記のコードのコメントがある部分に記載をすればよいと思うのですがここからMainActivityの処理を呼び出す方法がわかりません。
25
+
26
+ 質問が複数になって申し訳ありませんが、以下のコードは正しいですか。特にContext(★)の部分が心配です。またほかに良い方法はありますでしょうか。
27
+ ```Java
28
+ //MainActivity
29
+ AsyncTaskClass task = new AsyncTaskClass(this);
30
+ task.execute("");
31
+ ```
32
+ ```Java
33
+ public class AsyncTaskClass extends AsyncTask<String, String, String> {
34
+
35
+ Context context; //★
36
+
37
+ public AsyncTaskClass(Context context) {
38
+ this.context = context;
39
+ }
40
+
41
+ @Override
42
+ protected String doInBackground (String... params) {
43
+ scanFile();
44
+ return "";
45
+ }
46
+ private void scanFile(String fileName){
47
+ InputStream is = null;
48
+ BufferedReader br = null;
49
+ int times = 1;
50
+ try {
51
+ try {
52
+ is = context.getAssets().open(fileName+".txt"); //★
53
+ br = new BufferedReader(new InputStreamReader(is));
54
+ String str;
55
+
56
+ while ((str = br.readLine()) != null) {
57
+ //省略
58
+ }
59
+ } finally {
60
+ if (is != null) is.close();
61
+ if (br != null) br.close();
62
+ }
63
+ } catch (Exception e){
64
+ }
65
+ }
66
+
67
+ @Override
68
+ protected void onPostExecute(String str) {
69
+ // UIスレッドに反映する処理
70
+ }
71
+ }
72
+ ```