-
Notifications
You must be signed in to change notification settings - Fork 6
/
BroadcastReviever.java
123 lines (93 loc) · 3.59 KB
/
BroadcastReviever.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
public class BroadcastReviever {
}
/////////////// Broadcast Reciever (Airplane Mode)
TextView output;
MyBroadcastReciever myBroadcastReciever;
output = findViewById(R.id.output);
}
@Override
protected void onStart() {
super.onStart();
myBroadcastReciever = new MyBroadcastReciever(output);
IntentFilter filter = new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
registerReceiver(myBroadcastReciever, filter);
}
@Override
protected void onStop() {
super.onStop();
unregisterReceiver(myBroadcastReciever);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(myBroadcastReciever);
}
}
public class MyBroadcastReciever extends BroadcastReceiver {
TextView output;
// create a constructor with same clss name
public void MyBroadcastReciever() {
}
public MyBroadcastReciever(TextView output) {
this.output = output;
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() != null && intent.getAction().equals(Intent.ACTION_AIRPLANE_MODE_CHANGED)) {
boolean isAirplaneModeOn = Settings.System.getInt(context.getContentResolver(),
Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
if (isAirplaneModeOn) {
output.setText("Airplane Mode is ON");
Toast.makeText(context, "Airplane Mode is ON", Toast.LENGTH_SHORT).show();
} else {
output.setText("Airplane Mode is OFF");
Toast.makeText(context, "Airplane Mode is OFF", Toast.LENGTH_SHORT).show();
}
}
}
/////////////////////////////////////////// Broadcast Reciever (Battery Level)
TextView output;
MyBroadcastReciever myBroadcastReciever;
output=
findViewById(R.id.output);
@Override
protected void onStart() {
super.onStart();
myBroadcastReciever = new MyBroadcastReciever(output);
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(myBroadcastReciever, filter);
}
@Override
protected void onStop() {
super.onStop();
unregisterReceiver(myBroadcastReciever);
}
/// Class
public class MyBroadcastReciever extends BroadcastReceiver {
TextView output;
public MyBroadcastReciever() {
}
public MyBroadcastReciever(TextView output) {
this.output = output;
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() != null && intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED))
;
int batteryLevel = intent.getIntExtra("level", 0);
if (batteryLevel <= 15) {
output.setText("Battery low : " + batteryLevel + "%");
Toast.makeText(context, "Battery low ", Toast.LENGTH_SHORT).show();
} else if (batteryLevel >= 15 && batteryLevel <= 99) {
output.setText("Battery is : " + batteryLevel + "%");
Toast.makeText(context, "Battery is :", Toast.LENGTH_SHORT).show();
} else if (batteryLevel == 100) {
output.setText("Battery is full : " + batteryLevel + "%");
Toast.makeText(context, "Battery full", Toast.LENGTH_SHORT).show();
}
else {
output.setText("Battery off");
Toast.makeText(context, "Battery off", Toast.LENGTH_SHORT).show();
}
}
}