疑問
Androidアプリ開発の教科書(翔泳社)の第13章の「バックグラウンド処理と通知機能」というところで、通知機能のサンプルコードとその解説に疑問を持ちました。
通知を扱うにはチャンネルを生成する。そして、その後に通知を出すためにビルダーとマネージャーを使う。
という感じの流れだったのですが、そもそもチャンネルを生成する理由が分かりません。。チャンネル生成のコード部分を書かなくても通知はビルダーとマネージャーで生成できているみたいですし、エミュレータ側でも全く違いが分かりませんでした。
また、結局通知チャンネルを調べてもよく理解することができませんでした。。
質問をまとめると、
- なぜ通知チャンネルを作らなくてはいけないのでしょうか?
- 通知チャンネルとはユーザが設定するもので、プログラマが書く必要があるのでしょうか?
- 通知チャンネルとは結局なんなのでしょうか?
Androidはまだ初めてばかりで見慣れないことも多いのですが、どうかよろしくお願いします。
コメントアウトしている部分が該当箇所で、参考書「Androidアプリ開発の教科書」のサンプルコードとそのURLを載せておきます。ただし、imort文は省略しています。
URL:http://www.wings.msn.to/index.php/-/A-03/978-7981-5202-8/
の中のServiceSampleというフォルダです。
該当のソースコード(SoundManagerService.java)
java
1public class SoundManageService extends Service { 2 3 private MediaPlayer _player; 4 5 @Override 6 public void onCreate(){ 7 8 _player=new MediaPlayer(); 9 //通知チャンネルの作成 10 String id="soundmanagerservice_notification_channel"; 11 String name=getString(R.string.notification_channel_name); 12 int importance=NotificationManager.IMPORTANCE_HIGH; 13 NotificationChannel channel=new NotificationChannel(id,name,importance); 14 channel.enableVibration(true); 15 NotificationManager manager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 16 manager.createNotificationChannel(channel); 17 } 18 19 @Override 20 public int onStartCommand(Intent intent,int flags,int startId){ 21 String mediaFileUriStr="android.resource://"+getPackageName()+"/"+R.raw.creek; 22 Uri mediaFileUri=Uri.parse(mediaFileUriStr); 23 24 try{ 25 _player.setDataSource(SoundManageService.this,mediaFileUri); 26 _player.setOnPreparedListener(new PlayerPreparedListener()); 27 _player.setOnCompletionListener(new PlayerCompletionListener()); 28 _player.prepareAsync(); 29 30 }catch (IOException e){ 31 e.printStackTrace(); 32 } 33 return START_NOT_STICKY; 34 } 35 36 @Override 37 public void onDestroy(){ 38 if (_player.isPlaying()){ 39 _player.stop(); 40 } 41 42 _player.release(); 43 _player=null; 44 } 45 46 @Override 47 public IBinder onBind(Intent intent) { 48 // TODO: Return the communication channel to the service. 49 throw new UnsupportedOperationException("Not yet implemented"); 50 } 51 52 private class PlayerPreparedListener implements MediaPlayer.OnPreparedListener{ 53 54 @Override 55 public void onPrepared(MediaPlayer mp){ 56 mp.start(); 57 } 58 } 59 60 private class PlayerCompletionListener implements MediaPlayer.OnCompletionListener{ 61 62 @Override 63 public void onCompletion(MediaPlayer mp){ 64 //ビルダーとマネージャーを使い通知を出す 65 NotificationCompat.Builder builder=new NotificationCompat.Builder(SoundManageService.this,"soundmanagerservice_notification_channel"); 66 builder.setSmallIcon(android.R.drawable.ic_dialog_info); 67 builder.setContentTitle(getString(R.string.msg_notification_title_finish)); 68 builder.setContentText(getString(R.string.msg_notification_text_finish)); 69 Notification notification=builder.build(); 70 NotificationManager manager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 71 manager.notify(0,notification); 72 73 stopSelf(); 74 } 75 } 76} 77 78
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/12 15:25
2018/11/19 06:51 編集
2018/11/24 09:23