Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

21 TH

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

activity_main.

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/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginStart="8dp"
android:layout_marginTop="252dp"
android:layout_marginEnd="8dp"
android:text="Broadcast Intent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.538"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivty.java
package com.example.exp21to32;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
Button button;
MyReceiver myReceiver = new MyReceiver();
IntentFilter filter = new IntentFilter();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main);
button = findViewById(R.id.button);
filter.addAction( Intent.ACTION_AIRPLANE_MODE_CHANGED );
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity.this.registerReceiver(myReceiver,filter);
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
MainActivity.this.unregisterReceiver( myReceiver );
}
}
MyReciever.java
package com.example.exp21to32;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver {
public MyReceiver() {
super();
}
@Override
public void onReceive(Context context, Intent intent) {
String intentAction = intent.getAction();
if (intentAction != null) {
String toastMessage = "unknown intent action";
switch (intentAction) {
case Intent.ACTION_AIRPLANE_MODE_CHANGED:
boolean status = intent.getBooleanExtra("state", true);
if (status) {
toastMessage = "Airplane Mode turned on";
} else {
toastMessage = "Airplane Mode turned off";
}
break;
}
Toast.makeText(context, toastMessage, Toast.LENGTH_SHORT).show();
}
}
}
AndroidManifest.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:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
>
<activity android:name=".MainActivity"
android:theme="@style/AppTheme"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
<receiver android:name=".MyReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.AIRPLANE_MODE" />
</intent-filter>
</receiver>

</application>
</manifest>
themes.xml

<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
</resources>

OUTPUT:-

You might also like