質問編集履歴
3
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -140,7 +140,7 @@
|
|
140
140
|
|
141
141
|
public void onCreate() {
|
142
142
|
|
143
|
-
Log.v("test, "onCreate");
|
143
|
+
Log.v("test", "onCreate");
|
144
144
|
|
145
145
|
}
|
146
146
|
|
2
誤りを修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -22,8 +22,6 @@
|
|
22
22
|
|
23
23
|
|
24
24
|
|
25
|
-
private NotificationManager nm;
|
26
|
-
|
27
25
|
private ServiceConnection connect = new ServiceConnection() {
|
28
26
|
|
29
27
|
@Override
|
1
ソースコードを追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -11,3 +11,225 @@
|
|
11
11
|
2ヶ月ほど解決策が見つからず困っています。
|
12
12
|
|
13
13
|
よろしくお願いします。
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
```ここに言語を入力
|
18
|
+
|
19
|
+
↓アクティビティ
|
20
|
+
|
21
|
+
public class MainActivity extends Activity {
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
private NotificationManager nm;
|
26
|
+
|
27
|
+
private ServiceConnection connect = new ServiceConnection() {
|
28
|
+
|
29
|
+
@Override
|
30
|
+
|
31
|
+
public void onServiceConnected(ComponentName name, IBinder service) {
|
32
|
+
|
33
|
+
}
|
34
|
+
|
35
|
+
@Override
|
36
|
+
|
37
|
+
public void onServiceDisconnected(ComponentName name) {
|
38
|
+
|
39
|
+
}
|
40
|
+
|
41
|
+
}
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
@Override
|
46
|
+
|
47
|
+
protected void onCreate(Bundle bundle) {
|
48
|
+
|
49
|
+
super.onCreate(bundle);
|
50
|
+
|
51
|
+
setContentView(R.layout.activity_main);
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
Button bindBtn = (Button)findViewById(R.id.button);
|
56
|
+
|
57
|
+
startBtn.setOnClickListener(new BtnClickListener());
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
Button unbindBtn = (Button)findViewById(R.id.button2);
|
62
|
+
|
63
|
+
stopBtn.setOnClickListener(new BtnClickListener());
|
64
|
+
|
65
|
+
};
|
66
|
+
|
67
|
+
private class BtnClickListener implements View.OnClickListener {
|
68
|
+
|
69
|
+
public void onClick(View v) {
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
Intent service;
|
74
|
+
|
75
|
+
service = new Intent(MainActivity.this, NotificationService.class);
|
76
|
+
|
77
|
+
switch (v.getId()) {
|
78
|
+
|
79
|
+
case R.id.button:
|
80
|
+
|
81
|
+
service = new Intent(MainActivity.this,NotificationService.class);
|
82
|
+
|
83
|
+
bindService(service, connect, BIND_AUTO_CREATE);
|
84
|
+
|
85
|
+
break;
|
86
|
+
|
87
|
+
case R.id.button2:
|
88
|
+
|
89
|
+
service = new Intent(MainActivity.this,NotificationService.class);
|
90
|
+
|
91
|
+
unbindService(connect)
|
92
|
+
|
93
|
+
break;
|
94
|
+
|
95
|
+
}
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
}
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
```
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
```ここに言語を入力
|
108
|
+
|
109
|
+
↓サービスクラス
|
110
|
+
|
111
|
+
public class NotificationService extends NotificationListenerService {
|
112
|
+
|
113
|
+
private final IBinder mBinder = new MyServiceLocalBinder();
|
114
|
+
|
115
|
+
@Override
|
116
|
+
|
117
|
+
public int onStartCommand(Intent intent, int flags, int startId) {
|
118
|
+
|
119
|
+
Log.v("test", "onStartCommand");
|
120
|
+
|
121
|
+
return super.onStartCommand(intent, flags, startId);
|
122
|
+
|
123
|
+
}
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
//通知が発生するとコールされる
|
128
|
+
|
129
|
+
@Override
|
130
|
+
|
131
|
+
public void onNotificationPosted(StatusBarNotification sbn) {
|
132
|
+
|
133
|
+
super.onNotificationPosted(sbn);
|
134
|
+
|
135
|
+
Log.v("test", "onNotificationPosted");
|
136
|
+
|
137
|
+
}
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
@Override
|
142
|
+
|
143
|
+
public void onCreate() {
|
144
|
+
|
145
|
+
Log.v("test, "onCreate");
|
146
|
+
|
147
|
+
}
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
@Override
|
152
|
+
|
153
|
+
public IBinder onBind(Intent intent) {
|
154
|
+
|
155
|
+
Log.v("test", "onBind");
|
156
|
+
|
157
|
+
return null;
|
158
|
+
|
159
|
+
}
|
160
|
+
|
161
|
+
@Override
|
162
|
+
|
163
|
+
public boolean onUnbind(Intent intent) {
|
164
|
+
|
165
|
+
Log.v("test", "onUnbind");
|
166
|
+
|
167
|
+
return super.onUnbind(intent);
|
168
|
+
|
169
|
+
}
|
170
|
+
|
171
|
+
@Override
|
172
|
+
|
173
|
+
public void onRebind(Intent intent){
|
174
|
+
|
175
|
+
Log.v("test", "onRebind");
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
@Override
|
180
|
+
|
181
|
+
public void onDestroy() {
|
182
|
+
|
183
|
+
Log.v("test", "onDestroy");
|
184
|
+
|
185
|
+
}
|
186
|
+
|
187
|
+
}
|
188
|
+
|
189
|
+
```
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
```ここに言語を入力
|
194
|
+
|
195
|
+
マニフェストに以下を追加
|
196
|
+
|
197
|
+
<service android:name=".NotificationService"
|
198
|
+
|
199
|
+
android:label="@string/app_name"
|
200
|
+
|
201
|
+
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
|
202
|
+
|
203
|
+
<intent-filter>
|
204
|
+
|
205
|
+
<action android:name=
|
206
|
+
|
207
|
+
"android.service.notification.NotificationListenerService" />
|
208
|
+
|
209
|
+
</intent-filter>
|
210
|
+
|
211
|
+
</service>
|
212
|
+
|
213
|
+
```
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
操作の順番
|
218
|
+
|
219
|
+
bindService→通知を発生させる→unbindService
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
実際の動作ログ
|
224
|
+
|
225
|
+
test﹕ onCreate
|
226
|
+
|
227
|
+
test﹕ onBind
|
228
|
+
|
229
|
+
test﹕ onUnbind
|
230
|
+
|
231
|
+
test﹕ onDestroy
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
↑なぜかonNotificationPostedがコールされない
|