Usama Sajid
Usama Sajid

Reputation: 81

background service not working after app is killed in android

I am trying to load an URL (API) after every 15 seconds in a service. Everything is working fine but when the app is killed URL is not called. I dont need any UI in my app. I just want it to work in background when the app is killed. I have been finding a solution for two days but nothing worked. Please help! Here is my service code :

public class MyService extends Service {
Handler handler = new Handler();
Runnable runnable;
int delay = 15*1000;
String data ;

@Override
public IBinder onBind(Intent intent) {
    return null;
    }

@Override
public int onStartCommand(final Intent intent, int flags, int startId) {
    handler.postDelayed( runnable = new Runnable() {
    public void run() {
    data = (String) intent.getExtras().get("data");
    Toast.makeText(MyService.this, ""+data, Toast.LENGTH_SHORT).show();
    loadURL(data);

    handler.postDelayed(runnable, delay);
    }
    }, delay);
    return START_STICKY ;
    }

@Override
public void onDestroy() {
    handler.postDelayed( runnable = new Runnable() {
            public void run() {
                    Toast.makeText(MyService.this, ""+data, Toast.LENGTH_SHORT).show();
                    loadURL(data);
                    handler.postDelayed(runnable, delay);
            }
    }, delay);
}

public void loadURL(String data){
    try{
            RequestFuture<JSONObject> requestFuture=RequestFuture.newFuture();
            final String mURL = "http://192.168.1.12/att.php?emp_id=" + data + "&status=&submit=Insert";
            JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST,
                    mURL,new JSONObject(),requestFuture,requestFuture);
            MySingleton.getInstance(this).addToRequestQueue(request);
            Toast.makeText(this, "Done", Toast.LENGTH_SHORT).show();
            try {
                    JSONObject object= requestFuture.get(10, TimeUnit.SECONDS);
            } catch (Exception e) {
                    e.printStackTrace();
            }
    } catch (Exception e){
    Toast.makeText(this, ""+e.getMessage(), Toast.LENGTH_SHORT).show();
    }
  }
}

Here is my MainActivity.java where I am getting an intent as a userID :

public class MainActivity extends AppCompatActivity {

String data;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    data = getIntent().getStringExtra("ID");
    Intent intent = new Intent(MainActivity.this, MyService.class);
    intent.putExtra("data", data);
    startService(intent);
  }
}

Upvotes: 1

Views: 902

Answers (1)

Alex
Alex

Reputation: 972

You will have to turn that background service into a foreground service, because of the limitations called Background Execution Limits that started from android Oreo.

Please check out this link for more better understanding: https://developer.android.com/about/versions/oreo/background

Upvotes: 1

Related Questions