0

I Have a service in android app and I want to run in the background continuously even app is killed from task Manager.My current code works good for pre-lollipop versions.But in lollipop it is not worked(In lollipop it stops from task manager).How to resolve this.

Service class

public class MyService extends Service {

    int total_time = 1000 * 60 * 60 * 24; // total one day you can change
    int peroid_time = 10000; // one hour time is assumed to make request

@Override
    public void onCreate() {

    }

    /** The service is starting, due to a call to startService() */
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {


        new CountDownTimer(total_time, peroid_time) {


            public void onTick(long millisUntilFinished) {
                // make request to web and get reponse and show notification.
                MakingWebRequest();

                Toast.makeText(MyService.this, " Tesitng the data", Toast.LENGTH_LONG).show();
            }


            public void onFinish() {

                Toast.makeText(MyService.this, " ending............", Toast.LENGTH_LONG).show();
                //
            }
        }.start();
        // Let it continue running until it is stopped.
        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
        return START_STICKY;

    }

    public void MakingWebRequest() {

                                NotificationManager notif = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                                NotificationCompat.Builder builder = new NotificationCompat.Builder(MyService.this);
                                Notification notify ;
                                PendingIntent pending = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0);

                                notify = builder.setContentIntent(pending)
                                        .setStyle(new NotificationCompat.BigTextStyle().bigText(message1))
                                        .setSmallIcon(R.drawable.push).setTicker(excep).setWhen(System.currentTimeMillis())
                                        .setAutoCancel(true).setContentTitle(message1)
                                        .setContentText(message1).build();
//                notif.notify(NOTIFICATION, notify);
//                                notif.notify(0, notify);
                                    startForeground(1, notify);

        }



    }

    /** A client is binding to the service with bindService() */
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    /** Called when all clients have unbound with unbindService() */
    @Override
    public boolean onUnbind(Intent intent) {
        return mAllowRebind;
    }

    /** Called when a client is binding to the service with bindService()*/
    @Override
    public void onRebind(Intent intent) {

    }

Main Activity

Intent serviceIntent = new Intent(this, MyService.class);
        serviceIntent.setPackage(this.getPackageName());
        startService(serviceIntent);
Adi
  • 400
  • 8
  • 25
  • So you want something done once every hour? You don't need a continuous service for that. You fire a service every hour. Google GcmNetworkManager and PeriodicTask. – Eugen Pechanec Aug 08 '16 at 07:40
  • I don't want gcm like things..Plz stay on my code and logic ,Just how to prevent stoping the app – Adi Aug 08 '16 at 08:10
  • You can't prevent it. I offer you an alternative which also happens to be best practice. GCM != GcmNetworkManager. GcmNetworkManager = JobScheduler, which is exactly what you need. It runs a task periodically when network is available (if you set it up so). Yes, sometimes we have to rewrite code. – Eugen Pechanec Aug 08 '16 at 08:19

1 Answers1

0

This is an Android bug.

See https://groups.google.com/forum/#!topic/android-developers/mRDeMHVVS3Q and https://code.google.com/p/android/issues/detail?id=53313#c1 there are workarounds suggested using onTaskRemoved().

David Wasser
  • 93,459
  • 16
  • 209
  • 274