Android background services and alarms
Asked Answered
M

2

1

Recently encountered with a problem when Android 4.4 killed my app's Service and AlarmManager when device going into a sleep mode (START_STICKY param doesn't helps). I tried a lot of things, but nothing works as I need.

In my task manager app I always see a lot of processes of non-default apps such as Google+, Skype, Google Drive end some else that working in real time and never were killed by the system.

I want to ask more experienced developers how to create Service or Alarm that will not be killed by the system or this is impossible on some Android assemblies?

Millisecond answered 22/4, 2015 at 13:31 Comment(0)
D
4

This is not a kind of programming answer, because the reason of not working is not inside Your code. I stumpled about that too before two weeks. My service allways stopped and I never understood why, until I detected the "Power Saving" Option inside the settings (my device Huawei Ascend Mate 7) Navigate:

Settings --> Power Saving --> on the top Power Info --> keep running after screen off.

Here You can enable or disable apps, that should run in the background if the screen goes to sleep.

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

Dyke answered 22/4, 2015 at 13:55 Comment(2)
Oh my God... I spent on this problem one day, I tried a lot of things but solution was in Power Saving mode whitelist ))))Millisecond
:) ...... I was also suprised with my problem. Sometimes we should also read manuals from our devices :)Dyke
M
1

We do this with our existing app to keep the user within our app even if the home button is hit. I modified the code a little so I could post an example. We are running Android 4.4.3.

You can keep your activity running by running a background service. You keep the task in 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, keep alive with a 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 untested code - so buyer beware.

Monitor answered 22/4, 2015 at 14:27 Comment(2)
This answer is off topic but interesting and I will save it for the future. Can U told me for what task did U write this code?Millisecond
@Millisecond Sure, we are doing electronic diary data tracking on Nexus 7 tablets and we needed to keep the app alive and running at all times and not let them venture onto other apps.Monitor

© 2022 - 2024 — McMap. All rights reserved.