Is it possible to run service in android platform continuously even after locking the device?

后端 未结 8 977
小鲜肉
小鲜肉 2020-12-16 05:44

We have been working on developing service for android platform.

In our service we need to send GPS data (Lat and Long) of device to some external REST service afte

相关标签:
8条回答
  • 2020-12-16 06:48

    Yes you can implement a background service that it will almost never be killed. But you have to declare it to run in the foreground. you can see what Android Developer site says, by referring to this url(http://developer.android.com/guide/components/services.html) also in this article (http://developer.android.com/guide/components/processes-and-threads.html) they say,

    There are five levels in the importance hierarchy and the different types of processes in order of importance (the first process is most important and is killed last):

    1. Foreground process:

    A process that is required for what the user is currently doing. A process is considered to be in the foreground if any of the following conditions are true:

    • It hosts an Activity that the user is interacting with (the Activity's onResume() method has been called).
    • It hosts a Service that's bound to the activity that the user is interacting with.
    • It hosts a Service that's running "in the foreground"—the service has called startForeground().
    • It hosts a Service that's executing one of its lifecycle callbacks (onCreate(), onStart(), or onDestroy()).
    • It hosts a BroadcastReceiver that's executing its onReceive() method.

    Generally, only a few foreground processes exist at any given time. They are killed only as a last resort—if memory is so low that they cannot all continue to run. Generally, at that point, the device has reached a memory paging state, so killing some foreground processes is required to keep the user interface responsive.

    So you have to start your service in the foreground. In order to do this you have implement the service as below.

    public class MyForegroundService extends Service {
    
        @Override
        public void onCreate() {
            super.onCreate();
            //your code goes here
        }
    
        @Override
        public IBinder onBind(Intent intent) {
            throw new UnsupportedOperationException("Not yet implemented");
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            keepServiceAlive();
            //your code goes here
            return(START_NOT_STICKY);
        }
    
        private void keepServiceAlive() {
            Intent notificationIntent = new Intent(this, MainActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    
            Notification notification = new NotificationCompat.Builder(this).setContentTitle(getString(R.string.app_name))
                    .setContentText("Hello")
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentIntent(pendingIntent)
                    .build();
            startForeground(Notification.FLAG_ONGOING_EVENT, notification);
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
            Log.w(getClass().getName(), "Got to stop()!");
            stopForeground(true);
        }
    }
    

    Thanks and gud luck..

    0 讨论(0)
  • 2020-12-16 06:48
    In Manifest file,
    <service android:name=".MyService"></service>
    

    MyService.java

    public class MyService extends Service {
    
        @Override
        public void onCreate() {
            super.onCreate();
           // your code here
        }
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            return START_STICKY;
        }
        @Override
        public void onDestroy() {
            super.onDestroy();
            Intent it = new Intent(MyService.this, MyService.class);
            getApplication().startService(it); // If service will destroy, Start the service again
        }
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }
    
    
    }
    

    to run the service, add this to your Activity,

        Intent it = new Intent(getApplication(), MyService.class);
    getApplicationContext().startService(it);
    
    0 讨论(0)
提交回复
热议问题