Skip to content

Instantly share code, notes, and snippets.

@thatfiredev
Last active January 9, 2018 22:28
Show Gist options
  • Save thatfiredev/04cd67cf9526b4349d873644abd96b9b to your computer and use it in GitHub Desktop.
Save thatfiredev/04cd67cf9526b4349d873644abd96b9b to your computer and use it in GitHub Desktop.
Utilizado no artigo O job dos Jobs - JobServices https://medium.com/@rosariopfernandes/job2-fbf022368795
public class NotificationJobService extends JobService{
private final String TAG = NotificationJobService.class.getSimpleName();
private AsyncTask backgroundTask;
@Override
public boolean onStartJob(JobParameters job) {
//Executar todo trabalho no background
backgroundTask = new AsyncTask()
{
@Override
protected Object doInBackground(Object[] objects){
//Executar qualquer trabalho que for necessário
return null;
}
@Override
protected void onPostExecute(Object o)
{
notificar("Olha Que Novidade","Já sei utilizar JobServices!")
jobFinished(job, false);
}
};
mBackgroundTask.execute();
return true; //Ainda há algo a ser executado?
//Retornamos true porque o trabalho ainda está sendo executado dentro
//do AsyncTask.
//Se fosse uma operação mais simples retornavamos false.
}
private void notificar(String title, String text)
{
NotificationCompat.Builder notification =
new NotificationCompat.Builder(getApplicationContext());
notification.setStyle(new NotificationCompat.BigTextStyle());
notification.setSmallIcon(R.drawable.icon);
notification.setContentText(text);
notification.setContentTitle(title);
notification.setPriority(NotificationCompat.PRIORITY_MIN);
notification.setAutoCancel(true);
Intent update = new Intent(Intent.ACTION_VIEW);
TaskStackBuilder stackBuilder1 = TaskStackBuilder.create(getApplicationContext());
stackBuilder1.addNextIntent(update);
PendingIntent updateIntent = stackBuilder1.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
notification.setContentIntent(updateIntent);
NotificationManagerCompat.from(getApplicationContext())
.notify(100, notification.build());
}
@Override
public boolean onStopJob(JobParameters job) {
return false;//Tentar novamente?
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment