SlideShare une entreprise Scribd logo
1  sur  41
PROGRAMAÇÃO PARA
DISPOSITIVOS MÓVEIS
AlarmManager
Objetivos da aula
 AlarmManager
Qual a maneira mais correta para
disparar uma intent as 15:00:30 de
cada dia?
Android nos facilita com
AlarmManager...
Podemos predizer o futuro...
Podemos disparar uma Intent na data
e hora desejada.
Depois que ativamos o alarme,
podemos até esquecê-lo.
O alarme só será cancelado por nós
ou caso o device seja reiniciado.
public class ExemploAlarme extends Activity {
public void onCreate(Bundle savedInstanceState) {
...
agendarPara10Segundos();
}
}
public class ExemploAlarme extends Activity {
public void onCreate(Bundle savedInstanceState) {
...
agendarPara10Segundos();
}
private void agendarPara10Segundos() {
}
}
public class ExemploAlarme extends Activity {
public void onCreate(Bundle savedInstanceState) {
...
agendarPara10Segundos();
}
private void agendarPara10Segundos() {
Intent intencao = new Intent("ALARME_TESTE");
}
}
public class ExemploAlarme extends Activity {
public void onCreate(Bundle savedInstanceState) {
...
agendarPara10Segundos();
}
private void agendarPara10Segundos() {
Intent intencao = new Intent("ALARME_TESTE");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intencao, 0);
}
}
public class ExemploAlarme extends Activity {
public void onCreate(Bundle savedInstanceState) {
...
agendarPara10Segundos();
}
private void agendarPara10Segundos() {
Intent intencao = new Intent("ALARME_TESTE");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intencao, 0);
Calendar calendario = Calendar.getInstance();
calendario.setTimeInMillis(System.currentTimeMillis());
}
}
public class ExemploAlarme extends Activity {
public void onCreate(Bundle savedInstanceState) {
...
agendarPara10Segundos();
}
private void agendarPara10Segundos() {
Intent intencao = new Intent("ALARME_TESTE");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intencao, 0);
Calendar calendario = Calendar.getInstance();
calendario.setTimeInMillis(System.currentTimeMillis());
calendario.add(Calendar.SECOND, 10);
long agendamentoEmMilis = calendario.getTimeInMillis();
}
}
public class ExemploAlarme extends Activity {
public void onCreate(Bundle savedInstanceState) {
...
agendarPara10Segundos();
}
private void agendarPara10Segundos() {
Intent intencao = new Intent("ALARME_TESTE");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intencao, 0);
Calendar calendario = Calendar.getInstance();
calendario.setTimeInMillis(System.currentTimeMillis());
calendario.add(Calendar.SECOND, 10);
long agendamentoEmMilis = calendario.getTimeInMillis();
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
}
}
public class ExemploAlarme extends Activity {
public void onCreate(Bundle savedInstanceState) {
...
agendarPara10Segundos();
}
private void agendarPara10Segundos() {
Intent intencao = new Intent("ALARME_TESTE");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intencao, 0);
Calendar calendario = Calendar.getInstance();
calendario.setTimeInMillis(System.currentTimeMillis());
calendario.add(Calendar.SECOND, 10);
long agendamentoEmMilis = calendario.getTimeInMillis();
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, agendamentoEmMilis, pendingIntent);
}
}
O AlarmManager irá disparar a Intent
“ALARME_TESTE” após 10 segundos.
Neste período nosso aplicativo pode
ter sido finalizado. Justamente por
isso, precisaremos de um
BroadcastReceiver...
public class ReceberAlarme extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarme recebido!", Toast.LENGTH_LONG).show();
}
}
public class ReceberAlarme extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarme recebido!", Toast.LENGTH_LONG).show();
}
}
public class ReceberAlarme extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarme recebido!", Toast.LENGTH_LONG).show();
}
}
Registramos o BroadcastReceiver
aonde?
<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="br.senai.alarmmanager“ android:versionCode="1“>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name=".ExemploAlarme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".ReceberAlarme">
<intent-filter>
<action android:name="ALARME_TESTE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>
<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="br.senai.alarmmanager“ android:versionCode="1“>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name=".ExemploAlarme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".ReceberAlarme">
<intent-filter>
<action android:name="ALARME_TESTE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>
<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="br.senai.alarmmanager“ android:versionCode="1“>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name=".ExemploAlarme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".ReceberAlarme">
<intent-filter>
<action android:name="ALARME_TESTE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>
<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="br.senai.alarmmanager“ android:versionCode="1“>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name=".ExemploAlarme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".ReceberAlarme">
<intent-filter>
<action android:name="ALARME_TESTE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>
Como cancelar um alarme?
public class ExemploAlarme extends Activity {
public void onCreate(Bundle savedInstanceState) {
...
agendarPara10Segundos();
}
private void agendarPara10Segundos() {
Intent intencao = new Intent("ALARME_TESTE");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intencao, 0);
Calendar calendario = Calendar.getInstance();
calendario.setTimeInMillis(System.currentTimeMillis());
calendario.add(Calendar.SECOND, 10);
long agendamentoEmMilis = calendario.getTimeInMillis();
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, agendamentoEmMilis, pendingIntent);
}
protected void onDestroy() {
super.onDestroy();
}
}
public class ExemploAlarme extends Activity {
public void onCreate(Bundle savedInstanceState) {
...
agendarPara10Segundos();
}
private void agendarPara10Segundos() {
Intent intencao = new Intent("ALARME_TESTE");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intencao, 0);
Calendar calendario = Calendar.getInstance();
calendario.setTimeInMillis(System.currentTimeMillis());
calendario.add(Calendar.SECOND, 10);
long agendamentoEmMilis = calendario.getTimeInMillis();
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, agendamentoEmMilis, pendingIntent);
}
protected void onDestroy() {
super.onDestroy();
Intent it = new Intent("ALARME_TESTE");
}
}
public class ExemploAlarme extends Activity {
public void onCreate(Bundle savedInstanceState) {
...
agendarPara10Segundos();
}
private void agendarPara10Segundos() {
Intent intencao = new Intent("ALARME_TESTE");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intencao, 0);
Calendar calendario = Calendar.getInstance();
calendario.setTimeInMillis(System.currentTimeMillis());
calendario.add(Calendar.SECOND, 10);
long agendamentoEmMilis = calendario.getTimeInMillis();
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, agendamentoEmMilis, pendingIntent);
}
protected void onDestroy() {
super.onDestroy();
Intent it = new Intent("ALARME_TESTE");
PendingIntent pi = PendingIntent.getBroadcast(this, 0, it, 0);
}
}
public class ExemploAlarme extends Activity {
public void onCreate(Bundle savedInstanceState) {
...
agendarPara10Segundos();
}
private void agendarPara10Segundos() {
Intent intencao = new Intent("ALARME_TESTE");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intencao, 0);
Calendar calendario = Calendar.getInstance();
calendario.setTimeInMillis(System.currentTimeMillis());
calendario.add(Calendar.SECOND, 10);
long agendamentoEmMilis = calendario.getTimeInMillis();
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, agendamentoEmMilis, pendingIntent);
}
protected void onDestroy() {
super.onDestroy();
Intent it = new Intent("ALARME_TESTE");
PendingIntent pi = PendingIntent.getBroadcast(this, 0, it, 0);
AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
}
}
public class ExemploAlarme extends Activity {
public void onCreate(Bundle savedInstanceState) {
...
agendarPara10Segundos();
}
private void agendarPara10Segundos() {
Intent intencao = new Intent("ALARME_TESTE");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intencao, 0);
Calendar calendario = Calendar.getInstance();
calendario.setTimeInMillis(System.currentTimeMillis());
calendario.add(Calendar.SECOND, 10);
long agendamentoEmMilis = calendario.getTimeInMillis();
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, agendamentoEmMilis, pendingIntent);
}
protected void onDestroy() {
super.onDestroy();
Intent it = new Intent("ALARME_TESTE");
PendingIntent pi = PendingIntent.getBroadcast(this, 0, it, 0);
AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
manager.cancel(pi);
}
}
E para repetir o alarme a cada 30
segundos?
private void agendarPara10Segundos() {
Intent intencao = new Intent("ALARME_TESTE");
PendingIntent pi = PendingIntent.getBroadcast(this, 0, intencao, 0);
Calendar calendario = Calendar.getInstance();
calendario.setTimeInMillis(System.currentTimeMillis());
calendario.add(Calendar.SECOND, 10);
long emMilis = calendario.getTimeInMillis();
int trintaSegundos = 30 * 1000;
AlarmManager alarmmanager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmmanager.set(AlarmManager.RTC_WAKEUP, emMilis, pi);
}
private void agendarPara10Segundos() {
Intent intencao = new Intent("ALARME_TESTE");
PendingIntent pi = PendingIntent.getBroadcast(this, 0, intencao, 0);
Calendar calendario = Calendar.getInstance();
calendario.setTimeInMillis(System.currentTimeMillis());
calendario.add(Calendar.SECOND, 10);
long emMilis = calendario.getTimeInMillis();
int trintaSegundos = 30 * 1000;
AlarmManager alarmmanager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmmanager.setRepeating(AlarmManager.RTC_WAKEUP, emMilis, pi);
}
private void agendarPara10Segundos() {
Intent intencao = new Intent("ALARME_TESTE");
PendingIntent pi = PendingIntent.getBroadcast(this, 0, intencao, 0);
Calendar calendario = Calendar.getInstance();
calendario.setTimeInMillis(System.currentTimeMillis());
calendario.add(Calendar.SECOND, 10);
long emMilis = calendario.getTimeInMillis();
int trintaSegundos = 30 * 1000;
AlarmManager alarmmanager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmmanager.setRepeating(AlarmManager.RTC_WAKEUP, emMilis, trintaSegundos, pi);
}
Não utilize AlarmManager para
simplesmente fazer uma Thread
dormir por um determinado
momento. Para isso, utilize Handler.
Até a próxima!

