I was just testing my existing android application against Android O background limitations.
In testing, I found a strange behavior. So basically in my application, I am using background service and I am starting it in very first activity.
So now the issue is once the activity and background service gets started, I am closing my activity using the back button. So as per the service concepts, it keeps running in the background.
After approx 1 min onDestroy()
method gets called of background service but still the service keeps running. Ideally as per the documentation it should be get killed.
So, I don't know whether it is an issue or what.
For reference, I have created a sample code which reflects the same scenario which is as below:
Steps
- Start application
- Click on Start background service button.
- Close application using the back button.
HomeActivity.java
package com.icpl.otest;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import com.icpl.otest.service.MyService;
public class HomeActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
public void onStartServiceClick(View view) {
Intent serviceIntent = new Intent(this, MyService.class);
startService(serviceIntent);
}
}
MyService.java
package com.icpl.otest.service;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
private static final String TAG = "MyService";
private Handler mHandler;
public MyService() {
Log.i(TAG, "MyService constructor called.");
mHandler = new Handler();
}
@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "onStartCommand with intent:- " + intent);
startShowingVisibility();
return START_NOT_STICKY;
}
private void startShowingVisibility() {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
Log.i(TAG, "I am alive");
startShowingVisibility();
}
}, 3000);
}
@Override
public void onDestroy() {
Log.i(TAG, "onDestroy()");
super.onDestroy();
}
}