質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

Android Emulator

Android EmulatorはアンドロイドのOSで起動しているアンドロイドのデバイスの機能をシミュレートするソフトウェアです。Emulatorは開発者に複数の違う設定を持ったデバイスを必要とすることなくアプリケーションを開発しテストすることが可能になります。

Q&A

解決済

1回答

1994閲覧

AlarmManagerで設定時間に音(SoundPool)が鳴らない

kakashi55

総合スコア25

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

Android Emulator

Android EmulatorはアンドロイドのOSで起動しているアンドロイドのデバイスの機能をシミュレートするソフトウェアです。Emulatorは開発者に複数の違う設定を持ったデバイスを必要とすることなくアプリケーションを開発しテストすることが可能になります。

0グッド

0クリップ

投稿2021/09/29 13:42

android studioでkotlinにてアラーム機能を実装しようとしております。

〇やりたいこと
設定時間に通知(Notification)と音(SoundPool)を出したい
以下コードは5s後に通知されるように書いています

〇困っていること
指定時間にSoundPoolの処理を通過しているものの音が鳴らない

MainActiviy

1class MainActivity : FragmentActivity(), TimePickerDialog.OnTimeSetListener { 2 3 private lateinit var binding: ActivityMainBinding 4 private lateinit var soundManager: SoundManager 5 6 override fun onCreate(savedInstanceState: Bundle?) { 7 super.onCreate(savedInstanceState) 8 binding = ActivityMainBinding.inflate(layoutInflater) 9 setContentView(binding.root) 10 11 soundManager = SoundManager(this) 12 13 binding.btnTest.setOnClickListener { 14 alarmSet() 15 } 16 } 17 18 private fun alarmSet() { 19 val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager 20 val calendar = Calendar.getInstance() 21 22 var year = calendar.get(Calendar.YEAR) 23 var month = calendar.get(Calendar.MONTH) 24 var day = calendar.get(Calendar.DAY_OF_MONTH) 25 26 val intent = Intent(this, MyReceiver::class.java) 27 val pendingIntent = PendingIntent.getBroadcast( 28 this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT 29 ) 30 31 calendar.add(Calendar.SECOND, 5) 32 alarmManager.setExact( 33 AlarmManager.RTC_WAKEUP, 34 //calendar.getTimeInMillis(), 35 calendar.timeInMillis, 36 pendingIntent 37 ) 38 } 39 40 fun playOneReady() { 41 soundManager.playOne() 42 }

MyReceiver

1class MyReceiver: BroadcastReceiver() { 2 override fun onReceive(context: Context?, intent: Intent?) { 3 if (context != null) { 4 MyNotification.sendNotification(context) 5 } 6 } 7}

MyNotification

1class MyNotification(context: Context) { 2 3 companion object { 4 private const val NOTIFICATION_CHANNEL_ID = "com.example.sample_alarm_manager_channel_id" 5 private const val NOTIFICATION_CHANNEL_NAME = "Time for !!" 6 private const val NOTIFICATION_CHANNEL_DESCRIPTION = "Go To The" 7 private const val NOTIFICATION_TITLE = "sample alarm manager" 8 private const val NOTIFICATION_MESSAGE = "test message." 9 10 fun sendNotification(context: Context) { 11 Log.d("debug", "MyNotification sendNotification") 12 13 val channelId = Companion.NOTIFICATION_CHANNEL_ID 14 val channelName = Companion.NOTIFICATION_CHANNEL_NAME 15 val channelDescription = Companion.NOTIFICATION_CHANNEL_DESCRIPTION 16 17 //Android 8.0 以上ではアプリの通知チャンネルを登録することが必要。 18 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 19 val importance = NotificationManager.IMPORTANCE_DEFAULT 20 val channel = NotificationChannel(channelId, channelName, importance).apply { 21 description = channelDescription 22 } 23 val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager 24 manager.createNotificationChannel(channel) 25 } 26 27 //通知をシステムに登録しています。 28 val builder = NotificationCompat.Builder(context, channelId).apply { 29 setSmallIcon(R.drawable.ic_baseline_info_24) 30 setContentTitle(Companion.NOTIFICATION_TITLE) 31 setContentText(Companion.NOTIFICATION_MESSAGE) 32 priority = NotificationCompat.PRIORITY_DEFAULT 33 } 34 35 val id = 0 36 NotificationManagerCompat.from(context).notify(id, builder.build()) 37 38 val mainActivity = MainActivity() 39 mainActivity.playOneReady() 40 } 41 } 42 43}

soundManager

1class SoundManager(context: Context) { 2 3 private var soundPool: SoundPool 4 private var soundOne = 0 5 private var streamId2 = 0 6 7 init { 8 //soundPool設定 9 val audioAttributes = AudioAttributes.Builder() 10 .setUsage(AudioAttributes.USAGE_GAME) 11 .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH) 12 .build() 13 14 soundPool = SoundPool.Builder() 15 .setAudioAttributes(audioAttributes) 16 .setMaxStreams(1) 17 .build() 18 19 soundOne = soundPool.load(context, R.raw.bivlla, 1) 20 Log.d("debug", "SoundManager init") 21 } 22 23 fun playOne() { 24 cancelOne() 25 streamId2 = soundPool.play(soundOne, 1.0f, 1.0f, 0, -1, 1.0f) 26 Log.d("debug", "sound ringing") 27 } 28 29 fun cancelOne() { 30 if(streamId2 != 0) { //fun playOneを通るとここを通る(音が鳴るとstreamId2= 1 が入る) 31 soundPool.stop(streamId2) 32 streamId2 = 0 33 } 34 } 35}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

以下コード追加にて解説いたしました。

MainActivity

1 2class MainActivity{ 3 private val myReceiver = object: BroadcastReceiver() { 4 override fun onReceive(context: Context?, intent: Intent?) { 5 Log.d("debug", "戻ってきました") 6 } 7 } 8 9 override fun onCreate(savedInstanceState: Bundle?) { 10 //BroadcastReceiverの処理が終わったときの登録 BroadcastReceiverを登録する 11 LocalBroadcastManager.getInstance(this).registerReceiver( 12 myReceiver, 13 IntentFilter("test.app.actions.SERVICE_FINISHED") 14 ) 15 // サービス起動 16 startService(Intent(this, MyReceiver::class.java)) 17 } 18}

MyReceiver

1class MyReceiver: BroadcastReceiver() { 2 override fun onReceive(context: Context?, intent: Intent?) { 3 //MainActivityに戻る 4 LocalBroadcastManager.getInstance(context).sendBroadcast( 5 Intent("test.app.actions.SERVICE_FINISHED") 6 ) 7 } 8 } 9}

投稿2021/10/01 12:43

kakashi55

総合スコア25

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問