Android Programming
Practical no-8
Aim: :- Programs on Services, notification and broadcast receivers.
[Link]=
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#83C725"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="280dp"
android:layout_height="291dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
Android Programming
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/baseline_flight_24" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android Broadcast"
android:textSize="70px"
android:textAlignment="center"
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.074" />
</[Link]>
[Link]=
package [Link].practicalno8
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
class MainActivity : AppCompatActivity() {
private val myreceiver=MyReceiver()
override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
enableEdgeToEdge()
setContentView([Link].activity_main)
registerReceiver(
myreceiver,
IntentFilter("[Link].AIRPLANE_MODE")
)
}
}
[Link]=
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="[Link]
Android Programming
xmlns:tools="[Link]
<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.PracticalNo8"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="[Link]" />
<category android:name="[Link]" />
</intent-filter>
</activity>
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="[Link].AIRPLANE_MODE"/>
</intent-filter>
</receiver>
</application>
</manifest>
Android Programming
[Link]=
package [Link].practicalno8
import [Link]
import [Link]
import [Link]
import [Link]
class MyReceiver : BroadcastReceiver() {
override fun onReceive(context: Context,intent: Intent){
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
[Link](context,"Broadcast : Flight mode changed.",
Toast.LENGTH_LONG).show()
}
}
Output: