6
6
7
7
使用 ` git ` 命令或者熟悉的 ` git ` 客户端将 Myna 克隆到本地:
8
8
9
- git clone https://github.com/TalkingData/Myna.git
9
+ ``` st
10
+ git clone https://github.com/TalkingData/Myna.git
11
+ ```
10
12
11
13
### Demo App
12
14
@@ -33,84 +35,101 @@ Google 将 Google Play Service 中和用户情景感知相关的服务和功能
33
35
34
36
在应用自定义的 ` Application ` 派生类或者某个 ` Activity ` 的 ` onCreate ` 方法中调用下面的接口进行初始化:
35
37
36
- @Override
37
- public void onCreate() {
38
- super.onCreate();
39
- context = this;
40
- MynaApi.init(this, new MyInitCallback(), new MyCallback(), MynaApi.TALKINGDATA);
41
- }
38
+ ``` java
39
+ @Override
40
+ public void onCreate() {
41
+ super . onCreate();
42
+ context = this ;
43
+ MynaApi . init(this , new MyInitCallback (), new MyCallback (), MynaApi . TALKINGDATA );
44
+ }
45
+ ```
42
46
43
47
初始化的时候,需要传入一个实现了接口 ` MynaInitCallbacks ` 的类的实例作为回调,这样将可以在 Myna 初始化成功或者失败时做不同的处理。接口 ` MynaInitCallbacks ` 的定义为:
44
48
45
- /**
46
- * Define resultCallback methods to handle different initialization results.
47
- */
48
- public interface MynaInitCallback {
49
-
50
- /**
51
- * Called when Myna is successfully initialized.
52
- */
53
- void onSucceeded();
54
-
55
- /**
56
- * Called when Myna failed to initialize.
57
- */
58
- void onFailed(MynaResult error);
59
- }
49
+ ``` java
50
+ /**
51
+ * Define resultCallback methods to handle different initialization results.
52
+ */
53
+ public interface MynaInitCallback {
60
54
55
+ /**
56
+ * Called when Myna is successfully initialized.
57
+ */
58
+ void onSucceeded ();
59
+
60
+ /**
61
+ * Called when Myna failed to initialize.
62
+ */
63
+ void onFailed (MynaResult error );
64
+ }
65
+ ```
61
66
` MynaResultCallback ` 用来返回识别结果:
62
-
63
- public interface MynaResultCallback<R extends MynaResultInterface > {
67
+
68
+ ``` java
69
+ public interface MynaResultCallback <R extends MynaResultInterface > {
64
70
void onResult (@NonNull R var1 );
65
- }
71
+ }
72
+ ```
66
73
67
74
通过下面的接口可以获取 Myna 的初始化状态:
68
75
69
- /**
70
- * Get the status of Myna initialization
71
- */
72
- public static boolean isInitialized()
76
+ ``` java
77
+ /**
78
+ * Get the status of Myna initialization
79
+ */
80
+ public static boolean isInitialized()
81
+ ```
73
82
74
83
#### 开始和停止
75
84
76
85
初始化后,就可以调用 ` start ` 和 ` stop ` 接口接收和停止识别算法的运行并获得识别结果。
77
86
78
- /**
79
- * Stop all background tasks
80
- */
81
- public static void stop(){
82
- MynaHelper.stop();
83
- }
84
-
85
- /**
86
- * Start to recognize
87
- */
88
- public static void start(){
89
- MynaHelper.start();
90
- }
87
+ ``` java
88
+ /**
89
+ * Stop all background tasks
90
+ */
91
+ public static void stop(){
92
+ MynaHelper . stop();
93
+ }
94
+
95
+ /**
96
+ * Start to recognize
97
+ */
98
+ public static void start(){
99
+ MynaHelper . start();
100
+ }
101
+ ```
91
102
92
103
### 使用 Google Awareness API
93
104
94
105
如果希望使用 Google Awareness API 提供的实时行为识别能力,在初始化时:
95
106
96
- MynaApi.init(this, new MyInitCallback(), new MyCallback(), MynaApi.GOOGLE);
107
+ ``` java
108
+ MynaApi . init(this , new MyInitCallback (), new MyCallback (), MynaApi . GOOGLE );
109
+ ```
97
110
98
111
需要注意的是,这是还需要额外进行下面的配置:
99
112
100
113
- 参考 [ Android 开发者需要知道的 Google Awareness API] ( http://mp.weixin.qq.com/s?__biz=MjM5NzQ3NDg0Mg==&mid=2653096725&idx=1&sn=f6686df351aabe957a450c2fa1b01596&mpshare=1&scene=1&srcid=10311xdjSUJveY5gTtuLLQw2#rd ) 文章申请 App key 并创建用于自己应用的 Credential。
101
114
- 在 ` AndroidManifest.xml ` 的 ` application ` 块中添加下面的声明:
102
115
103
- <meta-data
104
- android:name="com.google.android.awareness.API_KEY"
105
- android:value="申请的 App Key"/>
106
-
116
+ ``` xml
117
+ <meta-data
118
+ android : name =" com.google.android.awareness.API_KEY"
119
+ android : value =" 申请的 App Key" />
120
+ ```
121
+
107
122
- 添加下面的 Permission:
108
123
109
- <uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
124
+ ``` xml
125
+ <uses-permission android : name =" com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
126
+ ```
110
127
111
128
- 在应用 module 的 ` build.gradle ` 文件中,添加下面的依赖:
112
129
113
- compile 'com.google.android.gms:play-services-awareness:9.8.0'
130
+ ``` st
131
+ compile 'com.google.android.gms:play-services-awareness:9.8.0'
132
+ ```
114
133
115
134
### 面向数据科学家的接口
116
135
@@ -120,53 +139,65 @@ Google 将 Google Play Service 中和用户情景感知相关的服务和功能
120
139
121
140
#### 添加新的传感器类型订阅:
122
141
123
- /**
124
- * Add a sensor into the chosen sensor list.
125
- * @param sensorType sensorType
126
- */
127
- public synchronized void addSensorType(int sensorType)
142
+ ``` java
143
+ /**
144
+ * Add a sensor into the chosen sensor list.
145
+ * @param sensorType sensorType
146
+ */
147
+ public synchronized void addSensorType(int sensorType)
148
+ ```
128
149
129
150
#### 移除已经订阅的传感器类型:
130
151
131
- /**
132
- * Remove a sensor from the chosen sensor list.
133
- * @param sensorType The type of the sensor to be removed.
134
- */
135
- public synchronized void removeSensorType(int sensorType)
152
+ ``` java
153
+ /**
154
+ * Remove a sensor from the chosen sensor list.
155
+ * @param sensorType The type of the sensor to be removed.
156
+ */
157
+ public synchronized void removeSensorType(int sensorType)
158
+ ```
136
159
137
160
#### 设置采样的间隔时间(反映采样频率,单位毫秒):
138
161
139
- /**
140
- * Set sampling duration.
141
- * @param duration Sampling duration
142
- */
143
- public void setSamplingDuration(int duration)
162
+ ``` java
163
+ /**
164
+ * Set sampling duration.
165
+ * @param duration Sampling duration
166
+ */
167
+ public void setSamplingDuration(int duration)
168
+ ```
144
169
145
170
##### 设置 batch size:
146
171
147
- /**
148
- * Set total count of the data points for each recognition.
149
- * @param pointCount Total count of the data points.
150
- */
151
- public void setSamplingPointCount
172
+ ``` java
173
+ /**
174
+ * Set total count of the data points for each recognition.
175
+ * @param pointCount Total count of the data points.
176
+ */
177
+ public void setSamplingPointCount
178
+ ```
152
179
153
180
Myna 支持同时配置多个识别器(MynaRecognizerInterface 派生类的实例),也就是 ` Recognizer ` ,而且,在运行时,可以随时移除已经添加的某个 recognizer config:
154
181
155
- /**
156
- * Add a new recognition configuration to be executed later
157
- */
158
- public static void addRecognizer(MynaRecognizerInterface recognizer)
182
+ ``` java
183
+ /**
184
+ * Add a new recognition configuration to be executed later
185
+ */
186
+ public static void addRecognizer(MynaRecognizerInterface recognizer)
159
187
160
- /**
161
- * Remove a new recognition configuration to be executed later
162
- */
163
- public static void removeRecognizer(int configId)
188
+ /**
189
+ * Remove a new recognition configuration to be executed later
190
+ */
191
+ public static void removeRecognizer(int configId)
192
+ ```
164
193
165
194
如果想完全重置 Myna 运行环境,需要运行:
166
195
167
- /**
168
- * Clean Myna env
169
- */
170
- public static void cleanUp(Context ctx)
196
+ ``` java
197
+ /**
198
+ * Clean Myna env
199
+ */
200
+ public static void cleanUp(Context ctx)
201
+ ```
171
202
172
203
清理环境后,再次启动 Myna 需要重新初始化。
0 commit comments