Contenu connexe

En vedette

Aula 05/06 (Service)
Aula 05/06 (Service)Aula 05/06 (Service)
Aula 05/06 (Service)Ricardo Longa
 
Android na prática - USCS
Android na prática - USCSAndroid na prática - USCS
Android na prática - USCSRenato
 
Aula 17 04 (Exercícios e ScrollView)
Aula 17 04 (Exercícios e ScrollView)Aula 17 04 (Exercícios e ScrollView)
Aula 17 04 (Exercícios e ScrollView)Ricardo Longa
 
Aula 10 04 (intents)
Aula 10 04 (intents)Aula 10 04 (intents)
Aula 10 04 (intents)Ricardo Longa
 
Android Palestra
Android PalestraAndroid Palestra
Android PalestraRenato
 
Aula 22/05 (Handler)
Aula 22/05 (Handler)Aula 22/05 (Handler)
Aula 22/05 (Handler)Ricardo Longa
 
Aula 5 - 24/04 (Landscape / Portrait)
Aula 5 - 24/04 (Landscape / Portrait)Aula 5 - 24/04 (Landscape / Portrait)
Aula 5 - 24/04 (Landscape / Portrait)Ricardo Longa
 
Aula 05/06 (Notification)
Aula 05/06 (Notification)Aula 05/06 (Notification)
Aula 05/06 (Notification)Ricardo Longa
 
