Service not restarting after BOOT COMPLETED under Android Oreo - android

I am having a problem with my service not starting under android Oreo.
I have an Alarm manager who schedules a service at a specific time of the day. I have a BootCompleted Broadcast receiver who resets the alarm if the user rebooted their phone.
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
new AlarmHelper(context).setAlarm(timeInMillis); // i get timeInMilis from SharedPref
}
}
I have an AlarmReceiver which starts the service when the alarm manager awakens.
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Constants.ACTION_SET_REPETITIVE_ALARM)) {
if(!isMyServiceRunning(context, PostReportsService.class)){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(background);
}else{
context.startService(background);
}
}
}
}
Device above and below Oreo both reach context.startService(background) (or context.startForegroundService(background)), but only devices above Oreo actually start the service.
This only happens after the device is rebooted, otherwise the service works for both under and above Oreo. Is there anything specfic i could do to resolve this.

Turns out the device I was testing on has power saving mode on that disabled the service on reboot.

Related

How can i start start the main activity(foregorundservice) at boot time?

I want the service to start automatically when the phone boots up. But it doesn't run. I also created a foreground service so that the main activity can run in the background.
public void onReceive(Context context, Intent intent) {
if("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())){
Intent tempService = new Intent(context, MainActivity.class);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
context.startForegroundService(tempService);
}else{
context.startService(tempService);
}
}
}
I tried to execute the method in the main activity after executing the class called myService, but I can't do that. You need another way.I tried to execute public void onreceive in the code below, but I couldn't find a way in the end.
private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
#Override
public void onReceive() {
//my code
}
};

Keep service alive after app has been destroyed

I'm developing an app that need to do some check in the server every certain amount of time. The check consist in verify if there are some notification to display. To reach that goal I implemented Service, Alarm Manager and Broadcast Reciever. This is the code that I'm using so far:
public class MainActivity {
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
setRecurringAlarm(this);
}
/**
*
* #param context
*/
private void setRecurringAlarm(Context context) {
Calendar updateTime = Calendar.getInstance();
Intent downloader = new Intent(context, MyStartServiceReceiver.class);
downloader.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, downloader, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(), 60000, pendingIntent);
}
...
}
Receiver class
public class MyStartServiceReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent dailyUpdater = new Intent(context, MyService.class);
context.startService(dailyUpdater);
Log.e("AlarmReceiver", "Called context.startService from AlarmReceiver.onReceive");
}
}
Service class
public class MyService extends IntentService {
public MyService() {
super("MyServiceName");
}
#Override
protected void onHandleIntent(Intent intent) {
Log.e("MyService", "Service running!");
// TODO Do the hard work here
this.sendNotification(this);
}
private void sendNotification(Context context) {
// TODO Manage notifications here
}
}
Manifest.xml
<!--SERVICE AND BROADCAST RECEIVER-->
<service
android:name=".MyService"
android:exported="false"/>
<receiver
android:name=".MyStartServiceReceiver"
android:process=":remote"/>
The code works fine, the task in the service will be excecuted periodically. The problem is that the service is destroyed when the app is forced to close. I need to keep alive the service, capable to execute the task, even if the user has closed the app, so the user can be updated via notifications. Thank you for your time!
You can't. If the app is forced closed, that means either its crashed (in which case the service has to be stopped as it may no longer work correctly) or the user force closed it in which case the user wants the app to stop- which means the user doesn't want the service to run. Allowing a service to be automatically restarted even if the user stops it would be basically writing malware into the OS.
In fact, Android went the exact opposite (and correct) way- if the user force stops an app, NOTHING of the app can run until the user runs it again by hand.
You may go through this. I hope this will solve your problem. If you want to keep awake your service it is practically not possible to restart the app which is forced close. So if you disable force stop your problem may be solved.

Android 4.4.2 Load app automatically

Is it possible to have an app automatically launched and running at start up of the tablet in foreground?
Beside that, is it possible to password lock an app to avoid beeing sent to the backgroud.
This will called when the device gets started
public class AutoStarter extends BroadcastReceiver {
public void onReceive(Context context, Intent intent)
{
if("android.intent.action.BOOT_COMPLETED".equals(intent.getAction()))
{
//do your stuff
}
}
}

Android - How can I check the sender class of the "stopService" Intent?

I stop a service from various places. How can I check when the sender of the command "stopService(Intent intent)" was my NetworkReceivar class (extends BroadcastReceiver)??
This is my code for do this more clear:
public class NetworkReceiver extends BroadcastReceiver{
public void onReceive(Context context, Intent intent) {
boolean isNetworkDown = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
if (isNetworkDown) {
context.stopService(new Intent(context, MyService.class));
}
My service class:
public void onDestroy() {
Log.i(TAG, "OnDestroy");
// if came from NetworkReceiver do something.
this.updater.interrupt();
this.updater = null;
super.onDestroy();
}
How can I check when the sender of the command "stopService(Intent intent)" was my NetworkReceivar class (extends BroadcastReceiver)??
AFAIK, you can't.
However, instead of stopService(), you could call startService(), sending over a command that your service interprets as "time to shut down". Something on that Intent that is your command could be used to indicate the sender, so that you can distinguish one sender from another. And, the service can call stopSelf() to shut itself down.

autostart and broadcast (remove alarm)

I've an autostart class. Everything runs perfectly, but from main app when I disable notify and I reboot I've again a notify. alarm.CancelAlarm should not remove al pending alarms?
public class AutoStartNuova extends BroadcastReceiver
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
{
Allarme alarm = new Allarme();
alarm.CancelAlarm(context);
AlarmManager:
Calendar now = Calendar.getInstance();
if(now.after(cal1))

Categories

Resources