adding a service with a notification

This commit is contained in:
tibbi 2020-03-30 11:25:40 +02:00
parent 4232321735
commit a7518e9ed2
6 changed files with 74 additions and 0 deletions

View File

@ -5,6 +5,7 @@
android:installLocation="auto">
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="28" />
@ -63,5 +64,7 @@
android:label="@string/frequently_asked_questions"
android:parentActivityName="com.simplemobiletools.commons.activities.AboutActivity" />
<service android:name=".services.RecorderService"/>
</application>
</manifest>

View File

@ -19,6 +19,7 @@ import com.simplemobiletools.commons.helpers.isQPlus
import com.simplemobiletools.commons.models.FAQItem
import com.simplemobiletools.voicerecorder.BuildConfig
import com.simplemobiletools.voicerecorder.R
import com.simplemobiletools.voicerecorder.services.RecorderService
import kotlinx.android.synthetic.main.activity_main.*
import java.io.File
import java.io.IOException
@ -139,6 +140,10 @@ class MainActivity : SimpleActivity() {
duration = 0
timer = Timer()
timer.scheduleAtFixedRate(getTimerTask(), 1000, 1000)
Intent(this@MainActivity, RecorderService::class.java).apply {
startService(this)
}
} catch (e: IOException) {
showErrorToast(e)
}
@ -148,6 +153,10 @@ class MainActivity : SimpleActivity() {
private fun stopRecording() {
timer.cancel()
Intent(this@MainActivity, RecorderService::class.java).apply {
stopService(this)
}
recorder?.apply {
stop()
release()

View File

@ -0,0 +1,3 @@
package com.simplemobiletools.voicerecorder.helpers
const val RECORDER_RUNNING_NOTIF_ID = 10000

View File

@ -0,0 +1,57 @@
package com.simplemobiletools.voicerecorder.services
import android.annotation.TargetApi
import android.app.*
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.IBinder
import androidx.core.app.NotificationCompat
import com.simplemobiletools.commons.extensions.getLaunchIntent
import com.simplemobiletools.commons.helpers.isOreoPlus
import com.simplemobiletools.voicerecorder.R
import com.simplemobiletools.voicerecorder.activities.SplashActivity
import com.simplemobiletools.voicerecorder.helpers.RECORDER_RUNNING_NOTIF_ID
class RecorderService : Service() {
override fun onBind(intent: Intent?): IBinder? = null
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
super.onStartCommand(intent, flags, startId)
startForeground(RECORDER_RUNNING_NOTIF_ID, showNotification())
return START_NOT_STICKY
}
@TargetApi(Build.VERSION_CODES.O)
private fun showNotification(): Notification {
val channelId = "simple_recorder"
val label = getString(R.string.app_name)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (isOreoPlus()) {
val importance = NotificationManager.IMPORTANCE_DEFAULT
NotificationChannel(channelId, label, importance).apply {
setSound(null, null)
notificationManager.createNotificationChannel(this)
}
}
val builder = NotificationCompat.Builder(this)
.setContentTitle(label)
.setContentText(getString(R.string.recording))
.setSmallIcon(R.drawable.ic_mic_vector)
.setContentIntent(getOpenAppIntent())
.setPriority(Notification.PRIORITY_DEFAULT)
.setSound(null)
.setOngoing(true)
.setAutoCancel(true)
.setChannelId(channelId)
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
return builder.build()
}
private fun getOpenAppIntent(): PendingIntent {
val intent = getLaunchIntent() ?: Intent(this, SplashActivity::class.java)
return PendingIntent.getActivity(this, RECORDER_RUNNING_NOTIF_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
}

View File

@ -2,4 +2,5 @@
<string name="app_name">Jednoduchý nahrávač hlasu</string>
<string name="app_launcher_name">Nahrávač hlasu</string>
<string name="recording_saved_successfully">Nahrávka bola uložená ako \n\"%s\"</string>
<string name="recording">Nahráva sa</string>
</resources>

View File

@ -2,4 +2,5 @@
<string name="app_name">Simple Voice Recorder</string>
<string name="app_launcher_name">Voice Recorder</string>
<string name="recording_saved_successfully">Recording saved successfully as\n\"%s\"</string>
<string name="recording">Recording</string>
</resources>