mirror of
https://github.com/SimpleMobileTools/Simple-Voice-Recorder.git
synced 2025-06-05 21:59:31 +02:00
adding a service with a notification
This commit is contained in:
@ -5,6 +5,7 @@
|
|||||||
android:installLocation="auto">
|
android:installLocation="auto">
|
||||||
|
|
||||||
<uses-permission android:name="android.permission.RECORD_AUDIO" />
|
<uses-permission android:name="android.permission.RECORD_AUDIO" />
|
||||||
|
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
|
||||||
<uses-permission
|
<uses-permission
|
||||||
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
|
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
|
||||||
android:maxSdkVersion="28" />
|
android:maxSdkVersion="28" />
|
||||||
@ -63,5 +64,7 @@
|
|||||||
android:label="@string/frequently_asked_questions"
|
android:label="@string/frequently_asked_questions"
|
||||||
android:parentActivityName="com.simplemobiletools.commons.activities.AboutActivity" />
|
android:parentActivityName="com.simplemobiletools.commons.activities.AboutActivity" />
|
||||||
|
|
||||||
|
<service android:name=".services.RecorderService"/>
|
||||||
|
|
||||||
</application>
|
</application>
|
||||||
</manifest>
|
</manifest>
|
||||||
|
@ -19,6 +19,7 @@ import com.simplemobiletools.commons.helpers.isQPlus
|
|||||||
import com.simplemobiletools.commons.models.FAQItem
|
import com.simplemobiletools.commons.models.FAQItem
|
||||||
import com.simplemobiletools.voicerecorder.BuildConfig
|
import com.simplemobiletools.voicerecorder.BuildConfig
|
||||||
import com.simplemobiletools.voicerecorder.R
|
import com.simplemobiletools.voicerecorder.R
|
||||||
|
import com.simplemobiletools.voicerecorder.services.RecorderService
|
||||||
import kotlinx.android.synthetic.main.activity_main.*
|
import kotlinx.android.synthetic.main.activity_main.*
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
@ -139,6 +140,10 @@ class MainActivity : SimpleActivity() {
|
|||||||
duration = 0
|
duration = 0
|
||||||
timer = Timer()
|
timer = Timer()
|
||||||
timer.scheduleAtFixedRate(getTimerTask(), 1000, 1000)
|
timer.scheduleAtFixedRate(getTimerTask(), 1000, 1000)
|
||||||
|
|
||||||
|
Intent(this@MainActivity, RecorderService::class.java).apply {
|
||||||
|
startService(this)
|
||||||
|
}
|
||||||
} catch (e: IOException) {
|
} catch (e: IOException) {
|
||||||
showErrorToast(e)
|
showErrorToast(e)
|
||||||
}
|
}
|
||||||
@ -148,6 +153,10 @@ class MainActivity : SimpleActivity() {
|
|||||||
private fun stopRecording() {
|
private fun stopRecording() {
|
||||||
timer.cancel()
|
timer.cancel()
|
||||||
|
|
||||||
|
Intent(this@MainActivity, RecorderService::class.java).apply {
|
||||||
|
stopService(this)
|
||||||
|
}
|
||||||
|
|
||||||
recorder?.apply {
|
recorder?.apply {
|
||||||
stop()
|
stop()
|
||||||
release()
|
release()
|
||||||
|
@ -0,0 +1,3 @@
|
|||||||
|
package com.simplemobiletools.voicerecorder.helpers
|
||||||
|
|
||||||
|
const val RECORDER_RUNNING_NOTIF_ID = 10000
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
@ -2,4 +2,5 @@
|
|||||||
<string name="app_name">Jednoduchý nahrávač hlasu</string>
|
<string name="app_name">Jednoduchý nahrávač hlasu</string>
|
||||||
<string name="app_launcher_name">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_saved_successfully">Nahrávka bola uložená ako \n\"%s\"</string>
|
||||||
|
<string name="recording">Nahráva sa</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
@ -2,4 +2,5 @@
|
|||||||
<string name="app_name">Simple Voice Recorder</string>
|
<string name="app_name">Simple Voice Recorder</string>
|
||||||
<string name="app_launcher_name">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_saved_successfully">Recording saved successfully as\n\"%s\"</string>
|
||||||
|
<string name="recording">Recording</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
Reference in New Issue
Block a user