AlarmeService.java
2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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);
}
}