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

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

新規登録して質問してみよう
ただいま回答率
85.48%
バックグラウンド処理

バックグラウンド処理とは、マルチタスク環境において、ユーザーに対して前面に表示させている処理の裏側で実行させる処理のことを呼びます。バックグラウンド処理を行う事によって、ユーザーが他の作業に携わることが可能となります。

Android Studio

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

Kotlin

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

Q&A

解決済

1回答

862閲覧

Android Kotlin 音声をバックグラウンドで出力し、音声の終了時に通知を表示したい。

退会済みユーザー

退会済みユーザー

総合スコア0

バックグラウンド処理

バックグラウンド処理とは、マルチタスク環境において、ユーザーに対して前面に表示させている処理の裏側で実行させる処理のことを呼びます。バックグラウンド処理を行う事によって、ユーザーが他の作業に携わることが可能となります。

Android Studio

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

Kotlin

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

0グッド

0クリップ

投稿2020/01/06 14:49

編集2020/01/06 14:59

参考書に従い、音声をバックグラウンドで出力し、音声の終了時に通知を表示したいのですが
通知がされません。

見直したものの参考書との差異はないように思います。
間違っている箇所がありましたら、ご指摘をお願いいたします。

xml

1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:orientation="vertical"> 7 8 <Button 9 android:id="@+id/btPlay" 10 android:layout_width="match_parent" 11 android:layout_height="wrap_content" 12 android:onClick="onPlayButtonClick" 13 android:text="@string/bt_play_play"/> 14 15 <Button 16 android:id="@+id/btStop" 17 android:layout_width="match_parent" 18 android:layout_height="wrap_content" 19 android:enabled="false" 20 android:onClick="onStopButtonClick" 21 android:text="@string/bt_play_stop"/> 22 23 24</LinearLayout>

xml

1<?xml version="1.0" encoding="utf-8"?> 2<manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.websarva.wings.android.servicesample"> 4 5 <application 6 android:allowBackup="true" 7 android:icon="@mipmap/ic_launcher" 8 android:label="@string/app_name" 9 android:roundIcon="@mipmap/ic_launcher_round" 10 android:supportsRtl="true" 11 android:theme="@style/AppTheme"> 12 <service 13 android:name=".SoundManagerService" 14 android:enabled="true" 15 android:exported="false"></service> 16 17 <activity android:name=".MainActivity"> 18 <intent-filter> 19 <action android:name="android.intent.action.MAIN" /> 20 21 <category android:name="android.intent.category.LAUNCHER" /> 22 </intent-filter> 23 </activity> 24 </application> 25 26</manifest>

kotlin

1package com.websarva.wings.android.servicesample 2 3import android.content.Intent 4import android.support.v7.app.AppCompatActivity 5import android.os.Bundle 6import android.view.View 7import android.widget.Button 8 9class MainActivity : AppCompatActivity() { 10 11 override fun onCreate(savedInstanceState: Bundle?) { 12 super.onCreate(savedInstanceState) 13 setContentView(R.layout.activity_main) 14 } 15 16 fun onPlayButtonClick(view: View) { 17 val intent = Intent(applicationContext, SoundManagerService::class.java) 18 startService(intent) 19 20 val btPlay = findViewById<Button>(R.id.btPlay) 21 val btStop = findViewById<Button>(R.id.btStop) 22 23 btPlay.isEnabled = false 24 btStop.isEnabled = true 25 } 26 27 fun onStopButtonClick(view: View) { 28 val intent = Intent(applicationContext, SoundManagerService::class.java) 29 stopService(intent) 30 31 val btPlay = findViewById<Button>(R.id.btPlay) 32 val btStop = findViewById<Button>(R.id.btStop) 33 34 btPlay.isEnabled = true 35 btStop.isEnabled = false 36 } 37} 38

kotlin

1package com.websarva.wings.android.servicesample 2 3import android.app.NotificationChannel 4import android.app.NotificationManager 5import android.app.Service 6import android.content.Context 7import android.content.Intent 8import android.media.MediaPlayer 9import android.net.Uri 10import android.os.IBinder 11import android.support.v4.app.NotificationCompat 12import android.util.Log 13import android.view.View 14import java.io.IOException 15import java.lang.IllegalArgumentException 16 17class SoundManagerService : Service() { 18 19 override fun onBind(intent: Intent): IBinder { 20 TODO("Return the communication channel to the service.") 21 } 22 23 private var _player: MediaPlayer? = null 24 override fun onCreate() { 25 _player = MediaPlayer() 26 27 val id = "soundmanagerservice_notification_channel" 28 val name = getString(R.string.notification_channel_name) 29 val importance = NotificationManager.IMPORTANCE_DEFAULT 30 val channel = NotificationChannel(id, name, importance) 31 32 val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager 33 manager.createNotificationChannel(channel) 34 } 35 36 override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { 37 38 val mediaFileUriStr = "android.resource://${packageName}/${R.raw.mountain_stream}" 39 val mediaFileUri = Uri.parse(mediaFileUriStr) 40 41 try { 42 _player?.setDataSource(applicationContext, mediaFileUri) 43 _player?.setOnPreparedListener(PlayerPreparedListener()) 44 _player?.setOnCompletionListener(PlayerCompletionListener()) 45 _player?.prepareAsync() 46 } catch (ex: IllegalArgumentException) { 47 Log.e("ServiceSample", "メディアプレーヤー準備時の例外発生") 48 } catch (ex: IOException) { 49 Log.e("ServiceSample", "メディアプレーヤー準備時の例外発生") 50 } 51 52 return Service.START_NOT_STICKY 53 } 54 55 override fun onDestroy() { 56 57 _player?.let { 58 if (it.isPlaying) { 59 it.stop() 60 } 61 62 it.release() 63 _player = null 64 } 65 } 66 67 private inner class PlayerPreparedListener : MediaPlayer.OnPreparedListener { 68 override fun onPrepared(mp: MediaPlayer) { 69 mp.start() 70 } 71 } 72 73 private inner class PlayerCompletionListener : MediaPlayer.OnCompletionListener { 74 override fun onCompletion(mp: MediaPlayer) { 75 val builder = NotificationCompat.Builder(applicationContext, "soundmanagerservice_notification_channel") 76 builder.setSmallIcon(android.R.drawable.ic_dialog_info) 77 builder.setContentTitle(getString(R.string.msg_notification_title_finish)) 78 builder.setContentText(getString(R.string.msg_notification_text_finish)) 79 80 val notification = builder.build() 81 val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager 82 manager.notify(0, notification) 83 84 stopSelf() 85 } 86 } 87 88 89} 90

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

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

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

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

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

guest

回答1

0

ベストアンサー

記載されているコードで試してみましたが、OS9の端末で通知を受け取ることができました。
(ただしAndroidX対応済み)

こちらを参考にAndroidXへの移行を行って再度確認してみて頂けませんか?
https://developer.android.com/jetpack/androidx/migrate?hl=ja

投稿2020/01/08 04:07

nono0812

総合スコア13

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問