質問するログイン新規登録

質問編集履歴

3

修正

2015/11/28 03:37

投稿

souri-t
souri-t

スコア16

title CHANGED
File without changes
body CHANGED
@@ -69,7 +69,7 @@
69
69
 
70
70
  @Override
71
71
  public void onCreate() {
72
- Log.v("test, "onCreate");
72
+ Log.v("test", "onCreate");
73
73
  }
74
74
 
75
75
  @Override

2

誤りを修正

2015/11/28 03:37

投稿

souri-t
souri-t

スコア16

title CHANGED
File without changes
body CHANGED
@@ -10,7 +10,6 @@
10
10
  ↓アクティビティ
11
11
  public class MainActivity extends Activity {
12
12
 
13
- private NotificationManager nm;
14
13
  private ServiceConnection connect = new ServiceConnection() {
15
14
  @Override
16
15
  public void onServiceConnected(ComponentName name, IBinder service) {

1

ソースコードを追加

2015/11/27 18:15

投稿

souri-t
souri-t

スコア16

title CHANGED
File without changes
body CHANGED
@@ -4,4 +4,115 @@
4
4
 
5
5
  バインドしたときだけ失敗する理由がよく分かりません。
6
6
  2ヶ月ほど解決策が見つからず困っています。
7
- よろしくお願いします。
7
+ よろしくお願いします。
8
+
9
+ ```ここに言語を入力
10
+ ↓アクティビティ
11
+ public class MainActivity extends Activity {
12
+
13
+ private NotificationManager nm;
14
+ private ServiceConnection connect = new ServiceConnection() {
15
+ @Override
16
+ public void onServiceConnected(ComponentName name, IBinder service) {
17
+ }
18
+ @Override
19
+ public void onServiceDisconnected(ComponentName name) {
20
+ }
21
+ }
22
+
23
+ @Override
24
+ protected void onCreate(Bundle bundle) {
25
+ super.onCreate(bundle);
26
+ setContentView(R.layout.activity_main);
27
+
28
+ Button bindBtn = (Button)findViewById(R.id.button);
29
+ startBtn.setOnClickListener(new BtnClickListener());
30
+
31
+ Button unbindBtn = (Button)findViewById(R.id.button2);
32
+ stopBtn.setOnClickListener(new BtnClickListener());
33
+ };
34
+ private class BtnClickListener implements View.OnClickListener {
35
+ public void onClick(View v) {
36
+
37
+ Intent service;
38
+ service = new Intent(MainActivity.this, NotificationService.class);
39
+ switch (v.getId()) {
40
+ case R.id.button:
41
+ service = new Intent(MainActivity.this,NotificationService.class);
42
+ bindService(service, connect, BIND_AUTO_CREATE);
43
+ break;
44
+ case R.id.button2:
45
+ service = new Intent(MainActivity.this,NotificationService.class);
46
+ unbindService(connect)
47
+ break;
48
+ }
49
+ }
50
+ }
51
+
52
+ ```
53
+
54
+ ```ここに言語を入力
55
+ ↓サービスクラス
56
+ public class NotificationService extends NotificationListenerService {
57
+ private final IBinder mBinder = new MyServiceLocalBinder();
58
+ @Override
59
+ public int onStartCommand(Intent intent, int flags, int startId) {
60
+ Log.v("test", "onStartCommand");
61
+ return super.onStartCommand(intent, flags, startId);
62
+ }
63
+
64
+ //通知が発生するとコールされる
65
+ @Override
66
+ public void onNotificationPosted(StatusBarNotification sbn) {
67
+ super.onNotificationPosted(sbn);
68
+ Log.v("test", "onNotificationPosted");
69
+ }
70
+
71
+ @Override
72
+ public void onCreate() {
73
+ Log.v("test, "onCreate");
74
+ }
75
+
76
+ @Override
77
+ public IBinder onBind(Intent intent) {
78
+ Log.v("test", "onBind");
79
+ return null;
80
+ }
81
+ @Override
82
+ public boolean onUnbind(Intent intent) {
83
+ Log.v("test", "onUnbind");
84
+ return super.onUnbind(intent);
85
+ }
86
+ @Override
87
+ public void onRebind(Intent intent){
88
+ Log.v("test", "onRebind");
89
+ }
90
+ @Override
91
+ public void onDestroy() {
92
+ Log.v("test", "onDestroy");
93
+ }
94
+ }
95
+ ```
96
+
97
+ ```ここに言語を入力
98
+ マニフェストに以下を追加
99
+ <service android:name=".NotificationService"
100
+ android:label="@string/app_name"
101
+ android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
102
+ <intent-filter>
103
+ <action android:name=
104
+ "android.service.notification.NotificationListenerService" />
105
+ </intent-filter>
106
+ </service>
107
+ ```
108
+
109
+ 操作の順番
110
+ bindService→通知を発生させる→unbindService
111
+
112
+ 実際の動作ログ
113
+ test﹕ onCreate
114
+ test﹕ onBind
115
+ test﹕ onUnbind
116
+ test﹕ onDestroy
117
+
118
+ ↑なぜかonNotificationPostedがコールされない