Introduction Android - C.E.S.A.R
Introduction Android - C.E.S.A.RIntroduction Android - C.E.S.A.R
Introduction Android - C.E.S.A.RRenato
 
Pomodoro agil
Pomodoro agilPomodoro agil
Pomodoro agilRenato
 
Treze ferramentas/frameworks para desenvolvimento android
Treze ferramentas/frameworks para desenvolvimento androidTreze ferramentas/frameworks para desenvolvimento android
Treze ferramentas/frameworks para desenvolvimento androidRicardo Longa
 
Aula04 android intents
Aula04 android intentsAula04 android intents
Aula04 android intentsRoberson Alves
 
Aula 6 - 08/05 (SharedPreferences)
Aula 6 - 08/05 (SharedPreferences)Aula 6 - 08/05 (SharedPreferences)
Aula 6 - 08/05 (SharedPreferences)Ricardo Longa
 
K19 k41 Desenvolvimento Mobile com Android
K19 k41 Desenvolvimento Mobile com AndroidK19 k41 Desenvolvimento Mobile com Android
K19 k41 Desenvolvimento Mobile com AndroidAline Diniz
 
Aula 6 - 08/05 (Menu)
Aula 6 - 08/05 (Menu)Aula 6 - 08/05 (Menu)
Aula 6 - 08/05 (Menu)Ricardo Longa
 
Atividades e Intenções (Android)
Atividades e Intenções (Android)Atividades e Intenções (Android)
Atividades e Intenções (Android)Natanael Fonseca
 
Minicurso sobre AndroidAnnotations, GreenDAO, EventBus e Crouton
Minicurso sobre AndroidAnnotations, GreenDAO, EventBus e CroutonMinicurso sobre AndroidAnnotations, GreenDAO, EventBus e Crouton
Minicurso sobre AndroidAnnotations, GreenDAO, EventBus e CroutonRicardo Longa
 

En vedette (20)

