Android Open Source - watchme Notify Service






From Project

Back to project page watchme.

License

The source code is released under:

Copyright (c) 2012 Johan Brook, Robin Andersson, Lisa Stenberg, Mattias Henriksson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documen...

If you think the Android project watchme listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
*  NotifyService.java/*from ww w  . j  a  v a 2 s. c o m*/
*
*  The service responsible for showing notifications.
*
*  @author Johan
*  @copyright (c) 2012 Johan Brook, Robin Andersson, Lisa Stenberg, Mattias Henriksson
*  @license MIT
*/

package se.chalmers.watchme.notifications;

import java.io.Serializable;

import se.chalmers.watchme.activity.MovieDetailsActivity;
import se.chalmers.watchme.model.Movie;
import android.R;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.Binder;
import android.util.Log;

public class NotifyService extends Service {
  
  public class ServiceBinder extends Binder {
    NotifyService getService() {
      return NotifyService.this;
    }
  }
  
  /** Intent key extra */
  public static final String INTENT_NOTIFY = "se.chalmers.watchme.notifications.INTENT_NOTIFY";
  
  /** Intent key extra for a Movie */
  public static final String INTENT_MOVIE = "se.chalmers.watchme.notifications.INTENT_MOVIE";
  
  private NotificationManager manager;
  private IBinder binder = new ServiceBinder();
  

  @Override
  public void onCreate() {
    // Fetch the system's notification service manager
    this.manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  }
  
  @Override
  public void onDestroy() {
    Log.i("Custom", "** Destroy notifications");
    this.manager.cancelAll();
  }
  
  @Override
  public int onStartCommand(Intent intent, int flags, int startID) {
    Log.i("Custom", "Received start id " + startID + ": " + intent.getBooleanExtra(INTENT_NOTIFY, false));
    
    // If this service was started by AlarmTask, show a notification
    if(intent.getBooleanExtra(INTENT_NOTIFY, false)) {
      showNotification( (Movie) intent.getSerializableExtra(INTENT_MOVIE));
    }
    
    // If the service is killed, it's no big deal as we've delivered our notification
    return START_NOT_STICKY;
  }
  
  
  @Override
  public IBinder onBind(Intent intent) {
    return this.binder;
  }

  
  private void showNotification(Notifiable obj) {
    
    int id = obj.getNotificationId();

    // The content of the notification box
    CharSequence title = getString(se.chalmers.watchme.R.string.notification_title);
    CharSequence text = "'" + obj.getTitle() + "' "+getString(se.chalmers.watchme.R.string.notification_suffix);
    int icon = R.drawable.ic_popup_reminder;
    
    // The intent to launch an activity if the user presses this notification
    Intent detailsIntent = new Intent(this, MovieDetailsActivity.class);
    detailsIntent.putExtra(MovieDetailsActivity.MOVIE_EXTRA, (Serializable) obj);
    
    PendingIntent pending = PendingIntent.getActivity(this, 0, detailsIntent, 0);

    // Build the notification

    Notification notification = new Notification.Builder(this)
      .setSmallIcon(icon)
      .setContentIntent(pending)
      .setContentTitle(title)
      .setContentText(text)
      .setAutoCancel(true)
      .setWhen(System.currentTimeMillis())
      .build();
    
    // Send the notification to the system along with our id
    this.manager.notify(id, notification);
    
    Log.i("Custom", "Sent notification for movie "+ obj +", with id: "+id);
  }

}




Java Source Code List

se.chalmers.watchme.activity.AddMovieActivity.java
se.chalmers.watchme.activity.AutoCompleteAdapter.java
se.chalmers.watchme.activity.MainActivity.java
se.chalmers.watchme.activity.MovieDetailsActivity.java
se.chalmers.watchme.activity.SearchableActivity.java
se.chalmers.watchme.activity.TabsAdapter.java
se.chalmers.watchme.activity.TagMovieListActivity.java
se.chalmers.watchme.database.DatabaseAdapter.java
se.chalmers.watchme.database.DatabaseHelper.java
se.chalmers.watchme.database.GenericCursorLoader.java
se.chalmers.watchme.database.HasTagTable.java
se.chalmers.watchme.database.ICursorHelper.java
se.chalmers.watchme.database.MovieAlreadyExistsException.java
se.chalmers.watchme.database.MoviesTable.java
se.chalmers.watchme.database.TagsTable.java
se.chalmers.watchme.database.WatchMeContentProvider.java
se.chalmers.watchme.model.Movie.java
se.chalmers.watchme.model.Tag.java
se.chalmers.watchme.net.HttpRetriever.java
se.chalmers.watchme.net.IMDBHandler.java
se.chalmers.watchme.net.ImageDownloadTask.java
se.chalmers.watchme.net.MovieSource.java
se.chalmers.watchme.net.NoEntityException.java
se.chalmers.watchme.notifications.Notifiable.java
se.chalmers.watchme.notifications.NotificationClient.java
se.chalmers.watchme.notifications.NotificationService.java
se.chalmers.watchme.notifications.NotifyService.java
se.chalmers.watchme.ui.ContentListFragment.java
se.chalmers.watchme.ui.DatePickerFragment.java
se.chalmers.watchme.ui.ImageDialog.java
se.chalmers.watchme.ui.MovieListFragment.java
se.chalmers.watchme.ui.TagListFragment.java
se.chalmers.watchme.utils.DateTimeUtils.java
se.chalmers.watchme.utils.ImageCache.java
se.chalmers.watchme.utils.MenuUtils.java
se.chalmers.watchme.utils.MovieHelper.java
se.chalmers.watchmetest.Constants.java
se.chalmers.watchmetest.activity.MainActivityTest.java
se.chalmers.watchmetest.activity.MovieDetailsActivityTest.java
se.chalmers.watchmetest.activity.SearchableActivityTest.java
se.chalmers.watchmetest.activity.TabsAdapterTest.java
se.chalmers.watchmetest.activity.TagMovieListActivityTest.java
se.chalmers.watchmetest.database.WatchMeContentProviderTest.java
se.chalmers.watchmetest.model.MovieTest.java
se.chalmers.watchmetest.model.TagTest.java
se.chalmers.watchmetest.net.HttpRetrieverTest.java
se.chalmers.watchmetest.net.IMDBHandlerTest.java
se.chalmers.watchmetest.ui.MovieListFragmentTest.java
se.chalmers.watchmetest.ui.TagListFragmentTest.java
se.chalmers.watchmetest.util.DateTimeUtilsTest.java
se.chalmers.watchmetest.util.MovieHelperTest.java