AlarmeService.java 2.07 KB
package com.example.martin.projetv5;

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.Vibrator;

import static android.app.Notification.FLAG_AUTO_CANCEL;

/**
 * Created by martin on 15/03/2017.
 */

public class AlarmeService extends IntentService {
    private final static String TAG ="IntentServiceExample";
    public int ID_NOTIFICATION = 0;

    public AlarmeService(){
        super(TAG);
    }

    @Override
    protected void onHandleIntent(Intent intent){
        int icon = R.drawable.pill;
        long when = System.currentTimeMillis();

        Vibrator vib = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
        long pattern[] = {0,400,200,400};
        vib.vibrate(pattern,-1);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(),0,intent,0);

        Notification.Builder builder = new Notification.Builder(getApplicationContext())
                .setWhen(when)
                .setTicker(getString(R.string.notif1))
                .setSmallIcon(icon)
                .setContentTitle("Tech' your pill")
                .setContentText(getString(R.string.notif1))
                .setContentIntent(contentIntent);

        Intent resultIntent = new Intent(this,MainActivity.class);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addNextIntent(intent);
        PendingIntent resultPendingIntent = PendingIntent.getActivity(this,0,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(resultPendingIntent);

        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notif = builder.build();
        notif.flags = FLAG_AUTO_CANCEL;
        manager.notify(ID_NOTIFICATION,notif);
    }
}