Aula 05/06 (Service)
Aula 05/06 (Service)Aula 05/06 (Service)
Aula 05/06 (Service)
 
Android na prática - USCS
Android na prática - USCSAndroid na prática - USCS
Android na prática - USCS
 
Aula 17 04 (Exercícios e ScrollView)
Aula 17 04 (Exercícios e ScrollView)Aula 17 04 (Exercícios e ScrollView)
Aula 17 04 (Exercícios e ScrollView)
 
Aula 12/06 (SQLite)
Aula 12/06 (SQLite)Aula 12/06 (SQLite)
Aula 12/06 (SQLite)
 
Aula 10 04 (intents)
Aula 10 04 (intents)Aula 10 04 (intents)
Aula 10 04 (intents)
 
Android Palestra
Android PalestraAndroid Palestra
Android Palestra
 
Aula 22/05 (Handler)
Aula 22/05 (Handler)Aula 22/05 (Handler)
Aula 22/05 (Handler)
 
Aula 5 - 24/04 (Landscape / Portrait)
Aula 5 - 24/04 (Landscape / Portrait)Aula 5 - 24/04 (Landscape / Portrait)
Aula 5 - 24/04 (Landscape / Portrait)
 
Aula 05/06 (Notification)
Aula 05/06 (Notification)Aula 05/06 (Notification)
Aula 05/06 (Notification)
 
Introduction Android - C.E.S.A.R
Introduction Android - C.E.S.A.RIntroduction Android - C.E.S.A.R
Introduction Android - C.E.S.A.R
 
Pomodoro agil
Pomodoro agilPomodoro agil
Pomodoro agil
 
Treze ferramentas/frameworks para desenvolvimento android
Treze ferramentas/frameworks para desenvolvimento androidTreze ferramentas/frameworks para desenvolvimento android
Treze ferramentas/frameworks para desenvolvimento android
 
Aula04 android intents
Aula04 android intentsAula04 android intents
Aula04 android intents
 
Aula 6 - 08/05 (SharedPreferences)
Aula 6 - 08/05 (SharedPreferences)Aula 6 - 08/05 (SharedPreferences)
Aula 6 - 08/05 (SharedPreferences)
 
K19 k41 Desenvolvimento Mobile com Android
K19 k41 Desenvolvimento Mobile com AndroidK19 k41 Desenvolvimento Mobile com Android
K19 k41 Desenvolvimento Mobile com Android
 
Oficina Sesc Android - V1
Oficina Sesc Android - V1Oficina Sesc Android - V1
Oficina Sesc Android - V1
 
Aula 6 - 08/05 (Menu)
Aula 6 - 08/05 (Menu)Aula 6 - 08/05 (Menu)
Aula 6 - 08/05 (Menu)
 
Atividades e Intenções (Android)
Atividades e Intenções (Android)Atividades e Intenções (Android)
Atividades e Intenções (Android)
 
Minicurso sobre AndroidAnnotations, GreenDAO, EventBus e Crouton
Minicurso sobre AndroidAnnotations, GreenDAO, EventBus e CroutonMinicurso sobre AndroidAnnotations, GreenDAO, EventBus e Crouton
Minicurso sobre AndroidAnnotations, GreenDAO, EventBus e Crouton
 
Tutorial Android - Activities
Tutorial Android - ActivitiesTutorial Android - Activities
Tutorial Android - Activities
 

Similaire à Aula 29/05 (AlarmManager)

Android tutorial (2)
Android tutorial (2)Android tutorial (2)
Android tutorial (2)Kumar
 
Android training in mumbai
Android training in mumbaiAndroid training in mumbai
Android training in mumbaiCIBIL
 
02 programmation mobile - android - (activity, view, fragment)
02 programmation mobile - android - (activity, view, fragment)02 programmation mobile - android - (activity, view, fragment)
02 programmation mobile - android - (activity, view, fragment)TECOS
 
Android Best Practices
Android Best PracticesAndroid Best Practices
Android Best PracticesYekmer Simsek
 
Android Code Puzzles (DroidCon Amsterdam 2012)
Android Code Puzzles (DroidCon Amsterdam 2012)Android Code Puzzles (DroidCon Amsterdam 2012)
Android Code Puzzles (DroidCon Amsterdam 2012)Danny Preussler
 
Paradigmas de linguagens de programacao - aula#9
Paradigmas de linguagens de programacao - aula#9Paradigmas de linguagens de programacao - aula#9
Paradigmas de linguagens de programacao - aula#9Ismar Silveira
 
