Presentation is loading. Please wait.

Presentation is loading. Please wait.

Notifying from the Background

Similar presentations


Presentation on theme: "Notifying from the Background"— Presentation transcript:

1

2 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

3 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

4 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(); } }

5 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; }

6 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..."); }

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

8 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”, “ ”); smsIntent.putExtra(“sms_body”, “Body Text”); To display a web page Intent pageIntent = new Intent(); pageIntent.setAction(Intent.ACTION_VIEW); pageIntent.setData(Uri.parse(“


Download ppt "Notifying from the Background"

Similar presentations


Ads by Google