回答編集履歴
1
ソースコードの記述場所が間違っていたため
answer
CHANGED
@@ -1,87 +1,83 @@
|
|
1
1
|
ハンドラを用いて解決することができました。
|
2
2
|
参考までにソースを掲載します。
|
3
|
+
|
3
4
|
```java
|
4
|
-
```
|
5
5
|
import android.app.Activity;
|
6
|
-
import android.os.Bundle;
|
6
|
+
import android.os.Bundle;
|
7
|
-
import android.os.Handler;
|
7
|
+
import android.os.Handler;
|
8
|
-
import android.util.Log;
|
8
|
+
import android.util.Log;
|
9
|
-
import android.widget.ArrayAdapter;
|
9
|
+
import android.widget.ArrayAdapter;
|
10
|
-
import android.widget.TextView;
|
10
|
+
import android.widget.TextView;
|
11
11
|
|
12
12
|
import com.mlkcca.client.DataElement;
|
13
|
-
import com.mlkcca.client.DataStore;
|
13
|
+
import com.mlkcca.client.DataStore;
|
14
|
-
import com.mlkcca.client.DataStoreEventListener;
|
14
|
+
import com.mlkcca.client.DataStoreEventListener;
|
15
|
-
import com.mlkcca.client.MilkCocoa;
|
15
|
+
import com.mlkcca.client.MilkCocoa;
|
16
16
|
|
17
17
|
public class MainActivity extends Activity implements DataStoreEventListener {
|
18
|
-
|
18
|
+
String temp;
|
19
|
-
|
19
|
+
int temp0;
|
20
|
-
|
20
|
+
String temp2;
|
21
|
-
|
21
|
+
float temp1;
|
22
|
-
|
22
|
+
TextView t3;
|
23
|
-
|
23
|
+
private MilkCocoa m_milkCocoa;
|
24
|
-
|
24
|
+
private DataStore m_dataStore;
|
25
|
-
|
25
|
+
private Handler handler = new Handler();
|
26
|
-
|
26
|
+
private ArrayAdapter<String> adapter;
|
27
27
|
|
28
|
-
|
28
|
+
@Override
|
29
|
-
|
29
|
+
protected void onCreate(Bundle savedInstanceState) {
|
30
|
-
|
30
|
+
super.onCreate(savedInstanceState);
|
31
|
-
|
31
|
+
setContentView(R.layout.activity_main);
|
32
|
-
|
32
|
+
t3 = (TextView) findViewById(R.id.text3);
|
33
|
-
|
33
|
+
m_milkCocoa = new MilkCocoa("MY_MILKCOCOA_APP_ID");
|
34
|
-
|
34
|
+
m_dataStore = m_milkCocoa.dataStore("DATASTORE NAME");
|
35
|
-
|
35
|
+
m_dataStore.addDataStoreEventListener(this);
|
36
|
-
|
36
|
+
m_dataStore.on("push");
|
37
|
-
|
37
|
+
}
|
38
38
|
|
39
|
+
@Override
|
40
|
+
public void onPushed(DataElement dataElement) {
|
41
|
+
final DataElement pushed = dataElement;
|
42
|
+
new Thread(new Runnable() {
|
43
|
+
@Override
|
44
|
+
public void run() {
|
45
|
+
handler.post(new Runnable() {
|
46
|
+
@Override
|
47
|
+
public void run() {
|
48
|
+
String temp = pushed.getValue("temp");
|
49
|
+
int temp0 = Integer.parseInt(temp);
|
50
|
+
temp1 = (float) temp0 / 10;
|
51
|
+
Log.i("MC", "温度は" + temp1 + "℃です");
|
52
|
+
temp2 = "温度は" + temp1 + "℃です";
|
53
|
+
t3.setText(temp2);
|
54
|
+
}
|
55
|
+
});
|
56
|
+
}
|
39
57
|
|
40
|
-
@Override
|
41
|
-
public void onPushed(DataElement dataElement) {
|
42
|
-
final DataElement pushed = dataElement;
|
43
|
-
new Thread(new Runnable() {
|
44
|
-
@Override
|
45
|
-
public void run() {
|
46
|
-
handler.post(new Runnable() {
|
47
|
-
@Override
|
48
|
-
public void run() {
|
49
|
-
String temp = pushed.getValue("temp");
|
50
|
-
int temp0 = Integer.parseInt(temp);
|
51
|
-
temp1 = (float) temp0 / 10;
|
52
|
-
Log.i("MC", "温度は" + temp1 + "℃です");
|
53
|
-
temp2 = "温度は" + temp1 + "℃です";
|
54
|
-
|
58
|
+
}).start();
|
55
|
-
|
59
|
+
}
|
56
|
-
});
|
57
|
-
}
|
58
60
|
|
59
|
-
|
61
|
+
@Override
|
60
|
-
|
62
|
+
public void onSetted(DataElement dataElement) {
|
61
63
|
|
64
|
+
}
|
62
65
|
|
63
|
-
|
66
|
+
@Override
|
64
|
-
|
67
|
+
public void onSended(DataElement dataElement) {
|
65
68
|
|
66
|
-
|
69
|
+
}
|
67
70
|
|
71
|
+
@Override
|
72
|
+
public void onRemoved(DataElement dataElement) {
|
68
73
|
|
69
|
-
|
74
|
+
}
|
70
|
-
public void onSended(DataElement dataElement) {
|
71
75
|
|
76
|
+
@Override
|
77
|
+
public void onPause() {
|
78
|
+
super.onPause();
|
79
|
+
finish();
|
80
|
+
android.os.Process.killProcess(android.os.Process.myPid());
|
72
|
-
|
81
|
+
}
|
73
|
-
|
74
|
-
|
75
|
-
@Override
|
76
|
-
public void onRemoved(DataElement dataElement) {
|
77
|
-
|
78
|
-
|
82
|
+
}
|
79
|
-
|
80
|
-
|
81
|
-
|
83
|
+
```
|
82
|
-
public void onPause() {
|
83
|
-
super.onPause();
|
84
|
-
finish();
|
85
|
-
android.os.Process.killProcess(android.os.Process.myPid());
|
86
|
-
}
|
87
|
-
}
|