回答編集履歴
3
見づらいので改行位置を修正
answer
CHANGED
@@ -21,14 +21,12 @@
|
|
21
21
|
OnSensorChanged()メソッドがそのスレッドで呼ばれているかどうかによります。
|
22
22
|
|
23
23
|
###ActivityとService間の通信について追記(2017/10/11)
|
24
|
-
ご質問の問題の中身とコメントの内容を再考してみますと、どうも混乱させてしまったようですので、
|
24
|
+
ご質問の問題の中身とコメントの内容を再考してみますと、どうも混乱させてしまったようですので、追記することで整理させていただきたいと思います。※混乱のひとつは私の回答のせいであります。
|
25
|
-
追記することで整理させていただきたいと思います。※混乱のひとつは私の回答のせいであります。
|
26
25
|
|
27
26
|
当初の質問で示された以下のエラーに関してですが、
|
28
27
|
> java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View com.example.kimuratomoya.sensor_db_backup.MainActivity.findViewById(int)' on a null object reference
|
29
28
|
|
30
|
-
この点の回答としては、abs123様のものが適切だと考えています。(K-Tomoya様、これはOKですよね?)
|
29
|
+
この点の回答としては、abs123様のものが適切だと考えています。(K-Tomoya様、これはOKですよね?)で、次に、私が最初に回答させてもらった件
|
31
|
-
で、次に、私が最初に回答させてもらった件
|
32
30
|
> 他のスレッドからTextViewの更新をしている為にエラーが起きています。
|
33
31
|
> TextViewは、この場合MainActivityが動作しているUIスレッドで更新する必要があります。
|
34
32
|
|
2
当初の回答に不足、不適切な事項があったので、追記。
answer
CHANGED
@@ -18,4 +18,195 @@
|
|
18
18
|
としているので、一見、大丈夫なようにも見えます。
|
19
19
|
|
20
20
|
問題無いかどうかは、SensorEventListenerが違うスレッドで動作していて、
|
21
|
-
OnSensorChanged()メソッドがそのスレッドで呼ばれているかどうかによります。
|
21
|
+
OnSensorChanged()メソッドがそのスレッドで呼ばれているかどうかによります。
|
22
|
+
|
23
|
+
###ActivityとService間の通信について追記(2017/10/11)
|
24
|
+
ご質問の問題の中身とコメントの内容を再考してみますと、どうも混乱させてしまったようですので、
|
25
|
+
追記することで整理させていただきたいと思います。※混乱のひとつは私の回答のせいであります。
|
26
|
+
|
27
|
+
当初の質問で示された以下のエラーに関してですが、
|
28
|
+
> java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View com.example.kimuratomoya.sensor_db_backup.MainActivity.findViewById(int)' on a null object reference
|
29
|
+
|
30
|
+
この点の回答としては、abs123様のものが適切だと考えています。(K-Tomoya様、これはOKですよね?)
|
31
|
+
で、次に、私が最初に回答させてもらった件
|
32
|
+
> 他のスレッドからTextViewの更新をしている為にエラーが起きています。
|
33
|
+
> TextViewは、この場合MainActivityが動作しているUIスレッドで更新する必要があります。
|
34
|
+
|
35
|
+
ですが、これはこれでその通りなのですが、今回はActivityとService間の通信の話になってくるので、ActivityのUIスレッドだけの話では終わらないです。この点、私の回答が拙速であったこと、お詫びします。
|
36
|
+
|
37
|
+
では結局、Activityが持っているTextViewをService側で発生したデータで更新して表示したいと言う要求はどうしたらいいのかと言うと、簡単には「Activityに`BroadcastReceiver`を持たせ、Serivice側からActivityへIntentで通知する。」との方法が適切かと思います。その為のサンプルを示します。Service内で1秒毎にActivityへIntentを送信し、ActivityではそのIntentから取り出した文字列をTextViewに表示するだけのものです。短いので全部載せます。
|
38
|
+
|
39
|
+
尚、**本サンプルではActivityが停まってもServiceは停まりません**のでご注意ください。
|
40
|
+
|
41
|
+
Activityの、MainActivity.java です。
|
42
|
+
```Java
|
43
|
+
package examples.products.test24;
|
44
|
+
|
45
|
+
import android.content.BroadcastReceiver;
|
46
|
+
import android.content.Context;
|
47
|
+
import android.content.Intent;
|
48
|
+
import android.content.IntentFilter;
|
49
|
+
import android.support.v7.app.AppCompatActivity;
|
50
|
+
import android.os.Bundle;
|
51
|
+
import android.util.Log;
|
52
|
+
import android.widget.TextView;
|
53
|
+
|
54
|
+
public class MainActivity extends AppCompatActivity {
|
55
|
+
|
56
|
+
private static final String TAG = "Test24.MainActivity";
|
57
|
+
|
58
|
+
private BroadcastReceiver mReceiver = null;
|
59
|
+
private IntentFilter mIntentFilter = null;
|
60
|
+
private TextView mTextView1 = null;
|
61
|
+
|
62
|
+
@Override
|
63
|
+
protected void onCreate(Bundle savedInstanceState) {
|
64
|
+
super.onCreate(savedInstanceState);
|
65
|
+
setContentView(R.layout.activity_main);
|
66
|
+
mTextView1 = (TextView) findViewById(R.id.textview1);
|
67
|
+
|
68
|
+
mReceiver = new BroadcastReceiver() {
|
69
|
+
@Override
|
70
|
+
public void onReceive(Context context, Intent intent) {
|
71
|
+
// このonReceiveでMainServiceからのIntentを受信する。
|
72
|
+
Bundle bundle = intent.getExtras();
|
73
|
+
String message = bundle.getString("message");
|
74
|
+
Log.d(TAG, "Message from MainService: " + message);
|
75
|
+
// TextViewへ文字列をセット
|
76
|
+
mTextView1.setText(message);
|
77
|
+
}
|
78
|
+
};
|
79
|
+
|
80
|
+
// "TEST24_ACTION" Intentフィルターをセット
|
81
|
+
mIntentFilter = new IntentFilter();
|
82
|
+
mIntentFilter.addAction("TEST24_ACTION");
|
83
|
+
registerReceiver(mReceiver, mIntentFilter);
|
84
|
+
|
85
|
+
Intent intent = new Intent(getApplication(), MainService.class);
|
86
|
+
startService(intent);
|
87
|
+
|
88
|
+
// !!!! サンプルの為、MainServiceサービスを停める処理が無いので、注意してください。
|
89
|
+
}
|
90
|
+
}
|
91
|
+
```
|
92
|
+
|
93
|
+
Serviceの、MainService.javaです。
|
94
|
+
```Java
|
95
|
+
package examples.products.test24;
|
96
|
+
|
97
|
+
import android.app.Service;
|
98
|
+
import android.content.Intent;
|
99
|
+
import android.os.IBinder;
|
100
|
+
import android.util.Log;
|
101
|
+
|
102
|
+
import java.util.Timer;
|
103
|
+
import java.util.TimerTask;
|
104
|
+
|
105
|
+
public class MainService extends Service {
|
106
|
+
private static final String TAG = "Test24.MainService";
|
107
|
+
private Timer mTimer = null;
|
108
|
+
private int mCount = 0;
|
109
|
+
|
110
|
+
@Override
|
111
|
+
public void onCreate() {
|
112
|
+
super.onCreate();
|
113
|
+
}
|
114
|
+
|
115
|
+
@Override
|
116
|
+
public int onStartCommand(Intent intent, int flags, int startId) {
|
117
|
+
Log.d(TAG, "onStartCommand():");
|
118
|
+
mTimer = new Timer();
|
119
|
+
mTimer.schedule(new TimerTask() {
|
120
|
+
@Override
|
121
|
+
public void run() {
|
122
|
+
mCount++;
|
123
|
+
|
124
|
+
// MainActivityへデータを送信
|
125
|
+
String message = String.format("count=%d", mCount);
|
126
|
+
sendBroadcast(message);
|
127
|
+
}
|
128
|
+
}, 0, 1000);
|
129
|
+
|
130
|
+
return super.onStartCommand(intent, flags, startId);
|
131
|
+
}
|
132
|
+
|
133
|
+
@Override
|
134
|
+
public void onDestroy() {
|
135
|
+
super.onDestroy();
|
136
|
+
Log.d(TAG, "onDestroy():");
|
137
|
+
// タイマーを停止
|
138
|
+
if (mTimer != null) {
|
139
|
+
mTimer.cancel();
|
140
|
+
mTimer = null;
|
141
|
+
}
|
142
|
+
}
|
143
|
+
|
144
|
+
@Override
|
145
|
+
public IBinder onBind(Intent intent) {
|
146
|
+
Log.d(TAG, "onBind():");
|
147
|
+
return null;
|
148
|
+
}
|
149
|
+
|
150
|
+
private void sendBroadcast(String message) {
|
151
|
+
Log.d(TAG, "sendBroadcast: " + message);
|
152
|
+
|
153
|
+
// IntentをブロードキャストすることでMainActivityへデータを送信
|
154
|
+
Intent intent = new Intent();
|
155
|
+
intent.setAction("TEST24_ACTION");
|
156
|
+
intent.putExtra("message", message);
|
157
|
+
getBaseContext().sendBroadcast(intent);
|
158
|
+
}
|
159
|
+
}
|
160
|
+
```
|
161
|
+
AndroidManifest.xmlです
|
162
|
+
```XML
|
163
|
+
<?xml version="1.0" encoding="utf-8"?>
|
164
|
+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
165
|
+
package="examples.products.test24">
|
166
|
+
|
167
|
+
<application
|
168
|
+
android:allowBackup="true"
|
169
|
+
android:icon="@mipmap/ic_launcher"
|
170
|
+
android:label="@string/app_name"
|
171
|
+
android:roundIcon="@mipmap/ic_launcher_round"
|
172
|
+
android:supportsRtl="true"
|
173
|
+
android:theme="@style/AppTheme">
|
174
|
+
<activity android:name=".MainActivity">
|
175
|
+
<intent-filter>
|
176
|
+
<action android:name="android.intent.action.MAIN" />
|
177
|
+
|
178
|
+
<category android:name="android.intent.category.LAUNCHER" />
|
179
|
+
</intent-filter>
|
180
|
+
</activity>
|
181
|
+
|
182
|
+
<service android:name=".MainService"></service>
|
183
|
+
</application>
|
184
|
+
</manifest>
|
185
|
+
```
|
186
|
+
レイアウトのlayout/activity_main.xmlです。
|
187
|
+
```XML
|
188
|
+
<?xml version="1.0" encoding="utf-8"?>
|
189
|
+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
190
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
191
|
+
xmlns:tools="http://schemas.android.com/tools"
|
192
|
+
android:layout_width="match_parent"
|
193
|
+
android:layout_height="match_parent"
|
194
|
+
android:orientation="vertical"
|
195
|
+
tools:context="examples.products.test24.MainActivity">
|
196
|
+
|
197
|
+
<TextView
|
198
|
+
android:id="@+id/textview1"
|
199
|
+
android:layout_width="wrap_content"
|
200
|
+
android:layout_height="wrap_content"
|
201
|
+
android:text="Text#1"
|
202
|
+
app:layout_constraintBottom_toBottomOf="parent"
|
203
|
+
app:layout_constraintLeft_toLeftOf="parent"
|
204
|
+
app:layout_constraintRight_toRightOf="parent"
|
205
|
+
app:layout_constraintTop_toTopOf="parent" />
|
206
|
+
</LinearLayout>
|
207
|
+
```
|
208
|
+
UIスレッドではないスレッドからのTextView等のビューの更新についてですが、先の回答で提示させてもらった以下のクラスメソッドの利用
|
209
|
+
- `Handler#post(Runnnable)`
|
210
|
+
- `TextView#post(Runnable)`
|
211
|
+
- `Activity#runOnUiThread(Runnable)`
|
212
|
+
は、これらはActivityで SensorEventListener, LocationListener等を使っていて、イベントがActivityとは違うスレッドで通知される場合に必要になります。今回はServiceでイベントをハンドリングしているので、恐らく問題にはならないと思われます。混乱させてすみません。
|
1
注意事項を追記
answer
CHANGED
@@ -7,4 +7,15 @@
|
|
7
7
|
Handler#post(Runnnable)とか、
|
8
8
|
TextView#post(Runnable)とか、
|
9
9
|
Activity#runOnUiThread(Runnable) 等の
|
10
|
-
メソッドを利用して、その中でTextViewを更新することになると思います。
|
10
|
+
メソッドを利用して、その中でTextViewを更新することになると思います。
|
11
|
+
|
12
|
+
**※上記、外している可能性があるので追記させていただきます。**
|
13
|
+
ご提示のコードをよく見ると
|
14
|
+
public void onSensorChanged() 内で
|
15
|
+
new Thread(new Runnable() {
|
16
|
+
@Override
|
17
|
+
public void run() {
|
18
|
+
としているので、一見、大丈夫なようにも見えます。
|
19
|
+
|
20
|
+
問題無いかどうかは、SensorEventListenerが違うスレッドで動作していて、
|
21
|
+
OnSensorChanged()メソッドがそのスレッドで呼ばれているかどうかによります。
|