How to make an Android app to always run in background Programmatically
Hello Friends, welcome to my new android programming Article during this Article I will be able to show you ways to urge background running applications in android programmatically and make background running app in android so let’s get started.
Step-1
First of all, you’ve got to import the ASCII text file of any application inside Android Studio or AIDE application, I am currently getting to make a background running application in android using the Aide application.
Step-2
After importing the appliance, you’ve got to open the MainActivity file, you’ve got to follow this process altogether application.
Step-3
Now you’ve got to first import the Android Requirement java function into it, with the assistance of which we will create a background service app for android here.
import android.content.Intent;
import android.os.PowerManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
Step-4
How to make an android app always run within the background . Now you’ve got to stick this code slightly below the appliance launch activity, with the assistance of this application always running background.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Intent intent = new Intent();
String packageName = getPackageName();
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
if (!pm.isIgnoringBatteryOptimizations(packageName)) {
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
startActivity(intent);
}
}
Step-5
How to run background service continuously in android. Now you’ve got to feature the permission of Battery optimization to the AndroidManifest file of the appliance.
<uses-permission
android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
Step-6
How to keep an app running within the background android programmatically.
Now you’ve got to permit the appliance whitelist permission, which can allow the app to run continuously within the background.
THIS IS ONLY FOR EDUCATIONAL PURPOSE…hope you like this Article…Thanks