Android background services and alarms

2019-02-20 10:18发布

问题:

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?

回答1:

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.



回答2:

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.