How to run a service even when phone is idle?
I know that this question has been asked multiple times. And from other SO questions, this is what I tried. I am returning device location and phone state every 15 mins to SQLITE and sending it to server the next day. How to avoid service getting killed by Android when phone is idle? (Converting to System app is out of question.)
@Override
public void onCreate() {
Log.e("onCreate", "Service Method");
mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
if (mTimer != null) {// Cancel if already existed
mTimer.cancel();
mTimer = null;
}
mTimer = new Timer(); //recreate new
mTimer.scheduleAtFixedRate(new ConnectActivity(), 0, 900000); //Schedule task
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
startServiceForeground(intent, flags, startId);
return Service.START_STICKY;
}
public int startServiceForeground(Intent intent, int flags, int startId) {
Intent notificationIntent = new Intent(this, DeviceStatusService.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this, "channel_id")
.setContentTitle("NOTIFICATION NAME")
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.default)
.setPriority(Notification.PRIORITY_MAX)
.setContentIntent(pendingIntent)
.setOngoing(true)
.build();
startForeground(300, notification);
return START_STICKY;
}
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
Intent intent=new Intent(this,this.getClass());
startService(intent);
}
@Override
public void onDestroy() {
Log.e("onDestroy", "Service Method");
mTimer.cancel(); //For Cancel Timer
mTimer.purge();
mTimer = null;
super.onDestroy();
Intent broadcastIntent = new Intent("RestartService");
sendBroadcast(broadcastIntent);
}
Also, I have used this manifest.xml
:
<receiver
android:name=".ServiceRestarterBroadcastReceiver"
android:enabled="true"
android:label="RestartServiceWhenStopped">
<intent-filter>
<action android:name="RestartService" />
</intent-filter>
</receiver>
<service
android:name=".DeviceStatusService"
android:enabled="true"
android:stopWithTask="false"/>
ServiceRestarterBroadcastReceiver.class
public class ServiceRestarterBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
/*context.startService(new Intent(context.getApplicationContext(), DeviceStatusService.class));*/
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(new Intent(context, DeviceStatusService.class));
} else {
context.startService(new Intent(context.getApplicationContext(), DeviceStatusService.class));
}
}
}, 60000);
}
}
The problem that I face is if the screen is off and the device goes to idle state, somehow the service is either killed or does not return any value to the DB.
P.S: It worked fine when the device is in use. This problem persists especially when you do not use the device.
android database service
add a comment |
I know that this question has been asked multiple times. And from other SO questions, this is what I tried. I am returning device location and phone state every 15 mins to SQLITE and sending it to server the next day. How to avoid service getting killed by Android when phone is idle? (Converting to System app is out of question.)
@Override
public void onCreate() {
Log.e("onCreate", "Service Method");
mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
if (mTimer != null) {// Cancel if already existed
mTimer.cancel();
mTimer = null;
}
mTimer = new Timer(); //recreate new
mTimer.scheduleAtFixedRate(new ConnectActivity(), 0, 900000); //Schedule task
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
startServiceForeground(intent, flags, startId);
return Service.START_STICKY;
}
public int startServiceForeground(Intent intent, int flags, int startId) {
Intent notificationIntent = new Intent(this, DeviceStatusService.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this, "channel_id")
.setContentTitle("NOTIFICATION NAME")
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.default)
.setPriority(Notification.PRIORITY_MAX)
.setContentIntent(pendingIntent)
.setOngoing(true)
.build();
startForeground(300, notification);
return START_STICKY;
}
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
Intent intent=new Intent(this,this.getClass());
startService(intent);
}
@Override
public void onDestroy() {
Log.e("onDestroy", "Service Method");
mTimer.cancel(); //For Cancel Timer
mTimer.purge();
mTimer = null;
super.onDestroy();
Intent broadcastIntent = new Intent("RestartService");
sendBroadcast(broadcastIntent);
}
Also, I have used this manifest.xml
:
<receiver
android:name=".ServiceRestarterBroadcastReceiver"
android:enabled="true"
android:label="RestartServiceWhenStopped">
<intent-filter>
<action android:name="RestartService" />
</intent-filter>
</receiver>
<service
android:name=".DeviceStatusService"
android:enabled="true"
android:stopWithTask="false"/>
ServiceRestarterBroadcastReceiver.class
public class ServiceRestarterBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
/*context.startService(new Intent(context.getApplicationContext(), DeviceStatusService.class));*/
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(new Intent(context, DeviceStatusService.class));
} else {
context.startService(new Intent(context.getApplicationContext(), DeviceStatusService.class));
}
}
}, 60000);
}
}
The problem that I face is if the screen is off and the device goes to idle state, somehow the service is either killed or does not return any value to the DB.
P.S: It worked fine when the device is in use. This problem persists especially when you do not use the device.
android database service
Look at This
– Sniffer
Nov 14 '18 at 5:00
@HarishJose Exactly.. Now that's why I tried to run it also as foreground so that service keeps running. Yet somehow if it's idle for long time, my task does not happen..
– sanjeev
Nov 14 '18 at 5:41
@Sniffer bro.. have implemented that as well actually.. I will update my code.
– sanjeev
Nov 14 '18 at 5:42
add a comment |
I know that this question has been asked multiple times. And from other SO questions, this is what I tried. I am returning device location and phone state every 15 mins to SQLITE and sending it to server the next day. How to avoid service getting killed by Android when phone is idle? (Converting to System app is out of question.)
@Override
public void onCreate() {
Log.e("onCreate", "Service Method");
mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
if (mTimer != null) {// Cancel if already existed
mTimer.cancel();
mTimer = null;
}
mTimer = new Timer(); //recreate new
mTimer.scheduleAtFixedRate(new ConnectActivity(), 0, 900000); //Schedule task
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
startServiceForeground(intent, flags, startId);
return Service.START_STICKY;
}
public int startServiceForeground(Intent intent, int flags, int startId) {
Intent notificationIntent = new Intent(this, DeviceStatusService.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this, "channel_id")
.setContentTitle("NOTIFICATION NAME")
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.default)
.setPriority(Notification.PRIORITY_MAX)
.setContentIntent(pendingIntent)
.setOngoing(true)
.build();
startForeground(300, notification);
return START_STICKY;
}
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
Intent intent=new Intent(this,this.getClass());
startService(intent);
}
@Override
public void onDestroy() {
Log.e("onDestroy", "Service Method");
mTimer.cancel(); //For Cancel Timer
mTimer.purge();
mTimer = null;
super.onDestroy();
Intent broadcastIntent = new Intent("RestartService");
sendBroadcast(broadcastIntent);
}
Also, I have used this manifest.xml
:
<receiver
android:name=".ServiceRestarterBroadcastReceiver"
android:enabled="true"
android:label="RestartServiceWhenStopped">
<intent-filter>
<action android:name="RestartService" />
</intent-filter>
</receiver>
<service
android:name=".DeviceStatusService"
android:enabled="true"
android:stopWithTask="false"/>
ServiceRestarterBroadcastReceiver.class
public class ServiceRestarterBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
/*context.startService(new Intent(context.getApplicationContext(), DeviceStatusService.class));*/
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(new Intent(context, DeviceStatusService.class));
} else {
context.startService(new Intent(context.getApplicationContext(), DeviceStatusService.class));
}
}
}, 60000);
}
}
The problem that I face is if the screen is off and the device goes to idle state, somehow the service is either killed or does not return any value to the DB.
P.S: It worked fine when the device is in use. This problem persists especially when you do not use the device.
android database service
I know that this question has been asked multiple times. And from other SO questions, this is what I tried. I am returning device location and phone state every 15 mins to SQLITE and sending it to server the next day. How to avoid service getting killed by Android when phone is idle? (Converting to System app is out of question.)
@Override
public void onCreate() {
Log.e("onCreate", "Service Method");
mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
if (mTimer != null) {// Cancel if already existed
mTimer.cancel();
mTimer = null;
}
mTimer = new Timer(); //recreate new
mTimer.scheduleAtFixedRate(new ConnectActivity(), 0, 900000); //Schedule task
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
startServiceForeground(intent, flags, startId);
return Service.START_STICKY;
}
public int startServiceForeground(Intent intent, int flags, int startId) {
Intent notificationIntent = new Intent(this, DeviceStatusService.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this, "channel_id")
.setContentTitle("NOTIFICATION NAME")
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.default)
.setPriority(Notification.PRIORITY_MAX)
.setContentIntent(pendingIntent)
.setOngoing(true)
.build();
startForeground(300, notification);
return START_STICKY;
}
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
Intent intent=new Intent(this,this.getClass());
startService(intent);
}
@Override
public void onDestroy() {
Log.e("onDestroy", "Service Method");
mTimer.cancel(); //For Cancel Timer
mTimer.purge();
mTimer = null;
super.onDestroy();
Intent broadcastIntent = new Intent("RestartService");
sendBroadcast(broadcastIntent);
}
Also, I have used this manifest.xml
:
<receiver
android:name=".ServiceRestarterBroadcastReceiver"
android:enabled="true"
android:label="RestartServiceWhenStopped">
<intent-filter>
<action android:name="RestartService" />
</intent-filter>
</receiver>
<service
android:name=".DeviceStatusService"
android:enabled="true"
android:stopWithTask="false"/>
ServiceRestarterBroadcastReceiver.class
public class ServiceRestarterBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
/*context.startService(new Intent(context.getApplicationContext(), DeviceStatusService.class));*/
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(new Intent(context, DeviceStatusService.class));
} else {
context.startService(new Intent(context.getApplicationContext(), DeviceStatusService.class));
}
}
}, 60000);
}
}
The problem that I face is if the screen is off and the device goes to idle state, somehow the service is either killed or does not return any value to the DB.
P.S: It worked fine when the device is in use. This problem persists especially when you do not use the device.
android database service
android database service
edited Nov 14 '18 at 5:49
sanjeev
asked Nov 14 '18 at 4:54
sanjeevsanjeev
14016
14016
Look at This
– Sniffer
Nov 14 '18 at 5:00
@HarishJose Exactly.. Now that's why I tried to run it also as foreground so that service keeps running. Yet somehow if it's idle for long time, my task does not happen..
– sanjeev
Nov 14 '18 at 5:41
@Sniffer bro.. have implemented that as well actually.. I will update my code.
– sanjeev
Nov 14 '18 at 5:42
add a comment |
Look at This
– Sniffer
Nov 14 '18 at 5:00
@HarishJose Exactly.. Now that's why I tried to run it also as foreground so that service keeps running. Yet somehow if it's idle for long time, my task does not happen..
– sanjeev
Nov 14 '18 at 5:41
@Sniffer bro.. have implemented that as well actually.. I will update my code.
– sanjeev
Nov 14 '18 at 5:42
Look at This
– Sniffer
Nov 14 '18 at 5:00
Look at This
– Sniffer
Nov 14 '18 at 5:00
@HarishJose Exactly.. Now that's why I tried to run it also as foreground so that service keeps running. Yet somehow if it's idle for long time, my task does not happen..
– sanjeev
Nov 14 '18 at 5:41
@HarishJose Exactly.. Now that's why I tried to run it also as foreground so that service keeps running. Yet somehow if it's idle for long time, my task does not happen..
– sanjeev
Nov 14 '18 at 5:41
@Sniffer bro.. have implemented that as well actually.. I will update my code.
– sanjeev
Nov 14 '18 at 5:42
@Sniffer bro.. have implemented that as well actually.. I will update my code.
– sanjeev
Nov 14 '18 at 5:42
add a comment |
1 Answer
1
active
oldest
votes
I think, you should not use a Service for this purpose. You should use some sort of task shedulers. There are a lot of them - AlertManager, JobSheduler etc. And sometime it's really tricky to choose one. I recommended to start from this article: https://medium.com/mindorks/android-scheduling-background-services-a-developers-nightmare-c573807c2705
1
Hey thanks.. But I can't use schedulers as my minSDK is 19.. Now Android job doesn't work as I have to run this service every 15 mins.. I have problem only when phone goes to idle state. Don't know if Android is killing the service. But sometimes the foreground process is running yet the DB operation does not happen.. What do you think?
– sanjeev
Nov 14 '18 at 5:39
Have you read the article? :) GCM Network Manager, mentioned on it, can be used from Api 9. And why do you need to send the data every 15 minutes? Think, is it really necessary?
– Alex Shevelev
Nov 14 '18 at 17:37
Nope. I don't need to send the data every 15 mins.. But I am retrieving data every 15 mins and writing it to sqlite.. Then I am sending the same next day.. Apparently I figured out now that my service is running yet, sqlite operation does not happen in the background for every 15 mins..
– sanjeev
Nov 15 '18 at 4:03
Ok. But why do you need to store data every 15 minutes? If your device is idle it means that it's location is not changing etc. See this: source.android.com/devices/tech/power/platform_mgmt
– Alex Shevelev
Nov 15 '18 at 5:08
It's a client requirement and basically I am against these.. But if a user is travelling and not using his phone. The device is still idle right?
– sanjeev
Nov 15 '18 at 5:10
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293389%2fhow-to-run-a-service-even-when-phone-is-idle%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think, you should not use a Service for this purpose. You should use some sort of task shedulers. There are a lot of them - AlertManager, JobSheduler etc. And sometime it's really tricky to choose one. I recommended to start from this article: https://medium.com/mindorks/android-scheduling-background-services-a-developers-nightmare-c573807c2705
1
Hey thanks.. But I can't use schedulers as my minSDK is 19.. Now Android job doesn't work as I have to run this service every 15 mins.. I have problem only when phone goes to idle state. Don't know if Android is killing the service. But sometimes the foreground process is running yet the DB operation does not happen.. What do you think?
– sanjeev
Nov 14 '18 at 5:39
Have you read the article? :) GCM Network Manager, mentioned on it, can be used from Api 9. And why do you need to send the data every 15 minutes? Think, is it really necessary?
– Alex Shevelev
Nov 14 '18 at 17:37
Nope. I don't need to send the data every 15 mins.. But I am retrieving data every 15 mins and writing it to sqlite.. Then I am sending the same next day.. Apparently I figured out now that my service is running yet, sqlite operation does not happen in the background for every 15 mins..
– sanjeev
Nov 15 '18 at 4:03
Ok. But why do you need to store data every 15 minutes? If your device is idle it means that it's location is not changing etc. See this: source.android.com/devices/tech/power/platform_mgmt
– Alex Shevelev
Nov 15 '18 at 5:08
It's a client requirement and basically I am against these.. But if a user is travelling and not using his phone. The device is still idle right?
– sanjeev
Nov 15 '18 at 5:10
|
show 1 more comment
I think, you should not use a Service for this purpose. You should use some sort of task shedulers. There are a lot of them - AlertManager, JobSheduler etc. And sometime it's really tricky to choose one. I recommended to start from this article: https://medium.com/mindorks/android-scheduling-background-services-a-developers-nightmare-c573807c2705
1
Hey thanks.. But I can't use schedulers as my minSDK is 19.. Now Android job doesn't work as I have to run this service every 15 mins.. I have problem only when phone goes to idle state. Don't know if Android is killing the service. But sometimes the foreground process is running yet the DB operation does not happen.. What do you think?
– sanjeev
Nov 14 '18 at 5:39
Have you read the article? :) GCM Network Manager, mentioned on it, can be used from Api 9. And why do you need to send the data every 15 minutes? Think, is it really necessary?
– Alex Shevelev
Nov 14 '18 at 17:37
Nope. I don't need to send the data every 15 mins.. But I am retrieving data every 15 mins and writing it to sqlite.. Then I am sending the same next day.. Apparently I figured out now that my service is running yet, sqlite operation does not happen in the background for every 15 mins..
– sanjeev
Nov 15 '18 at 4:03
Ok. But why do you need to store data every 15 minutes? If your device is idle it means that it's location is not changing etc. See this: source.android.com/devices/tech/power/platform_mgmt
– Alex Shevelev
Nov 15 '18 at 5:08
It's a client requirement and basically I am against these.. But if a user is travelling and not using his phone. The device is still idle right?
– sanjeev
Nov 15 '18 at 5:10
|
show 1 more comment
I think, you should not use a Service for this purpose. You should use some sort of task shedulers. There are a lot of them - AlertManager, JobSheduler etc. And sometime it's really tricky to choose one. I recommended to start from this article: https://medium.com/mindorks/android-scheduling-background-services-a-developers-nightmare-c573807c2705
I think, you should not use a Service for this purpose. You should use some sort of task shedulers. There are a lot of them - AlertManager, JobSheduler etc. And sometime it's really tricky to choose one. I recommended to start from this article: https://medium.com/mindorks/android-scheduling-background-services-a-developers-nightmare-c573807c2705
answered Nov 14 '18 at 5:18
Alex ShevelevAlex Shevelev
19718
19718
1
Hey thanks.. But I can't use schedulers as my minSDK is 19.. Now Android job doesn't work as I have to run this service every 15 mins.. I have problem only when phone goes to idle state. Don't know if Android is killing the service. But sometimes the foreground process is running yet the DB operation does not happen.. What do you think?
– sanjeev
Nov 14 '18 at 5:39
Have you read the article? :) GCM Network Manager, mentioned on it, can be used from Api 9. And why do you need to send the data every 15 minutes? Think, is it really necessary?
– Alex Shevelev
Nov 14 '18 at 17:37
Nope. I don't need to send the data every 15 mins.. But I am retrieving data every 15 mins and writing it to sqlite.. Then I am sending the same next day.. Apparently I figured out now that my service is running yet, sqlite operation does not happen in the background for every 15 mins..
– sanjeev
Nov 15 '18 at 4:03
Ok. But why do you need to store data every 15 minutes? If your device is idle it means that it's location is not changing etc. See this: source.android.com/devices/tech/power/platform_mgmt
– Alex Shevelev
Nov 15 '18 at 5:08
It's a client requirement and basically I am against these.. But if a user is travelling and not using his phone. The device is still idle right?
– sanjeev
Nov 15 '18 at 5:10
|
show 1 more comment
1
Hey thanks.. But I can't use schedulers as my minSDK is 19.. Now Android job doesn't work as I have to run this service every 15 mins.. I have problem only when phone goes to idle state. Don't know if Android is killing the service. But sometimes the foreground process is running yet the DB operation does not happen.. What do you think?
– sanjeev
Nov 14 '18 at 5:39
Have you read the article? :) GCM Network Manager, mentioned on it, can be used from Api 9. And why do you need to send the data every 15 minutes? Think, is it really necessary?
– Alex Shevelev
Nov 14 '18 at 17:37
Nope. I don't need to send the data every 15 mins.. But I am retrieving data every 15 mins and writing it to sqlite.. Then I am sending the same next day.. Apparently I figured out now that my service is running yet, sqlite operation does not happen in the background for every 15 mins..
– sanjeev
Nov 15 '18 at 4:03
Ok. But why do you need to store data every 15 minutes? If your device is idle it means that it's location is not changing etc. See this: source.android.com/devices/tech/power/platform_mgmt
– Alex Shevelev
Nov 15 '18 at 5:08
It's a client requirement and basically I am against these.. But if a user is travelling and not using his phone. The device is still idle right?
– sanjeev
Nov 15 '18 at 5:10
1
1
Hey thanks.. But I can't use schedulers as my minSDK is 19.. Now Android job doesn't work as I have to run this service every 15 mins.. I have problem only when phone goes to idle state. Don't know if Android is killing the service. But sometimes the foreground process is running yet the DB operation does not happen.. What do you think?
– sanjeev
Nov 14 '18 at 5:39
Hey thanks.. But I can't use schedulers as my minSDK is 19.. Now Android job doesn't work as I have to run this service every 15 mins.. I have problem only when phone goes to idle state. Don't know if Android is killing the service. But sometimes the foreground process is running yet the DB operation does not happen.. What do you think?
– sanjeev
Nov 14 '18 at 5:39
Have you read the article? :) GCM Network Manager, mentioned on it, can be used from Api 9. And why do you need to send the data every 15 minutes? Think, is it really necessary?
– Alex Shevelev
Nov 14 '18 at 17:37
Have you read the article? :) GCM Network Manager, mentioned on it, can be used from Api 9. And why do you need to send the data every 15 minutes? Think, is it really necessary?
– Alex Shevelev
Nov 14 '18 at 17:37
Nope. I don't need to send the data every 15 mins.. But I am retrieving data every 15 mins and writing it to sqlite.. Then I am sending the same next day.. Apparently I figured out now that my service is running yet, sqlite operation does not happen in the background for every 15 mins..
– sanjeev
Nov 15 '18 at 4:03
Nope. I don't need to send the data every 15 mins.. But I am retrieving data every 15 mins and writing it to sqlite.. Then I am sending the same next day.. Apparently I figured out now that my service is running yet, sqlite operation does not happen in the background for every 15 mins..
– sanjeev
Nov 15 '18 at 4:03
Ok. But why do you need to store data every 15 minutes? If your device is idle it means that it's location is not changing etc. See this: source.android.com/devices/tech/power/platform_mgmt
– Alex Shevelev
Nov 15 '18 at 5:08
Ok. But why do you need to store data every 15 minutes? If your device is idle it means that it's location is not changing etc. See this: source.android.com/devices/tech/power/platform_mgmt
– Alex Shevelev
Nov 15 '18 at 5:08
It's a client requirement and basically I am against these.. But if a user is travelling and not using his phone. The device is still idle right?
– sanjeev
Nov 15 '18 at 5:10
It's a client requirement and basically I am against these.. But if a user is travelling and not using his phone. The device is still idle right?
– sanjeev
Nov 15 '18 at 5:10
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293389%2fhow-to-run-a-service-even-when-phone-is-idle%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Look at This
– Sniffer
Nov 14 '18 at 5:00
@HarishJose Exactly.. Now that's why I tried to run it also as foreground so that service keeps running. Yet somehow if it's idle for long time, my task does not happen..
– sanjeev
Nov 14 '18 at 5:41
@Sniffer bro.. have implemented that as well actually.. I will update my code.
– sanjeev
Nov 14 '18 at 5:42