23
Cloud Interaction Design Android

Android L04 - Notifications and Threading

Embed Size (px)

Citation preview

Page 1: Android L04 - Notifications and Threading

CloudInteraction Design

Android

Page 2: Android L04 - Notifications and Threading

Toast

Page 3: Android L04 - Notifications and Threading

Toast

Page 4: Android L04 - Notifications and Threading

Toast

Context  context  =  getApplicationContext();                CharSequence text  =  "Hello  toast!";                int duration  =  Toast.LENGTH_SHORT;                Toast  toast  =  Toast.makeText(context,   text,  duration);                toast.show();

Page 5: Android L04 - Notifications and Threading

Notifications

Page 6: Android L04 - Notifications and Threading

Notifications

Page 7: Android L04 - Notifications and Threading

Five priority levels: – PRIORITY_MIN (-2) to PRIORITY_MAX (2); – if not set, the priority defaults to PRIORITY_DEFAULT (0).

Notifications Priorities

Page 8: Android L04 - Notifications and Threading

Notifications

NotificationCompat.Builder mBuilder = new  NotificationCompat.Builder(this)                                                .setSmallIcon(android.R.drawable.stat_notify_more)                                                .setContentTitle("My   notification")                                                .setContentText("Hello   World!");

Page 9: Android L04 - Notifications and Threading

Notifications

NotificationCompat.Builder mBuilder = new  NotificationCompat.Builder(this)                                                .setSmallIcon(android.R.drawable.stat_notify_more)                                                .setContentTitle("My   notification")                                                .setContentText("Hello   World!");

Page 10: Android L04 - Notifications and Threading

ThreadsUI Thread, AsyncTask, Handler

Page 11: Android L04 - Notifications and Threading

BroadcastReceiver class

Page 12: Android L04 - Notifications and Threading

Alarms

Page 13: Android L04 - Notifications and Threading

Alarms / Scheduling

Page 14: Android L04 - Notifications and Threading

Google Cloud Messaging (GCM) with SyncAdapter

Page 15: Android L04 - Notifications and Threading

Alarms Use Cases

• MMS – Retry Scheduler• Bluetooth [Discoverable] Timeout• Notifications• Syncing data with the server• ..etc.

Page 16: Android L04 - Notifications and Threading

Alarms Firing

• ELAPSED_REALTIME– Fires the pending intent based on the amount of time since the device was booted, but doesn't

wake up the device. The elapsed time includes any time during which the device was asleep.• ELAPSED_REALTIME_WAKEUP

– Wakes up the device and fires the pending intent after the specified length of time has elapsed since device boot.

• RTC– Fires the pending intent at the specified time but does not wake up the device.

• RTC_WAKEUP– Wakes up the device to fire the pending intent at the specified time.

https://developer.android.com/training/scheduling/alarms.html

Page 17: Android L04 - Notifications and Threading

ELAPSED_REALTIME_WAKEUP

private  AlarmManager alarmMgr;

alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,AlarmManager.INTERVAL_HALF_HOUR,AlarmManager.INTERVAL_HALF_HOUR,  alarmIntent);

Page 18: Android L04 - Notifications and Threading

RTC

private  AlarmManager alarmMgr;

//  Set  the  alarm  to  start  at  approximately  2:00  p.m.Calendar  calendar  =  Calendar.getInstance();calendar.setTimeInMillis(System.currentTimeMillis());calendar.set(Calendar.HOUR_OF_DAY,  14);

alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,  calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,alarmIntent);

Page 19: Android L04 - Notifications and Threading

Back toBroadcastReceiver

class

Page 20: Android L04 - Notifications and Threading

Fire Alarm on Booting Example

Page 21: Android L04 - Notifications and Threading

BroadcastReceiver on Booting

• Add a permission:<uses-­‐permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

• Implement a BroadcastReceiver called SampleBootReceiver:public  class  SampleBootReceiver extends  BroadcastReceiver {

@Overridepublic  void  onReceive(Context  context,  Intent  intent)  {

if  (intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))  {//  Set  the  alarm  here.

}}

}

• Set the SampleBootReceiver to fire on Boot:<receiver  android:name=".SampleBootReceiver"

android:enabled="false"><intent-­‐filter>

<action  android:name="android.intent.action.BOOT_COMPLETED"></action></intent-­‐filter>

</receiver>

Page 22: Android L04 - Notifications and Threading

BroadcastReceiver on Booting

• Add a permission:<uses-­‐permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

• Implement a BroadcastReceiver called SampleBootReceiver:public  class  SampleBootReceiver extends  BroadcastReceiver {

@Overridepublic  void  onReceive(Context  context,  Intent  intent)  {

if  (intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))  {//  Set  the  alarm  here.

}}

}

• Set the SampleBootReceiver to fire on Boot:<receiver  android:name=".SampleBootReceiver"

android:enabled="false"><intent-­‐filter>

<action  android:name="android.intent.action.BOOT_COMPLETED"></action></intent-­‐filter>

</receiver>

Page 23: Android L04 - Notifications and Threading

BroadcastReceiver on Booting

• Add a permission:<uses-­‐permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

• Implement a BroadcastReceiver called SampleBootReceiver:public  class  SampleBootReceiver extends  BroadcastReceiver {

@Overridepublic  void  onReceive(Context  context,  Intent  intent)  {

if  (intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))  {//  Set  the  alarm  here.

}}

}

• Set the SampleBootReceiver to fire on Boot:<receiver  android:name=".SampleBootReceiver"

android:enabled="false"><intent-­‐filter>

<action  android:name="android.intent.action.BOOT_COMPLETED"></action></intent-­‐filter>

</receiver>