How to recognise that the user has just uninstalled your android app droidc...
How to recognise that the user has just uninstalled your android app   droidc...How to recognise that the user has just uninstalled your android app   droidc...
How to recognise that the user has just uninstalled your android app droidc...Przemek Jakubczyk
 
How to recognise that the user has just uninstalled your android app
How to recognise that the user has just uninstalled your android appHow to recognise that the user has just uninstalled your android app
How to recognise that the user has just uninstalled your android appPrzemek Jakubczyk
 
Android Lab Test : Using the sensor gyroscope (english)
Android Lab Test : Using the sensor gyroscope (english)Android Lab Test : Using the sensor gyroscope (english)
Android Lab Test : Using the sensor gyroscope (english)Bruno Delb
 
Android accelerometer sensor tutorial
Android accelerometer sensor tutorialAndroid accelerometer sensor tutorial
Android accelerometer sensor tutorialinfo_zybotech
 
Overview of Android Infrastructure
Overview of Android InfrastructureOverview of Android Infrastructure
Overview of Android InfrastructureAlexey Buzdin
 
Overview of Android Infrastructure
Overview of Android InfrastructureOverview of Android Infrastructure
Overview of Android InfrastructureC.T.Co
 

Similaire à Aula 29/05 (AlarmManager) (20)

Introduction toandroid
Introduction toandroidIntroduction toandroid
Introduction toandroid
 
Introduction to android
Introduction to androidIntroduction to android
Introduction to android
 
Android - Anatomy of android elements & layouts
Android - Anatomy of android elements & layoutsAndroid - Anatomy of android elements & layouts
Android - Anatomy of android elements & layouts
 
Android tutorial (2)
Android tutorial (2)Android tutorial (2)
Android tutorial (2)
 
Android training in mumbai
Android training in mumbaiAndroid training in mumbai
Android training in mumbai
 
Broadcast Receiver
Broadcast ReceiverBroadcast Receiver
Broadcast Receiver
 
02 programmation mobile - android - (activity, view, fragment)
02 programmation mobile - android - (activity, view, fragment)02 programmation mobile - android - (activity, view, fragment)
02 programmation mobile - android - (activity, view, fragment)
 
Android Best Practices
Android Best PracticesAndroid Best Practices
Android Best Practices
 
Uninstall opera
Uninstall operaUninstall opera
Uninstall opera
 
Android Code Puzzles (DroidCon Amsterdam 2012)
Android Code Puzzles (DroidCon Amsterdam 2012)Android Code Puzzles (DroidCon Amsterdam 2012)
Android Code Puzzles (DroidCon Amsterdam 2012)
 
Android For All The Things
Android For All The ThingsAndroid For All The Things
Android For All The Things
 
Paradigmas de linguagens de programacao - aula#9
Paradigmas de linguagens de programacao - aula#9Paradigmas de linguagens de programacao - aula#9
Paradigmas de linguagens de programacao - aula#9
 
E:\Plp 2009 2\Plp 9
E:\Plp 2009 2\Plp 9E:\Plp 2009 2\Plp 9
E:\Plp 2009 2\Plp 9
 
How to recognise that the user has just uninstalled your android app droidc...
How to recognise that the user has just uninstalled your android app   droidc...How to recognise that the user has just uninstalled your android app   droidc...
How to recognise that the user has just uninstalled your android app droidc...
 
How to recognise that the user has just uninstalled your android app
How to recognise that the user has just uninstalled your android appHow to recognise that the user has just uninstalled your android app
How to recognise that the user has just uninstalled your android app
 
Android Lab Test : Using the sensor gyroscope (english)
Android Lab Test : Using the sensor gyroscope (english)Android Lab Test : Using the sensor gyroscope (english)
Android Lab Test : Using the sensor gyroscope (english)
 
Integrando sua app Android com Chromecast
Integrando sua app Android com ChromecastIntegrando sua app Android com Chromecast
Integrando sua app Android com Chromecast
 
Android accelerometer sensor tutorial
Android accelerometer sensor tutorialAndroid accelerometer sensor tutorial
Android accelerometer sensor tutorial
 
Overview of Android Infrastructure
Overview of Android InfrastructureOverview of Android Infrastructure
Overview of Android Infrastructure
 
Overview of Android Infrastructure
Overview of Android InfrastructureOverview of Android Infrastructure
Overview of Android Infrastructure
 

Plus de Ricardo Longa

