Reputation: 81
In my app, it loads a url(API) after every 15 seconds in the background. I dont need any kind of UI in my app so I just load the url in my service class. To work properly on oreo+ devices I used startForgroundService(). But it is not working in Oreo after the app is killed. My testing device is OPPO Oreo 8.1.0. It is perfectly working on Samsung 7.0.
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) {
data = intent.getStringExtra("data");
loadURL(data);
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("URL App status :")
.setContentText("The app is running in background")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentIntent(pendingIntent)
.build();
startForeground(1, notification);
handler.postDelayed(runnable = new Runnable() {
public void run() {
Toast.makeText(MyService.this, "" + data, Toast.LENGTH_SHORT).show();
loadURL(data);
handler.postDelayed(runnable, delay);
}
}, delay);
return START_STICKY;
}
@Override
public void onDestroy() {
stopSelf();
}
public void loadURL(String data) {
try {
RequestFuture<JSONObject> requestFuture = RequestFuture.newFuture();
final String mURL = "http://localhost/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();
} catch (Exception e) {
Toast.makeText(this, "" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
App.class for notifications :
public class App extends Application {
public static final String CHANNEL_ID = "channel ID";
@Override
public void onCreate() {
super.onCreate();
createnotificationChannel();
}
private void createnotificationChannel(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel serviceChannel = new NotificationChannel(CHANNEL_ID, "URL channel ID",
NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(serviceChannel);
}
}
}
And the MainActivity where the foreground service is started :
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);
ContextCompat.startForegroundService(this,intent);
}
I have searched alot but I cant fint out what is wrong with my code. Just need help !
Upvotes: 1
Views: 5286
Reputation: 412
This is due battery Optimiser from manufacture itself. You have manually allow background in setting.
Upvotes: 0
Reputation: 2319
Read this answer
Every service can be killed depending on its phone situation. From Android 8, there are some limits for Background Execution.
Foreground services continue running even when the user isn't interacting with the app.but it may be killed after the app closed.
if you want to run your service after closing the app read this answer
Upvotes: 2