Android background services and alarm

I recently ran into a problem when Android 4.4 killed my Service app and AlarmManager when the device goes into sleep mode ( START_STICKY param doesn't help). I tried a lot, but nothing works as I need.

In my task manager application, I always see many application processes other than the default, such as Google+, Skype, Google Drive, and some others that work in real time and have never been killed by the system.

I want to ask more experienced developers to create a Service or Alarm that will not be killed by the system, or is this not possible on some Android builds?

+1
source share
2 answers

This is not a kind of programming answer, because the reason does not work outside of your code. I cheated on this even before two weeks. My service always stopped, and I never understood why, until I find the "Power Saving" option in the settings (my Huawei Ascend Mate 7 device) Go to:

Settings โ†’ Power Saver โ†’ at the top of Power Info โ†’ continue to work after turning off the screen.

Here you can enable or disable applications that should run in the background if the screen goes into sleep mode.

Also, if this is not the correct answer, too long for a comment, and perhaps it helps others with this problem.

+4
source

We do this using our existing application to support the user in our application, even if the home button is pressed. I modified the code a bit to post an example. We are launching Android 4.4.3.

You can keep your activity by running the background service. You keep the task at the forefront of the device. First, get the task id and start the service

 public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); int taskID = getTaskId(); Intent intent = new Intent(this, ServiceKeepInApp.class); intent.putExtra("TaskID", taskId); startService(intent); } } 

Your service class will continue to work with the handler:

 public class ServiceKeepInApp extends Service { private boolean sendHandler = false; private int taskID = -1; Handler taskHandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); ActivityManager activityManager = (ActivityManager)getSystemService(Service.ACTIVITY_SERVICE); if (taskID != -1) { if (activityManager.getRecentTasks(2, 0).get(0).id != taskID) { activityManager.moveTaskToFront(taskID, 0); } } if (sendHandler) { taskHandler.sendEmptyMessageDelayed(0, 1000); } } }; @Override public void onCreate() { Bundle extras = getIntent().getExtras(); if (extras != null) { taskID = extras.getInt("TaskID"); } super.onCreate(); sendHandler = true; taskHandler.sendEmptyMessage(0); } @Override public void onDestroy() { super.onDestroy(); sendHandler = false; } @Override public int onStartCommand(Intent intent, int flags, int startId) { return START_STICKY; } @Override public IBinder onBind(Intent arg0) { return null; } } 

This is an unverified code - so beware of the buyer.

+1
source

Source: https://habr.com/ru/post/1399486/


All Articles