android l04 - notifications and threading

Post on 13-Apr-2017

408 Views

Category:

Software

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

CloudInteraction Design

Android

Toast

Toast

Toast

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

Notifications

Notifications

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

Notifications Priorities

Notifications

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

Notifications

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

ThreadsUI Thread, AsyncTask, Handler

BroadcastReceiver class

Alarms

Alarms / Scheduling

Google Cloud Messaging (GCM) with SyncAdapter

Alarms Use Cases

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

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

ELAPSED_REALTIME_WAKEUP

private  AlarmManager alarmMgr;

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

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);

Back toBroadcastReceiver

class

Fire Alarm on Booting Example

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>

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>

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>

top related