package com.edocent.surveyapp; import android.app.IntentService; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.TaskStackBuilder; import android.content.Context; import android.content.Intent; import android.os.Handler; import android.util.Log; import android.widget.Toast; /** * An {@link IntentService} subclass for handling asynchronous task requests in * a service on a separate handler thread. *

* TODO: Customize class - update intent actions and extra parameters. */ public class SampleService extends IntentService { private static final String TAG = SampleService.class.getSimpleName(); public static final int NOTIFICATION_ID = 234; private Handler mHandler; public SampleService() { super("SampleService"); } @Override public int onStartCommand(Intent intent, int flags, int startId){ mHandler = new Handler(); return super.onStartCommand(intent, flags, startId); } @Override protected void onHandleIntent(Intent intent) { Log.v(TAG, "In onHandleIntent. Message will be printed after 10sec"); if (intent != null) { synchronized (this){ try { wait(10000); } catch (InterruptedException e) { e.printStackTrace(); } Log.v(TAG, "Service started"); mHandler.post(new Runnable() { @Override public void run() { Toast.makeText(getApplicationContext(), "Service Completed" , Toast.LENGTH_SHORT).show(); } }); } } } public void createNotification(){ Intent intent = new Intent(this, MainActivity.class); TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(this); taskStackBuilder.addParentStack(MainActivity.class); taskStackBuilder.addNextIntent(intent); PendingIntent pendingIntent = taskStackBuilder. getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); Notification notification = new Notification.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle(getString(R.string.app_name)) .setContentText("Sample Notification") .setAutoCancel(true) .setPriority(Notification.PRIORITY_MAX) .setDefaults(Notification.DEFAULT_VIBRATE) .setContentIntent(pendingIntent) .build(); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(NOTIFICATION_ID, notification); } }