-1

I want my service worked even after the application was to complete. It continued to support the work, and does not restart. I know this can be achieved in many applications while downloading the file, the service continues to run smoothly all, I want to achieve the same

Sample code to make it clear what is at stake

public class CacheFile extends Service {

Context context;
NotificationManager mNotificationManager;
int id = 100;

@Override
public void onCreate() {
    context = this;
    mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    toDownLoad();
    return super.onStartCommand(intent, flags, startId);
}


public void toDownLoad() {
    final NotificationCompat.Builder mBuilder;
    Intent deleteIntent = new Intent(this, ClickNotification.class);
    PendingIntent pendingIntentCancel = PendingIntent.getBroadcast(this, 0, deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(android.R.drawable.ic_menu_upload)
            .setContentTitle("Uploading Media...")
            .setTicker("Starting uploads")
            .addAction(android.R.drawable.ic_menu_close_clear_cancel, "Пауза", pendingIntentCancel)
            .addAction(android.R.drawable.ic_menu_close_clear_cancel, "Отмена", pendingIntentCancel);
    Intent notificationIntent = new Intent(this, MyActivity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(pendingIntent);
    mBuilder.setProgress(id, 0, true);
    new Thread(
            new Runnable() {
                @Override
                public void run() {
                    int incr;
                    for (incr = 0; incr <= 100; incr+=5) {
                        mBuilder.setProgress(100, incr, false);
                        mNotificationManager.notify(id, mBuilder.build());
                        try {
                            Thread.sleep(5*1000);
                        } catch (InterruptedException e) { }
                    }
                    mBuilder.setContentText("Download complete")
                            .addAction(0, null, null)
                            .addAction(0, null, null)
                            .setProgress(0, 0, false);
                    mNotificationManager.notify(id, mBuilder.build());
                }
            }
    ).start();
    mNotificationManager.notify(id, mBuilder.build());
}

@Override
public void onDestroy() {
    mNotificationManager.cancelAll();
    super.onDestroy();
}

}

Anton Polst
  • 119
  • 1
  • 2

1 Answers1

-1

Even if your application is closed, your Service keeps running. Service runs in its own process, you don't need a new Thread. You should put the code of method toDownLoad() inside the doInBackground() method of the Service. Have a look at the doc : http://developer.android.com/reference/android/app/Service.html

issathink
  • 1,220
  • 1
  • 15
  • 25
  • No, it is restarted. If this service is used as an audio player, then after you close the application, the service will restart, and it will be seen that the song began to play again, in many applications the music continues to play – Anton Polst Dec 28 '14 at 00:46
  • Here a quote from the official doc : "There are two reasons that a service can be run by the system. If someone calls Context.startService() then the system will retrieve the service (creating it and calling its onCreate() method if needed) and then call its onStartCommand(Intent, int, int) method with the arguments supplied by the client. The service will at this point continue running until Context.stopService() or stopSelf() is called." – issathink Dec 28 '14 at 00:49