Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CloseOrderService extends Service {
- private Notification notification;
- private SharedPreferences mPrefs;
- private ExecutorService es;
- @Override
- public void onCreate() {
- super.onCreate();
- es = Executors.newFixedThreadPool(3);
- }
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- Notification.Builder builder = new Notification.Builder(this)
- .setSmallIcon(R.drawable.ic_pizza_notification).setContentTitle("Title").setContentText("some content" + startId);
- if (Build.VERSION.SDK_INT < 16)
- notification = builder.getNotification();
- else
- notification = builder.build();
- startForeground(772, notification);
- MyRun mr = new MyRun(startId);
- es.execute(mr);
- return Service.START_NOT_STICKY;
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- Log.e(TAG,"Destroy service");
- stopForeground(true);
- }
- @Nullable
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- class MyRun implements Runnable {
- final int startId;
- final long orderId;
- public MyRun(int startId) {
- this.startId = startId;
- }
- public void run() {
- try {
- TimeUnit.SECONDS.sleep(Settings.WAIT_BEFORE_CLOSE_ORDER_SEC);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- //send some data to server
- stop();
- }
- void stop() {
- Log.d(TAG, "MyRun#" + startId + " end, stopSelfResult("
- + startId + ") = " + stopSelfResult(startId));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement