|
3 | 3 | import android.app.NotificationManager;
|
4 | 4 | import android.content.Intent;
|
5 | 5 | import android.content.SharedPreferences;
|
| 6 | +import android.graphics.Color; |
6 | 7 | import android.os.Bundle;
|
7 | 8 | import android.support.v7.app.AppCompatActivity;
|
8 | 9 | import android.support.v7.app.NotificationCompat;
|
| 10 | +import android.support.v7.widget.Toolbar; |
9 | 11 | import android.widget.CompoundButton;
|
10 | 12 | import android.widget.Switch;
|
11 | 13 |
|
12 | 14 | public class MainActivity extends AppCompatActivity{
|
13 |
| - private myservice service; |
| 15 | + myservice service; |
14 | 16 | Switch Batteryswitch;
|
15 | 17 | Intent intent;
|
16 | 18 | NotificationCompat.Builder notificationCompat;
|
17 | 19 | NotificationManager manager;
|
18 | 20 |
|
| 21 | + Toolbar toolbar; |
19 | 22 | SharedPreferences preferences;
|
20 | 23 | SharedPreferences.Editor editor;
|
| 24 | + |
21 | 25 | @Override
|
22 | 26 | protected void onCreate(Bundle savedInstanceState) {
|
23 | 27 | super.onCreate(savedInstanceState);
|
24 | 28 | 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. |
25 | 36 | Batteryswitch = (Switch) findViewById(R.id.Batteryswitch);
|
26 |
| - |
| 37 | + //check the current switch state (enable/disable). |
27 | 38 | 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. |
28 | 40 | Batteryswitch.setChecked
|
29 | 41 | (preferences.getBoolean("Switch", false));
|
30 | 42 |
|
31 | 43 |
|
32 | 44 | Batteryswitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
|
33 | 45 | @Override
|
34 | 46 | public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
|
35 |
| - |
| 47 | + //check if the switch is checked or not. |
36 | 48 | 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 |
38 | 53 | 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(); |
39 | 57 |
|
40 |
| - preferences = getSharedPreferences("myPreference", MODE_PRIVATE); |
41 |
| - |
42 |
| - editor = preferences.edit(); |
| 58 | + editor.putBoolean("Switch", true); |
43 | 59 |
|
44 |
| - editor.putBoolean("Switch", true); |
45 |
| - |
46 |
| - editor.commit(); |
| 60 | + editor.commit(); |
47 | 61 | startService(intent);
|
48 | 62 | }
|
49 | 63 | } else {
|
| 64 | + //means service running so stop all over features. |
| 65 | + //set the current switch state to sharedPreference. |
50 | 66 | editor = preferences.edit();
|
51 | 67 | editor.putBoolean("Switch", false);
|
52 | 68 | editor.apply();
|
53 |
| - intent = new Intent(MainActivity.this, myservice.class); |
54 | 69 |
|
| 70 | + intent = new Intent(MainActivity.this, myservice.class); |
55 | 71 | stopService(intent);//stop service.
|
56 | 72 |
|
57 | 73 | //cancel notification from status bar .
|
58 |
| - |
59 | 74 | notificationCompat = (NotificationCompat.Builder) new NotificationCompat.Builder(getApplicationContext())
|
60 | 75 | .setContentTitle("Battery is low")
|
61 | 76 | .setContentText("Current percentage is ")
|
|
0 commit comments