Background Service is not working in android PIE version properly - android

It was working fine up to Oreo but after the API 28 PIE VERSION released the background service is not working properly, the exact flow is after starting the service without stopping it manually, the service automatically is being stopped when the app is killed. Can anyone tell me how to do it.
public void stopLocationService() {
Log.d(TAG, "stopLocationService: ");
Intent i = new Intent(getApplicationContext(), LocationService.class);
stopService(i);
}
public void startLocationService() {
Log.d(TAG, "startLocationService: ");
Intent intent = new Intent(getApplicationContext(), LocationService.class);
startService(intent);
}

Many android device manufacturers are using aggressive policies to save battery. When a user clears his/her app from recent tabs, the app is force closed, thus cancelling all alarms,broadcastReceivers,services etc. This happens in most of the device manufacturers like OnePlus,Huwaei, Xiaomi, Vivo, Oppo etc.
They have AutoStartManagers/AutoLaunchManagers that prevent the background running of the apps. You will have to white list your app using steps mentioned in THIS SO ANSWER.
Also visit https://dontkillmyapp.com/ to know about task killers in different device manufacturers.

Related

How to Start Activity From Service Android Q

Hi Developers I hope All are You fine. I need help. I make a AppLocker app and I use service to check which app i currently running. i locked app in my database with package name. When I show my lock screen From Service my Intent hit but lockScreen not Showing.How i call Activity From Service below is my Codeto start Activty from Service
if (locker_list!=null) {
if (launchapp) {
Log.d("TAG", "run: lock Screen Show");
Intent intent = new Intent(mContext, Lock_app_screen.class);
intent.putExtra("package_name", current_app);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
}
}
Android 10 (API level 29) and higher place restrictions on when apps can start activities when the app is running in the background. These restrictions help minimize interruptions for the user and keep the user more in control of what's shown on their screen.
for more...
see this

Running a foreground service in Chinese ROMs

I've made a foreground service to constantly scan for BLE devices around me. For some reason it seems to work flawlessly on my stock android device Google pixel and also on Samsung S9+.
But recently I tested the app with a Chinese ROM(Oneplus 6T, Xiaomi Poco F1) the foreground service seems to be killed there after a few minutes. I've used workmanager to restart service but the app is not restarting and I get a bug report instead for the app.
Also on Chinese ROM devices below android 8.0(Xiaomi redmi 3s prime), simple service wont work, I need to use a foreground service there as well. Is there any solution to solving this?
Ask users to whitelist your app. This is the only solution. Even foreground service + wake lock won't work.
There was a discussion last month: Workmanager reliability for periodic tasks on Chinese roms (Xiaomi, Huawei, and so on). There are some useful links in there but eventually you'll have to let users whitelist your app in every ROM's specific battery optimization(or other name) settings.
A simple approach would be to ask user to put your app in non-optimized apps by opening the battery optimization settings at the starting of your app
Use below code to open the setting:
Intent batterySaverIntent=new Intent(Settings.ACTION_BATTERY_SAVER_SETTINGS);
startActivity(batterySaverIntent);
Or you can try this:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
startForegroundService(new Intent(this, ServiceClass.class));
}

When app clear from recent apps list, background service stopped in android nougat and oreo

When I clear my app from recent apps list, my app background service killed. I tried with android nougat and oreo. But it's working in Android 6.0. After searched about the issue. Lot of people navigated the answer to Background Execution Limits.
Any one have solution for this issue?
Android Nougat should not have those issues ideally. I've spend weeks on NotificationListenerService which runs perfectly fine in background in Nougat.
It also works great in Samsung S9 (Android Oreo) but I have some issues in Google devices and emulators.
I've raised a similar issue few weeks back.
Just to make sure, enable background services for your app through console.
Also, specify which service you are using?
try this
#TargetApi(Build.VERSION_CODES.O)
private void moveToStartedState() {
Intent intent = new MyIntentBuilder(this)
.setCommand(Command.START).build();
if (isPreAndroidO()) {
Log.d(TAG, "Running on Android N or lower");
startService(intent);
} else {
Log.d(TAG, "Running on Android O");
startForegroundService(intent);
}
}
Example
https://proandroiddev.com/deep-dive-into-android-services-4830b8c9a09
Simple answer:
Use foreground services instead of background services.
Why?
Because of changes in how latest Android SDKs treat background services and Doze mode.
Good luck :)

on android 8, will the background child process get suspended when app has a service and a notification in the foreground?

my app has a proxy server in a native executable written by C, it always runs in the background, let's call it httpproxy.exe.
The main activity I use startForegroundService and startForeground with a persist notification to keep the app in the foreground avoid killing by system, then run the httpproxy.exe in the background.
my app works well on android 5 and older verisons, but on android 8.0, when the activity goes background after minutes, httpproxy.exe looks like getting suspended, so I have to bring my app up time after time, that's unacceptable.
I heard there are a so-called Background Execution Limits on android 8,
how can I have my app normally work like before?
Make sure, that your app is excluded from the Doze mode.
Checking:
PowerManager powerManager =
(PowerManager)getActivity().getSystemService(Activity.POWER_SERVICE);
powerManager.isIgnoringBatteryOptimizations(getActivity().getPackageName());
Asking the user to white-list your app:
Intent intent = new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
startActivity(intent);

Notifications won't push to real device

I was following along to TheNewBoston Android App Development for Beginners - 63 - Styles video for pushing notifications.
The app works perfectly in Android Studio's emulator, but when I run the app on my phone, the notifications won't show up.
Is there a reason notifications work on the emulator, but not an actual device?
in some devices like Huawei you need to make your app as protected app to use notification feature. you can use below code to detect Device Model and make decision to go to the protected app setting.
String PhoneModel = android.os.Build.MANUFACTURER;
if ("huawei".equalsIgnoreCase(PhoneModel)) {
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity"));
startActivity(intent);
}
Shortly after I posted the question I figured out what the problem was. I updated my phone to Oreo recently and was trying to use the old way (the way thenewboston did it inside his videos). Apparently for android Oreo you have to code notifications with channels, the old way for notifications is not compatible with Oreo.

Categories

Resources