Skip to content

Commit ab74a85

Browse files
committed
IC
1 parent 9d3064f commit ab74a85

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

BoundService/app/src/main/java/bhavya/me/boundservice/MainActivity.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ public class MainActivity extends AppCompatActivity {
9494
* - call bindService in onCreate()
9595
* - call unBind in onDestroy()
9696
*
97+
* IMPORTANT POINT:
98+
* --- Since we are using started Service as well and started service is not bind to
99+
* the component which started the service, even the App is Destroyed service
100+
* won't stop. if we want to stop the service we want to override the onDestroy()
101+
* method of MainActivity by calling stopService() method
102+
*
97103
* */
98104

99105
BoundService mBoundService;
@@ -168,9 +174,15 @@ protected void onPause() {
168174

169175
@Override
170176
protected void onDestroy() {
177+
Log.i(TAG, "onDestroy: ");
171178
super.onDestroy();
172179

173-
Log.i(TAG, "onDestroy: ");
180+
//only uncomment if we want to destroy the service as well with the component which started
181+
//the service. Otherwise if will continue to run even if app is destroyed.
182+
/*Intent intent = new Intent(this, BoundService.class);
183+
stopService(intent);*/
184+
185+
Log.i(TAG, "onDestroy: 1");
174186
}
175187

176188
@Override

BoundService/app/src/main/java/bhavya/me/boundservice/service/BoundService.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.app.Service;
44
import android.content.Intent;
55
import android.os.Binder;
6+
import android.os.Handler;
67
import android.os.IBinder;
78
import android.os.SystemClock;
89
import android.util.Log;
@@ -16,13 +17,17 @@ public class BoundService extends Service {
1617

1718
private IBinder mBinder = new MyBinder();
1819
private Chronometer mChronometer;
20+
final Handler h = new Handler();
1921

2022
@Override
2123
public void onCreate() {
2224
super.onCreate();
23-
mChronometer = new Chronometer(this);
25+
Log.i(TAG, "onCreate: Service is created");
26+
mChronometer = new Chronometer(BoundService.this);
2427
mChronometer.setBase(SystemClock.elapsedRealtime());
2528
mChronometer.start();
29+
30+
2631
}
2732

2833
//onStartCommand() method will call only when startService() method will call but in this case both onStartCommand() and onBind() methods will call.
@@ -51,6 +56,7 @@ public IBinder onBind(Intent intent) {
5156

5257
@Override
5358
public void onDestroy() {
59+
Log.i(TAG, "onDestroy: service");
5460
super.onDestroy();
5561
mChronometer.stop();
5662
}

0 commit comments

Comments
 (0)