質問編集履歴

1

コードの追加

2016/04/21 05:55

投稿

peishun
peishun

スコア30

test CHANGED
File without changes
test CHANGED
@@ -17,3 +17,671 @@
17
17
 
18
18
 
19
19
  この現象に、どんな原因もしくはどんな対処法が考えられるでしょうか?
20
+
21
+
22
+
23
+ サービスのソースコードは以下のようになっています。
24
+
25
+
26
+
27
+ ```java
28
+
29
+ package jp.wings.nikkeiibp.napalerm.Process;
30
+
31
+
32
+
33
+ import android.app.Notification;
34
+
35
+ import android.app.NotificationManager;
36
+
37
+ import android.app.PendingIntent;
38
+
39
+ import android.app.Service;
40
+
41
+ import android.content.Context;
42
+
43
+ import android.content.Intent;
44
+
45
+ import android.os.IBinder;
46
+
47
+ import android.support.annotation.Nullable;
48
+
49
+ import android.support.v4.app.NotificationCompat;
50
+
51
+ import android.util.Log;
52
+
53
+
54
+
55
+ import java.util.Timer;
56
+
57
+ import java.util.TimerTask;
58
+
59
+
60
+
61
+ import jp.wings.nikkeiibp.napalerm.Activity.CountDownActivity;
62
+
63
+ import jp.wings.nikkeiibp.napalerm.Common.IntentKeyWord;
64
+
65
+ import jp.wings.nikkeiibp.napalerm.R;
66
+
67
+
68
+
69
+
70
+
71
+ public class TimerService extends Service {
72
+
73
+
74
+
75
+ //タイマー処理を行うオブジェクト
76
+
77
+ private Timer mTimer = null;
78
+
79
+
80
+
81
+ //非同期処理
82
+
83
+ android.os.Handler mHandler = new android.os.Handler();
84
+
85
+
86
+
87
+ /** 設定した秒数 */
88
+
89
+ private int mTimerSeconds;
90
+
91
+
92
+
93
+ @Nullable
94
+
95
+ @Override
96
+
97
+ public IBinder onBind ( Intent intent ) {
98
+
99
+ return null;
100
+
101
+ }
102
+
103
+
104
+
105
+ @Override
106
+
107
+ public void onCreate () {
108
+
109
+ super.onCreate ();
110
+
111
+ }
112
+
113
+
114
+
115
+ @Override
116
+
117
+ public int onStartCommand ( Intent intent, int flags, int startId ) {
118
+
119
+ super.onStartCommand(intent, flags, startId );
120
+
121
+
122
+
123
+ //前の画面で設定した時間(秒)を取得
124
+
125
+ mTimerSeconds = intent.getIntExtra ( IntentKeyWord.SET_SECONDS, 0 );
126
+
127
+ Log.i ( "TimerService getSecond", "" + mTimerSeconds );
128
+
129
+
130
+
131
+ //通知を作成
132
+
133
+ Notification notification = new NotificationCompat.Builder(this)
134
+
135
+ .setContentTitle(getString(R.string.nortification_title))
136
+
137
+ .setContentText(getString(R.string.nortification_text))
138
+
139
+ .setSmallIcon(R.mipmap.ic_launcher)
140
+
141
+ .build();
142
+
143
+
144
+
145
+
146
+
147
+ //通知削除不可にする
148
+
149
+ notification.flags = Notification.FLAG_ONGOING_EVENT;
150
+
151
+
152
+
153
+ //通知マネージャー取得
154
+
155
+ NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
156
+
157
+
158
+
159
+ //通知識別ID
160
+
161
+ final int NUM = 1;
162
+
163
+
164
+
165
+ manager.notify(NUM, notification);
166
+
167
+
168
+
169
+ startForeground(NUM,notification);
170
+
171
+
172
+
173
+ // タイマーの設定 1秒毎にループ
174
+
175
+ mTimer = new Timer (true);
176
+
177
+ mTimer.schedule ( new TimerTask () {
178
+
179
+ @Override
180
+
181
+ public void run () {
182
+
183
+ mHandler.post ( new Runnable () {
184
+
185
+ public void run () {
186
+
187
+ //カウントダウン
188
+
189
+ mTimerSeconds--;
190
+
191
+ //レシーバーに通知
192
+
193
+ sendBroadCast ();
194
+
195
+ }
196
+
197
+ } );
198
+
199
+ }
200
+
201
+ }, 1000, 1000 );
202
+
203
+
204
+
205
+
206
+
207
+
208
+
209
+ return START_STICKY;
210
+
211
+
212
+
213
+ }
214
+
215
+
216
+
217
+ @Override
218
+
219
+ public void onDestroy () {
220
+
221
+ super.onDestroy ();
222
+
223
+ // タイマー停止
224
+
225
+ if( mTimer != null ){
226
+
227
+ mTimer.cancel();
228
+
229
+ mTimer = null;
230
+
231
+ }
232
+
233
+ }
234
+
235
+
236
+
237
+ /**
238
+
239
+ * レシーバーに値を渡す
240
+
241
+ */
242
+
243
+ protected void sendBroadCast() {
244
+
245
+
246
+
247
+ Intent broadcastIntent = new Intent();
248
+
249
+ broadcastIntent.putExtra(IntentKeyWord.SET_SECONDS, mTimerSeconds);
250
+
251
+ Log.i ( "sendBroadCast setSecond", "" + mTimerSeconds );
252
+
253
+
254
+
255
+ broadcastIntent.setAction("UPDATE_ACTION");
256
+
257
+ getBaseContext().sendBroadcast(broadcastIntent);
258
+
259
+ }
260
+
261
+ }
262
+
263
+
264
+
265
+ ```
266
+
267
+
268
+
269
+ レーシーバは以下のようになっています
270
+
271
+ ```java
272
+
273
+ package jp.wings.nikkeiibp.napalerm.Process;
274
+
275
+
276
+
277
+ import android.content.BroadcastReceiver;
278
+
279
+ import android.content.Context;
280
+
281
+ import android.content.Intent;
282
+
283
+ import android.os.Bundle;
284
+
285
+ import android.os.Handler;
286
+
287
+ import android.os.Message;
288
+
289
+ import android.util.Log;
290
+
291
+
292
+
293
+ import jp.wings.nikkeiibp.napalerm.Common.IntentKeyWord;
294
+
295
+
296
+
297
+ public class TimerReceiver extends BroadcastReceiver{
298
+
299
+
300
+
301
+ /** サービスから値を受け取ったときに動かす非同期処理 */
302
+
303
+ public static Handler handler;
304
+
305
+
306
+
307
+ @Override
308
+
309
+ public void onReceive ( Context context, Intent intent ) {
310
+
311
+
312
+
313
+ Bundle getbundle = intent.getExtras ();
314
+
315
+ int seconds = getbundle.getInt ( IntentKeyWord.SET_SECONDS );
316
+
317
+ Log.i ( "TimerReceiver getSecond", "" + seconds );
318
+
319
+
320
+
321
+ if(handler !=null){
322
+
323
+
324
+
325
+ Message msg = new Message();
326
+
327
+
328
+
329
+ Bundle setbundle = new Bundle();
330
+
331
+
332
+
333
+ setbundle.putInt ( IntentKeyWord.SET_SECONDS, seconds);
334
+
335
+ Log.i ( "TimerReceiver setSecond", "" + seconds );
336
+
337
+
338
+
339
+ msg.setData(setbundle);
340
+
341
+
342
+
343
+ handler.sendMessage(msg);
344
+
345
+ }
346
+
347
+ }
348
+
349
+
350
+
351
+ /**
352
+
353
+ * 値受け取り時に実行する非同期処理の登録
354
+
355
+ */
356
+
357
+ public void registerHandler(Handler handler) {
358
+
359
+ this.handler = handler;
360
+
361
+ }
362
+
363
+ }
364
+
365
+ ```
366
+
367
+
368
+
369
+ カウントダウンを表示するアクティビティは以下の通りになっています
370
+
371
+
372
+
373
+ ```java
374
+
375
+ package jp.wings.nikkeiibp.napalerm.Activity;
376
+
377
+
378
+
379
+ import android.annotation.TargetApi;
380
+
381
+ import android.app.Activity;
382
+
383
+ import android.app.Service;
384
+
385
+ import android.content.DialogInterface;
386
+
387
+ import android.content.Intent;
388
+
389
+ import android.content.IntentFilter;
390
+
391
+ import android.graphics.Color;
392
+
393
+ import android.graphics.Typeface;
394
+
395
+ import android.media.AudioAttributes;
396
+
397
+ import android.media.AudioManager;
398
+
399
+ import android.media.MediaPlayer;
400
+
401
+ import android.media.SoundPool;
402
+
403
+ import android.os.Build;
404
+
405
+ import android.os.Bundle;
406
+
407
+ import android.os.Handler;
408
+
409
+ import android.os.Message;
410
+
411
+ import android.support.annotation.RawRes;
412
+
413
+ import android.support.v4.app.NotificationManagerCompat;
414
+
415
+ import android.support.v7.app.AlertDialog;
416
+
417
+ import android.support.v7.app.NotificationCompat;
418
+
419
+ import android.util.Log;
420
+
421
+ import android.view.KeyEvent;
422
+
423
+ import android.view.View;
424
+
425
+ import android.view.WindowManager;
426
+
427
+ import android.widget.TextView;
428
+
429
+ import android.widget.Toast;
430
+
431
+
432
+
433
+ import com.google.android.gms.ads.AdRequest;
434
+
435
+ import com.google.android.gms.ads.AdView;
436
+
437
+
438
+
439
+ import jp.wings.nikkeiibp.napalerm.Common.IntentKeyWord;
440
+
441
+ import jp.wings.nikkeiibp.napalerm.Process.TimerReceiver;
442
+
443
+ import jp.wings.nikkeiibp.napalerm.Process.TimerService;
444
+
445
+ import jp.wings.nikkeiibp.napalerm.R;
446
+
447
+
448
+
449
+ public class CountDownActivity extends Activity {
450
+
451
+
452
+
453
+ /** サービスから値を受け取るレシーバー */
454
+
455
+ private TimerReceiver mTimerReceiver;
456
+
457
+
458
+
459
+ /** 暗黙的インテントの値を受け取る際のフィルター */
460
+
461
+ private IntentFilter mIntentFilter;
462
+
463
+
464
+
465
+ /** カウントダウン表示用テキスト */
466
+
467
+ private TextView mCountText;
468
+
469
+
470
+
471
+ /** 時間をカウントダウンするサービス */
472
+
473
+ private Intent mService;
474
+
475
+
476
+
477
+ @Override
478
+
479
+ protected void onCreate ( Bundle savedInstanceState ) {
480
+
481
+ super.onCreate ( savedInstanceState );
482
+
483
+ setContentView ( R.layout.activity_count_down );
484
+
485
+
486
+
487
+ mCountText = (TextView )findViewById( R.id.countDownText);
488
+
489
+
490
+
491
+ //インテントから値を取得
492
+
493
+ mService = new Intent(this , TimerService.class);
494
+
495
+ int time = getIntent ().getIntExtra ( IntentKeyWord.SET_SECONDS, 0 );
496
+
497
+
498
+
499
+ //時分秒を計算
500
+
501
+ final int hour = time / 3600;
502
+
503
+ final int minute = ( time % 3600 ) / 60;
504
+
505
+ final int second = ( time % 3600 ) % 60;
506
+
507
+
508
+
509
+ //初期秒数をテキストに表示
510
+
511
+ setCountText(hour,minute,second);
512
+
513
+
514
+
515
+ //サービスに秒数をセット
516
+
517
+ mService.putExtra ( IntentKeyWord.SET_SECONDS, time );
518
+
519
+ Log.i ( "service setSecond", "" + time );
520
+
521
+
522
+
523
+ //タイマーサービス開始
524
+
525
+ startService ( mService );
526
+
527
+
528
+
529
+ //サービスのレシーバーをセット
530
+
531
+ setReceiver ();
532
+
533
+
534
+
535
+ }
536
+
537
+
538
+
539
+ @Override
540
+
541
+ protected void onDestroy() {
542
+
543
+ super.onDestroy();
544
+
545
+ stopService(mService);
546
+
547
+ }
548
+
549
+
550
+
551
+ /**
552
+
553
+ * レシーバーをセット
554
+
555
+ * レシーバーはサービスから値を受け取るもの
556
+
557
+ */
558
+
559
+ private void setReceiver(){
560
+
561
+ mTimerReceiver = new TimerReceiver ();
562
+
563
+ mIntentFilter = new IntentFilter ();
564
+
565
+ mIntentFilter.addAction ( "UPDATE_ACTION" );
566
+
567
+ registerReceiver ( mTimerReceiver, mIntentFilter );
568
+
569
+
570
+
571
+ mTimerReceiver.registerHandler ( updateHandler );
572
+
573
+ }
574
+
575
+
576
+
577
+ /**
578
+
579
+ * サービスから値を受け取った際の更新処理
580
+
581
+ */
582
+
583
+ private Handler updateHandler = new Handler() {
584
+
585
+ @Override
586
+
587
+ public void handleMessage(Message msg) {
588
+
589
+
590
+
591
+ Bundle bundle = msg.getData ();
592
+
593
+ int time = bundle.getInt ( IntentKeyWord.SET_SECONDS );
594
+
595
+
596
+
597
+ //カウントダウンが完了した
598
+
599
+ if(time == 0){
600
+
601
+
602
+
603
+ //サービス終了
604
+
605
+ stopService(mService);
606
+
607
+
608
+
609
+ //カウントダウンの文字を00:00:00にする
610
+
611
+ setCountText(0,0,0);
612
+
613
+
614
+
615
+ //文字の色を変える
616
+
617
+ mCountText.setTextColor(Color.RED);
618
+
619
+
620
+
621
+ //文字を太くする
622
+
623
+ mCountText.setTypeface(Typeface.DEFAULT_BOLD);
624
+
625
+
626
+
627
+ //次のアクティビティへ
628
+
629
+ goNotificaionAlert();
630
+
631
+
632
+
633
+ }else{
634
+
635
+
636
+
637
+ final int hour = time / 3600;
638
+
639
+ final int minute = ( time % 3600 ) / 60;
640
+
641
+ final int second = ( time % 3600 ) % 60;
642
+
643
+
644
+
645
+ //カウントダウン表示更新
646
+
647
+ setCountText(hour,minute,second);
648
+
649
+
650
+
651
+ }
652
+
653
+ }
654
+
655
+ };
656
+
657
+ }
658
+
659
+ ```
660
+
661
+
662
+
663
+ ちなみにマニュフェストファイルには以下の記述をしております。
664
+
665
+ ```xml
666
+
667
+ <application
668
+
669
+
670
+
671
+ ・・・・・・・・・・・
672
+
673
+
674
+
675
+ <service android:name=".Process.TimerService" />
676
+
677
+ <receiver android:name=".Process.TimerReceiver" />
678
+
679
+
680
+
681
+
682
+
683
+ ・・・・・・・・・・
684
+
685
+ </application>
686
+
687
+ ```