Big Data como Serviço: da captura à visualização de dados com alto desempenho
Big Data como Serviço: da captura à visualização de dados com alto desempenhoBig Data como Serviço: da captura à visualização de dados com alto desempenho
Big Data como Serviço: da captura à visualização de dados com alto desempenhoRicardo Longa
 
JSR 339 - Java API for RESTful Web Services
JSR 339 - Java API for RESTful Web ServicesJSR 339 - Java API for RESTful Web Services
JSR 339 - Java API for RESTful Web ServicesRicardo Longa
 
Android - Programação para dispositivos móveis (Aula 2)
Android - Programação para dispositivos móveis (Aula 2)Android - Programação para dispositivos móveis (Aula 2)
Android - Programação para dispositivos móveis (Aula 2)Ricardo Longa
 
Da introdução à prática no desenvolvimento Android
Da introdução à prática no desenvolvimento AndroidDa introdução à prática no desenvolvimento Android
Da introdução à prática no desenvolvimento AndroidRicardo Longa
 
Da introdução à prática com Drools Expert e Drools Flow
Da introdução à prática com Drools Expert e Drools FlowDa introdução à prática com Drools Expert e Drools Flow
Da introdução à prática com Drools Expert e Drools FlowRicardo Longa
 

Plus de Ricardo Longa (8)

Big Data como Serviço: da captura à visualização de dados com alto desempenho
Big Data como Serviço: da captura à visualização de dados com alto desempenhoBig Data como Serviço: da captura à visualização de dados com alto desempenho
Big Data como Serviço: da captura à visualização de dados com alto desempenho
 
Adopt a JSR
Adopt a JSRAdopt a JSR
Adopt a JSR
 
JSR 339 - Java API for RESTful Web Services
JSR 339 - Java API for RESTful Web ServicesJSR 339 - Java API for RESTful Web Services
JSR 339 - Java API for RESTful Web Services
 
JBoss Forge 2
JBoss Forge 2JBoss Forge 2
JBoss Forge 2
 
Android - Programação para dispositivos móveis (Aula 2)
Android - Programação para dispositivos móveis (Aula 2)Android - Programação para dispositivos móveis (Aula 2)
Android - Programação para dispositivos móveis (Aula 2)
 
Da introdução à prática no desenvolvimento Android
Da introdução à prática no desenvolvimento AndroidDa introdução à prática no desenvolvimento Android
Da introdução à prática no desenvolvimento Android
 
Open Networking
Open NetworkingOpen Networking
Open Networking
 
Da introdução à prática com Drools Expert e Drools Flow
Da introdução à prática com Drools Expert e Drools FlowDa introdução à prática com Drools Expert e Drools Flow
Da introdução à prática com Drools Expert e Drools Flow
 

Dernier

SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 

Dernier (20)

SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 

