Notifying from the Background

Slides:



Advertisements
Similar presentations
CSS216 MOBILE PROGRAMMING Android, Chapter 9 Book: “Professional Android™ 2 Application Development” by Reto Meier, 2010 by: Andrey Bogdanchikov (
Advertisements

Manifest File, Intents, and Multiple Activities. Manifest File.
Cosc 4755 Android Notifications. There are a couple of ways to notify users with interrupting what they are doing The first is Toast, use the factory.
Notifications & Alarms.  Notifications  Alarms.
Cosc 4730 Android Dialogs and notifications. Notifications There are a couple of ways to notify users without interrupting what they are doing The first.
1 Working with the Android Services Nilanjan Banerjee Mobile Systems Programming University of Arkansas Fayetteville, AR
System broadcasts and services. System broadcast events. EventDescription Intent.ACTION_BOOT_COMPLETEDBoot completed. Requires the android.permission.RECE.
@2011 Mihail L. Sichitiu1 Android Introduction Hello Views Part 1.
Developing Push Notifications (C2DM) for Android Vijai Co-Founder Adhish Technologies, Sweet’N’Spicy apps.
8. Notification과 Alarm.
Homescreen Widgets Demystified Pearl AndroidTO // Oct 26, 2010.
Integrating with Android Services. Introduction  Android has numerous built-in functionality that can be called from within your applications  SMS/MMS.
Cosc 5/4730 Broadcast Receiver. Broadcast receiver A broadcast receiver (short receiver) – is an Android component which allows you to register for system.
COMP 365 Android Development.  Perform operations in the background  Services do not have a user interface (UI)  Can run without appearing on screen.
DUE Hello World on the Android Platform.
1 Mobile Software Development Framework: Android 2/28/2011 Y. Richard Yang.
Threads and Services. Background Processes One of the key differences between Android and iPhone is the ability to run things in the background on Android.
SpotOn Game App Android How to Program © by Pearson Education, Inc. All Rights Reserved.
Understand applications and their components activity service broadcast receiver content provider intent AndroidManifest.xml.
Android Dev Tips I Catch run-time exceptions, send crash reports and still remain user friendly Stefan Anca
Cosc 5/4730 Android Communications Intents, callbacks, and setters.
User notification Android Club Agenda Toast Custom Toast Notification Dialog.
Themes and Menus: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition.
Services 1 CS440. What is a Service?  Component that runs on background  Context.startService(), asks the system to schedule work for the service, to.
Activity Android Club Agenda Hello Android application Application components Activity StartActivity.
Mobile Programming Midterm Review
Threads II IS Outline  Quiz  Thread review  Stopping a thread  java.util.Timer  Swing threads javax.swing.Timer  ProgressMonitor.
Styles, Dialog Boxes, and Menus. Styles Allow creation of a common format – placed in res/values/styles.xml – file name is incidental Can be applied.
Activities and Intents Richard S. Stansbury 2015.
Services Background operating component without a visual interface Running in the background indefinitely Differently from Activity, Service in Android.
User Interface Layout Interaction. EventsEvent Handlers/Listeners Interacting with a user.
Android Alert Dialog. Alert Dialog Place Button to open the dialog. public class MainActivity extends ActionBarActivity { private static Button button_sbm;
Dextrosoft SCHEDULED PHONE BACKUP Backup your mobile life Version Copyright © 2015 Dextrosoft Private Limited. All Rights Reserved.
Уведомление пользователя ANDROID CLUB Сегодня  Toast  Диалог  Уведомление.
Class on Fragments & threads. Fragments fragment is a modular section of an activity, which has its own lifecycle, receives its own input events, and.
Speech Service & client(Activity) 오지영.
Intents and Broadcast Receivers Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution.
Mobile Software Development for Android - I397 IT COLLEGE, ANDRES KÄVER, WEB:
Services. What is a Service? A Service is not a separate process. A Service is not a thread. A Service itself is actually very simple, providing two main.
Developing Android Services. Objectives Creating a service that runs in background Performing long-running tasks in a separate thread Performing repeated.
Cosc 4735 Nougat API 24+ additions.
Concurrency in Android
Intents and Broadcast Receivers
Mobile Software Development for Android - I397
Broadcast receivers.
CS371m - Mobile Computing Services and Broadcast Receivers
Android Notifications
Messaging Unit-4.
Notifications and Services
Mobile Software Development for Android - I397
Android training in Chandigarh. What is ADB ADB stands for Android Debug Bridge. It is a command line tool that is used to communicate with the emulator.
Mobile Computing With Android ACST 4550 Alerts
Android Programming Lecture 9
Threads II IS
Developing Android Services
Android Notifications (Part 1)
Android Notifications (Part 2) Plus Alarms and BroadcastReceivers
CIS 470 Mobile App Development
Android Topics UI Thread and Limited processing resources
Android Developer Fundamentals V2
Activities and Intents
Android Notifications
Android Developer Fundamentals V2 Lesson 5
Objects First with Java
Service Services.
SE4S701 Mobile Application Development
Implication of Orientation Changes
Activities, Fragments, and Intents
Mobile Programming Broadcast Receivers.
CA16R405 - Mobile Application Development (Theory)
Presentation transcript:

Notifying from the Background NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); Intent launchIntent = new Intent(getApplicationContext(), NotificationActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, launchIntent, 0); //Create notification with the time it was fired Notification note = new Notification(R.drawable.icon, "Something Happened", System.currentTimeMillis()); //Set notification information note.setLatestEventInfo(getApplicationContext(), "We're Finished!", "Click Here!", contentIntent); note.defaults |= Notification.DEFAULT_SOUND; note.flags |= Notification.FLAG_AUTO_CANCEL; manager.notify(NOTE_ID, note); FLAG_INSISTENT - Repeats the Notification sounds until the user responds. FLAG_NO_CLEAR - Does not allow the Notification to be cleared with the user’s “Clear Notifications” button; only through a call to cancel(). Notifying from the Background

Creating Timed and Periodic Tasks private Handler mHandler = new Handler(); private Runnable timerTask = new Runnable() { @Override public void run() { Calendar now = Calendar.getInstance(); mClock.setText(String.format("%02d:%02d:%02d", now.get(Calendar.HOUR), now.get(Calendar.MINUTE), now.get(Calendar.SECOND)) ); //Schedule the next update in one second mHandler.postDelayed(timerTask,1000); } }; @Override public void onResume() { super.onResume(); mHandler.post(timerTask); } @Override public void onPause() { super.onPause(); mHandler.removeCallbacks(timerTask); } } Creating Timed and Periodic Tasks

Periodic Task Scheduling a public class AlarmReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //display the current time Calendar now = Calendar.getInstance(); DateFormat formatter = SimpleDateFormat.getTimeInstance(); Toast.makeText(context, formatter.format(now.getTime()), Toast.LENGTH_SHORT).show(); } }

Periodic Task Scheduling a Intent launchIntent = new Intent(this, AlarmReceiver.class); mAlarmIntent = PendingIntent.getBroadcast(this, 0, launchIntent, 0); public void onClick(View v) { AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); long interval = 5*1000; //5 seconds switch(v.getId()) { case R.id.start: Toast.makeText(this, "Scheduled", Toast.LENGTH_SHORT).show(); manager.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime()+interval, interval, mAlarmIntent); break; case R.id.stop: Toast.makeText(this, "Canceled", Toast.LENGTH_SHORT).show(); manager.cancel(mAlarmIntent); default: break; }

Creating Sticky Operations IntentService is a wrapper around Android’s base Service implementation, the key component to doing work in the background without interaction from the user. Running Persistent Background Operations running in the background indefinitely, performing some operation or monitoring certain events to occur. public void startTracking() { if(!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { return; } Toast.makeText(this, "Starting Tracker", Toast.LENGTH_SHORT).show(); manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 0, this); isTracking = true; } public void stopTracking() { Toast.makeText(this, "Stopping Tracker", Toast.LENGTH_SHORT).show(); manager.removeUpdates(this); isTracking = false; } public void onCreate() { manager = (LocationManager)getSystemService( LOCATION_SERVICE); storedLocations = new ArrayList<Location>(); Log.i(LOGTAG, "Tracking Service Running..."); }

Launching Other Applications private void viewPdf(Uri file) { Intent intent; intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(file, "application/pdf"); try { startActivity(intent); } catch (ActivityNotFoundException e) { //No application to view, ask to download one AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("No Application Found"); builder.setMessage("We could not find an application to view PDFs." +" Would you like to download one from Android Market?"); builder.setPositiveButton("Yes, Please", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent marketIntent = new Intent(Intent.ACTION_VIEW); marketIntent.setData(Uri.parse("market://details?id=com.adobe.reader")); startActivity(marketIntent); } }); builder.setNegativeButton("No, Thanks", null); builder.create().show(); } }

Launching System Applications Contact Picker SMS Intent pickIntent = new Intent(); pickIntent.setAction(Intent.ACTION_PICK); pickIntent.setData(URI_TO_CONTACT_TABLE); Intent smsIntent = new Intent(); smsIntent.setAction(Intent.ACTION_VIEW); smsIntent.setType(“vnd.android-dir/mms-sms”); smsIntent.putExtra(“address”, “8885551234”); smsIntent.putExtra(“sms_body”, “Body Text”); To display a web page Intent pageIntent = new Intent(); pageIntent.setAction(Intent.ACTION_VIEW); pageIntent.setData(Uri.parse(“http://WEB_ADDRESS_TO_VIEW”));