File tree Expand file tree Collapse file tree
BoundService/app/src/main/java/bhavya/me/boundservice Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 33import android .app .Service ;
44import android .content .Intent ;
55import android .os .Binder ;
6+ import android .os .Handler ;
67import android .os .IBinder ;
78import android .os .SystemClock ;
89import 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 }
You can’t perform that action at this time.
0 commit comments