Prac_8(AMP)
PRACTICAL 8
Activity.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btnstart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.465" />
<Button
android:id="@+id/btnstop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.718" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Practical 8 -> 724_723"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.342" />
</androidx.constraintlayout.widget.ConstraintLayout>
Mainactivit.kt
package com.example.practical8
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btnStart = findViewById<Button>(R.id.btnstart)
val btnStop = findViewById<Button>(R.id.btnstop)
btnStart.setOnClickListener{
startService(Intent(this, ExampleService::class.java))
}
btnStop.setOnClickListener{
stopService(Intent(this, ExampleService::class.java))
}
}
override fun onBackPressed() {
val builder = AlertDialog.Builder(this)
builder.setTitle("Are you sure? - 702 & 734")
builder.setMessage("Are you sure you want to close the app?")
builder.setPositiveButton("Yes",{dialogInterface,i->finish()})
builder.setNegativeButton("No",{dialogInterface, i->})
builder.setNeutralButton("Cancel",{dialogInterface,i-> Toast.makeText(this, "Cancel button pressed",Toast.LENGTH_LONG).show()})
builder.show()
}
}
ExampleService.kt
package com.example.practical8
import android.app.Service
import android.content.Intent
import android.media.MediaPlayer
import android.os.IBinder
import android.widget.Toast
class ExampleService : Service(){
lateinit var mPlayer: MediaPlayer
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
mPlayer = MediaPlayer.create(this, R.raw.music)
mPlayer.start()
Toast.makeText(this,"Song started", Toast.LENGTH_LONG).show()
return START_STICKY
}
override fun onDestroy() {
super.onDestroy()
mPlayer.stop()
Toast.makeText(this,"Song stopped", Toast.LENGTH_LONG).show()
}
override fun onBind(p0: Intent?): IBinder? {
return null
}
}
Androdimanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Practical8"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".ExampleService"/>
</application>
</manifest>
Comments
Post a Comment