Aula 29/05 (AlarmManager)

  • 2. Objetivos da aula  AlarmManager
  • 3. Qual a maneira mais correta para disparar uma intent as 15:00:30 de cada dia?
  • 4. Android nos facilita com AlarmManager...
  • 5.
  • 6.
  • 7. Podemos predizer o futuro...
  • 8. Podemos disparar uma Intent na data e hora desejada.
  • 9. Depois que ativamos o alarme, podemos até esquecê-lo.
  • 10. O alarme só será cancelado por nós ou caso o device seja reiniciado.
  • 11.
  • 12. public class ExemploAlarme extends Activity { public void onCreate(Bundle savedInstanceState) { ... agendarPara10Segundos(); } }
  • 13. public class ExemploAlarme extends Activity { public void onCreate(Bundle savedInstanceState) { ... agendarPara10Segundos(); } private void agendarPara10Segundos() { } }
  • 14. public class ExemploAlarme extends Activity { public void onCreate(Bundle savedInstanceState) { ... agendarPara10Segundos(); } private void agendarPara10Segundos() { Intent intencao = new Intent("ALARME_TESTE"); } }
  • 15. public class ExemploAlarme extends Activity { public void onCreate(Bundle savedInstanceState) { ... agendarPara10Segundos(); } private void agendarPara10Segundos() { Intent intencao = new Intent("ALARME_TESTE"); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intencao, 0); } }
  • 16. public class ExemploAlarme extends Activity { public void onCreate(Bundle savedInstanceState) { ... agendarPara10Segundos(); } private void agendarPara10Segundos() { Intent intencao = new Intent("ALARME_TESTE"); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intencao, 0); Calendar calendario = Calendar.getInstance(); calendario.setTimeInMillis(System.currentTimeMillis()); } }
  • 17. public class ExemploAlarme extends Activity { public void onCreate(Bundle savedInstanceState) { ... agendarPara10Segundos(); } private void agendarPara10Segundos() { Intent intencao = new Intent("ALARME_TESTE"); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intencao, 0); Calendar calendario = Calendar.getInstance(); calendario.setTimeInMillis(System.currentTimeMillis()); calendario.add(Calendar.SECOND, 10); long agendamentoEmMilis = calendario.getTimeInMillis(); } }
  • 18. public class ExemploAlarme extends Activity { public void onCreate(Bundle savedInstanceState) { ... agendarPara10Segundos(); } private void agendarPara10Segundos() { Intent intencao = new Intent("ALARME_TESTE"); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intencao, 0); Calendar calendario = Calendar.getInstance(); calendario.setTimeInMillis(System.currentTimeMillis()); calendario.add(Calendar.SECOND, 10); long agendamentoEmMilis = calendario.getTimeInMillis(); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); } }
  • 19. public class ExemploAlarme extends Activity { public void onCreate(Bundle savedInstanceState) { ... agendarPara10Segundos(); } private void agendarPara10Segundos() { Intent intencao = new Intent("ALARME_TESTE"); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intencao, 0); Calendar calendario = Calendar.getInstance(); calendario.setTimeInMillis(System.currentTimeMillis()); calendario.add(Calendar.SECOND, 10); long agendamentoEmMilis = calendario.getTimeInMillis(); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.set(AlarmManager.RTC_WAKEUP, agendamentoEmMilis, pendingIntent); } }
  • 20. O AlarmManager irá disparar a Intent “ALARME_TESTE” após 10 segundos. Neste período nosso aplicativo pode ter sido finalizado. Justamente por isso, precisaremos de um BroadcastReceiver...
  • 21. public class ReceberAlarme extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "Alarme recebido!", Toast.LENGTH_LONG).show(); } }
  • 22. public class ReceberAlarme extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "Alarme recebido!", Toast.LENGTH_LONG).show(); } }
  • 23. public class ReceberAlarme extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "Alarme recebido!", Toast.LENGTH_LONG).show(); } }
  • 25. <?xml version="1.0" encoding="UTF-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="br.senai.alarmmanager“ android:versionCode="1“> <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".ExemploAlarme"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".ReceberAlarme"> <intent-filter> <action android:name="ALARME_TESTE" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </receiver> </application> </manifest>
  • 26. <?xml version="1.0" encoding="UTF-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="br.senai.alarmmanager“ android:versionCode="1“> <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".ExemploAlarme"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".ReceberAlarme"> <intent-filter> <action android:name="ALARME_TESTE" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </receiver> </application> </manifest>
  • 27. <?xml version="1.0" encoding="UTF-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="br.senai.alarmmanager“ android:versionCode="1“> <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".ExemploAlarme"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".ReceberAlarme"> <intent-filter> <action android:name="ALARME_TESTE" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </receiver> </application> </manifest>
  • 28. <?xml version="1.0" encoding="UTF-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="br.senai.alarmmanager“ android:versionCode="1“> <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".ExemploAlarme"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".ReceberAlarme"> <intent-filter> <action android:name="ALARME_TESTE" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </receiver> </application> </manifest>
  • 29. Como cancelar um alarme?
  • 30. public class ExemploAlarme extends Activity { public void onCreate(Bundle savedInstanceState) { ... agendarPara10Segundos(); } private void agendarPara10Segundos() { Intent intencao = new Intent("ALARME_TESTE"); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intencao, 0); Calendar calendario = Calendar.getInstance(); calendario.setTimeInMillis(System.currentTimeMillis()); calendario.add(Calendar.SECOND, 10); long agendamentoEmMilis = calendario.getTimeInMillis(); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.set(AlarmManager.RTC_WAKEUP, agendamentoEmMilis, pendingIntent); } protected void onDestroy() { super.onDestroy(); } }
  • 31. public class ExemploAlarme extends Activity { public void onCreate(Bundle savedInstanceState) { ... agendarPara10Segundos(); } private void agendarPara10Segundos() { Intent intencao = new Intent("ALARME_TESTE"); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intencao, 0); Calendar calendario = Calendar.getInstance(); calendario.setTimeInMillis(System.currentTimeMillis()); calendario.add(Calendar.SECOND, 10); long agendamentoEmMilis = calendario.getTimeInMillis(); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.set(AlarmManager.RTC_WAKEUP, agendamentoEmMilis, pendingIntent); } protected void onDestroy() { super.onDestroy(); Intent it = new Intent("ALARME_TESTE"); } }
  • 32. public class ExemploAlarme extends Activity { public void onCreate(Bundle savedInstanceState) { ... agendarPara10Segundos(); } private void agendarPara10Segundos() { Intent intencao = new Intent("ALARME_TESTE"); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intencao, 0); Calendar calendario = Calendar.getInstance(); calendario.setTimeInMillis(System.currentTimeMillis()); calendario.add(Calendar.SECOND, 10); long agendamentoEmMilis = calendario.getTimeInMillis(); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.set(AlarmManager.RTC_WAKEUP, agendamentoEmMilis, pendingIntent); } protected void onDestroy() { super.onDestroy(); Intent it = new Intent("ALARME_TESTE"); PendingIntent pi = PendingIntent.getBroadcast(this, 0, it, 0); } }
  • 33. public class ExemploAlarme extends Activity { public void onCreate(Bundle savedInstanceState) { ... agendarPara10Segundos(); } private void agendarPara10Segundos() { Intent intencao = new Intent("ALARME_TESTE"); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intencao, 0); Calendar calendario = Calendar.getInstance(); calendario.setTimeInMillis(System.currentTimeMillis()); calendario.add(Calendar.SECOND, 10); long agendamentoEmMilis = calendario.getTimeInMillis(); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.set(AlarmManager.RTC_WAKEUP, agendamentoEmMilis, pendingIntent); } protected void onDestroy() { super.onDestroy(); Intent it = new Intent("ALARME_TESTE"); PendingIntent pi = PendingIntent.getBroadcast(this, 0, it, 0); AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE); } }
  • 34. public class ExemploAlarme extends Activity { public void onCreate(Bundle savedInstanceState) { ... agendarPara10Segundos(); } private void agendarPara10Segundos() { Intent intencao = new Intent("ALARME_TESTE"); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intencao, 0); Calendar calendario = Calendar.getInstance(); calendario.setTimeInMillis(System.currentTimeMillis()); calendario.add(Calendar.SECOND, 10); long agendamentoEmMilis = calendario.getTimeInMillis(); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.set(AlarmManager.RTC_WAKEUP, agendamentoEmMilis, pendingIntent); } protected void onDestroy() { super.onDestroy(); Intent it = new Intent("ALARME_TESTE"); PendingIntent pi = PendingIntent.getBroadcast(this, 0, it, 0); AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE); manager.cancel(pi); } }
  • 35. E para repetir o alarme a cada 30 segundos?
  • 36. private void agendarPara10Segundos() { Intent intencao = new Intent("ALARME_TESTE"); PendingIntent pi = PendingIntent.getBroadcast(this, 0, intencao, 0); Calendar calendario = Calendar.getInstance(); calendario.setTimeInMillis(System.currentTimeMillis()); calendario.add(Calendar.SECOND, 10); long emMilis = calendario.getTimeInMillis(); int trintaSegundos = 30 * 1000; AlarmManager alarmmanager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmmanager.set(AlarmManager.RTC_WAKEUP, emMilis, pi); }
  • 37. private void agendarPara10Segundos() { Intent intencao = new Intent("ALARME_TESTE"); PendingIntent pi = PendingIntent.getBroadcast(this, 0, intencao, 0); Calendar calendario = Calendar.getInstance(); calendario.setTimeInMillis(System.currentTimeMillis()); calendario.add(Calendar.SECOND, 10); long emMilis = calendario.getTimeInMillis(); int trintaSegundos = 30 * 1000; AlarmManager alarmmanager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmmanager.setRepeating(AlarmManager.RTC_WAKEUP, emMilis, pi); }
  • 38. private void agendarPara10Segundos() { Intent intencao = new Intent("ALARME_TESTE"); PendingIntent pi = PendingIntent.getBroadcast(this, 0, intencao, 0); Calendar calendario = Calendar.getInstance(); calendario.setTimeInMillis(System.currentTimeMillis()); calendario.add(Calendar.SECOND, 10); long emMilis = calendario.getTimeInMillis(); int trintaSegundos = 30 * 1000; AlarmManager alarmmanager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmmanager.setRepeating(AlarmManager.RTC_WAKEUP, emMilis, trintaSegundos, pi); }
  • 39. Não utilize AlarmManager para simplesmente fazer uma Thread dormir por um determinado momento. Para isso, utilize Handler.
  • 40.