Skip to content

Commit cbffd17

Browse files
authored
updated code
1 parent 9334254 commit cbffd17

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

batterypercentage code file/MainActivity.java

+27-12
Original file line numberDiff line numberDiff line change
@@ -3,59 +3,74 @@
33
import android.app.NotificationManager;
44
import android.content.Intent;
55
import android.content.SharedPreferences;
6+
import android.graphics.Color;
67
import android.os.Bundle;
78
import android.support.v7.app.AppCompatActivity;
89
import android.support.v7.app.NotificationCompat;
10+
import android.support.v7.widget.Toolbar;
911
import android.widget.CompoundButton;
1012
import android.widget.Switch;
1113

1214
public class MainActivity extends AppCompatActivity{
13-
private myservice service;
15+
myservice service;
1416
Switch Batteryswitch;
1517
Intent intent;
1618
NotificationCompat.Builder notificationCompat;
1719
NotificationManager manager;
1820

21+
Toolbar toolbar;
1922
SharedPreferences preferences;
2023
SharedPreferences.Editor editor;
24+
2125
@Override
2226
protected void onCreate(Bundle savedInstanceState) {
2327
super.onCreate(savedInstanceState);
2428
setContentView(R.layout.activity_main);
29+
//add toolbar and find id.
30+
toolbar= (Toolbar) findViewById(R.id.toolbar);
31+
//set toolbar title as our app name.
32+
toolbar.setTitle(getResources().getString(R.string.app_name));
33+
//set toolbar title text color as 'white'.
34+
toolbar.setTitleTextColor(Color.parseColor("#ffffff"));
35+
//find switch(android view/control) id.
2536
Batteryswitch = (Switch) findViewById(R.id.Batteryswitch);
26-
37+
//check the current switch state (enable/disable).
2738
preferences = getSharedPreferences("myPreference", MODE_PRIVATE);
39+
//set the sharedPreference current state to switch. by default must be false.because switch by default is turn off you have to turn on.
2840
Batteryswitch.setChecked
2941
(preferences.getBoolean("Switch", false));
3042

3143

3244
Batteryswitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
3345
@Override
3446
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
35-
47+
//check if the switch is checked or not.
3648
if (Batteryswitch.isChecked() == true) {
37-
if (service == null) {
49+
//switch is checked.
50+
//check service is null or not.
51+
if (service==null) {
52+
//start the service here
3853
intent = new Intent(MainActivity.this, myservice.class);
54+
//set the current switch state to sharedPreference.
55+
preferences = getSharedPreferences("myPreference", MODE_PRIVATE);
56+
editor = preferences.edit();
3957

40-
preferences = getSharedPreferences("myPreference", MODE_PRIVATE);
41-
42-
editor = preferences.edit();
58+
editor.putBoolean("Switch", true);
4359

44-
editor.putBoolean("Switch", true);
45-
46-
editor.commit();
60+
editor.commit();
4761
startService(intent);
4862
}
4963
} else {
64+
//means service running so stop all over features.
65+
//set the current switch state to sharedPreference.
5066
editor = preferences.edit();
5167
editor.putBoolean("Switch", false);
5268
editor.apply();
53-
intent = new Intent(MainActivity.this, myservice.class);
5469

70+
intent = new Intent(MainActivity.this, myservice.class);
5571
stopService(intent);//stop service.
5672

5773
//cancel notification from status bar .
58-
5974
notificationCompat = (NotificationCompat.Builder) new NotificationCompat.Builder(getApplicationContext())
6075
.setContentTitle("Battery is low")
6176
.setContentText("Current percentage is ")

0 commit comments

Comments
 (0)