質問編集履歴

1

コードの追加

2019/08/01 01:02

投稿

uhsi
uhsi

スコア57

test CHANGED
File without changes
test CHANGED
@@ -45,3 +45,99 @@
45
45
  ```
46
46
 
47
47
  上記のコードのコメントがある部分に記載をすればよいと思うのですがここからMainActivityの処理を呼び出す方法がわかりません。
48
+
49
+
50
+
51
+ 質問が複数になって申し訳ありませんが、以下のコードは正しいですか。特にContext(★)の部分が心配です。またほかに良い方法はありますでしょうか。
52
+
53
+ ```Java
54
+
55
+ //MainActivity
56
+
57
+ AsyncTaskClass task = new AsyncTaskClass(this);
58
+
59
+ task.execute("");
60
+
61
+ ```
62
+
63
+ ```Java
64
+
65
+ public class AsyncTaskClass extends AsyncTask<String, String, String> {
66
+
67
+
68
+
69
+ Context context; //★
70
+
71
+
72
+
73
+ public AsyncTaskClass(Context context) {
74
+
75
+ this.context = context;
76
+
77
+ }
78
+
79
+
80
+
81
+ @Override
82
+
83
+ protected String doInBackground (String... params) {
84
+
85
+ scanFile();
86
+
87
+ return "";
88
+
89
+ }
90
+
91
+ private void scanFile(String fileName){
92
+
93
+ InputStream is = null;
94
+
95
+ BufferedReader br = null;
96
+
97
+ int times = 1;
98
+
99
+ try {
100
+
101
+ try {
102
+
103
+ is = context.getAssets().open(fileName+".txt"); //★
104
+
105
+ br = new BufferedReader(new InputStreamReader(is));
106
+
107
+ String str;
108
+
109
+
110
+
111
+ while ((str = br.readLine()) != null) {
112
+
113
+ //省略
114
+
115
+ }
116
+
117
+ } finally {
118
+
119
+ if (is != null) is.close();
120
+
121
+ if (br != null) br.close();
122
+
123
+ }
124
+
125
+ } catch (Exception e){
126
+
127
+ }
128
+
129
+ }
130
+
131
+
132
+
133
+ @Override
134
+
135
+ protected void onPostExecute(String str) {
136
+
137
+ // UIスレッドに反映する処理
138
+
139
+ }
140
+
141
+ }
142
+
143
+ ```