diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 5d41179..6e29407 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -21,6 +21,19 @@
+
+
+
+
+
+
+
+
+
diff --git a/res/layout/activity_main.xml b/res/layout/activity_main.xml
index 758c7ed..973f034 100644
--- a/res/layout/activity_main.xml
+++ b/res/layout/activity_main.xml
@@ -10,32 +10,38 @@
tools:context=".MainActivity" >
-
-
+
+
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 1205b7b..55d9ffc 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -4,5 +4,6 @@
Notifications
Settings
Hello world!
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean eu aliquet sem. Mauris tincidunt posuere erat vel dignissim. Donec condimentum, ligula id lobortis congue, leo nunc suscipit dui, non molestie ligula risus et velit. Pellentesque consectetur, ante molestie dapibus tincidunt, ipsum ipsum pretium turpis, eget elementum nulla arcu sed leo
diff --git a/src/doc/saulmm/notification/MainActivity.java b/src/doc/saulmm/notification/MainActivity.java
index fd06e62..74be039 100644
--- a/src/doc/saulmm/notification/MainActivity.java
+++ b/src/doc/saulmm/notification/MainActivity.java
@@ -1,22 +1,48 @@
package doc.saulmm.notification;
-import android.os.Bundle;
import android.app.Activity;
-import android.view.Menu;
+import android.os.Bundle;
+import android.view.View;
-public class MainActivity extends Activity {
+public class MainActivity extends Activity implements View.OnClickListener {
+ NotificationHandler nHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
+ nHandler = NotificationHandler.getInstance(this);
+ initUI();
+ }
+
+
+ private void initUI () {
setContentView(R.layout.activity_main);
+ findViewById(R.id.simple_notification).setOnClickListener(this);
+ findViewById(R.id.big_notification).setOnClickListener(this);
+ findViewById(R.id.progress_notification).setOnClickListener(this);
+ findViewById(R.id.button_notifcation).setOnClickListener(this);
}
+
@Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
+ public void onClick (View v) {
+ switch (v.getId()) {
+
+ case R.id.simple_notification:
+ nHandler.createSimpleNotification(this);
+ break;
+
+ case R.id.big_notification:
+ nHandler.createExpandableNotification(this);
+ break;
+ case R.id.progress_notification:
+ nHandler.createProgressNotification(this);
+ break;
+
+ case R.id.button_notifcation:
+ nHandler.createButtonNotification(this);
+ }
+
+ }
}
diff --git a/src/doc/saulmm/notification/NotificationHandler.java b/src/doc/saulmm/notification/NotificationHandler.java
index b396f78..4e66359 100644
--- a/src/doc/saulmm/notification/NotificationHandler.java
+++ b/src/doc/saulmm/notification/NotificationHandler.java
@@ -1,4 +1,190 @@
package doc.saulmm.notification;
+import android.app.NotificationManager;
+import android.app.PendingIntent;
+import android.content.Context;
+import android.content.Intent;
+import android.os.AsyncTask;
+import android.os.Build;
+import android.support.v4.app.NotificationCompat;
+import android.support.v4.app.TaskStackBuilder;
+import android.widget.Toast;
+
+import java.util.Random;
+
public class NotificationHandler {
+ // Notification handler singleton
+ private static NotificationHandler nHandler;
+ private static NotificationManager mNotificationManager;
+
+
+ private NotificationHandler () {}
+
+
+ /**
+ * Singleton pattern implementation
+ * @return
+ */
+ public static NotificationHandler getInstance(Context context) {
+ if(nHandler == null) {
+ nHandler = new NotificationHandler();
+ mNotificationManager =
+ (NotificationManager) context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
+ }
+
+ return nHandler;
+ }
+
+
+ /**
+ * Shows a simple notification
+ * @param context aplication context
+ */
+ public void createSimpleNotification(Context context) {
+ // Creates an explicit intent for an Activity
+ Intent resultIntent = new Intent(context, TestActivity.class);
+
+ // Creating a artifical activity stack for the notification activity
+ TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
+ stackBuilder.addParentStack(TestActivity.class);
+ stackBuilder.addNextIntent(resultIntent);
+
+ // Pending intent to the notification manager
+ PendingIntent resultPending = stackBuilder
+ .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
+
+ // Building the notification
+ NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
+ .setSmallIcon(R.drawable.ic_launcher) // notification icon
+ .setContentTitle("I'm a simple notification") // main title of the notification
+ .setContentText("I'm the text of the simple notification") // notification text
+ .setContentIntent(resultPending); // notification intent
+
+ // mId allows you to update the notification later on.
+ mNotificationManager.notify(10, mBuilder.build());
+ }
+
+
+ public void createExpandableNotification (Context context) {
+ if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
+ // Building the expandable content
+ NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
+ String lorem = context.getResources().getString(R.string.long_lorem);
+ String [] content = lorem.split("\\.");
+
+ inboxStyle.setBigContentTitle("This is a big title");
+ for (String line : content) {
+ inboxStyle.addLine(line);
+ }
+
+ // Building the notification
+ NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(context)
+ .setSmallIcon(R.drawable.ic_launcher) // notification icon
+ .setContentTitle("Expandable notification") // title of notification
+ .setContentText("This is an example of an expandable notification") // text inside the notification
+ .setStyle(inboxStyle); // adds the expandable content to the notification
+
+ mNotificationManager.notify(11, nBuilder.build());
+
+ } else {
+ Toast.makeText(context, "Can't show", Toast.LENGTH_LONG).show();
+ }
+ }
+
+
+ /**
+ * Show a determinate and undeterminate progress notification
+ * @param context, activity context
+ */
+ public void createProgressNotification (final Context context) {
+
+ // used to update the progress notification
+ final int progresID = new Random().nextInt(1000);
+
+ // building the notification
+ final NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(context)
+ .setSmallIcon(R.drawable.refresh)
+ .setContentTitle("Progres notification")
+ .setContentText("Now waiting")
+ .setTicker("Progress notification created")
+ .setUsesChronometer(true)
+ .setProgress(100, 0, true);
+
+
+
+ AsyncTask downloadTask = new AsyncTask() {
+ @Override
+ protected void onPreExecute () {
+ super.onPreExecute();
+ mNotificationManager.notify(progresID, nBuilder.build());
+ }
+
+ @Override
+ protected Integer doInBackground (Integer... params) {
+ try {
+ // Sleeps 2 seconds to show the undeterminated progress
+ Thread.sleep(5000);
+
+ // update the progress
+ for (int i = 0; i < 101; i+=5) {
+ nBuilder
+ .setContentTitle("Progress running...")
+ .setContentText("Now running...")
+ .setProgress(100, i, false)
+ .setSmallIcon(R.drawable.download)
+ .setContentInfo(i + " %");
+
+ // use the same id for update instead created another one
+ mNotificationManager.notify(progresID, nBuilder.build());
+ Thread.sleep(500);
+ }
+
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+
+ return null;
+ }
+
+
+ @Override
+ protected void onPostExecute (Integer integer) {
+ super.onPostExecute(integer);
+
+ nBuilder.setContentText("Progress finished :D")
+ .setContentTitle("Progress finished !!")
+ .setTicker("Progress finished !!!")
+ .setSmallIcon(R.drawable.accept)
+ .setUsesChronometer(false);
+
+ mNotificationManager.notify(progresID, nBuilder.build());
+ }
+ };
+
+ // Executes the progress task
+ downloadTask.execute();
+ }
+
+
+ public void createButtonNotification (Context context) {
+ if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
+ // Prepare intent which is triggered if the notification button is pressed
+ Intent intent = new Intent(context, TestActivity.class);
+ PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
+
+ // Building the notifcation
+ NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(context)
+ .setSmallIcon(R.drawable.ic_launcher) // notification icon
+ .setContentTitle("Button notification") // notification title
+ .setContentText("Expand to show the buttons...") // content text
+ .setTicker("Showing button notification") // status bar message
+ .addAction(R.drawable.accept, "Accept", pIntent) // accept notification button
+ .addAction(R.drawable.cancel, "Cancel", pIntent); // cancel notification button
+
+ mNotificationManager.notify(1001, nBuilder.build());
+
+ } else {
+ Toast.makeText(context, "You need a higher version", Toast.LENGTH_LONG).show();
+ }
+ }
}
diff --git a/src/doc/saulmm/notification/TestActivity.java b/src/doc/saulmm/notification/TestActivity.java
index 4206c2a..f20b95f 100644
--- a/src/doc/saulmm/notification/TestActivity.java
+++ b/src/doc/saulmm/notification/TestActivity.java
@@ -1,4 +1,18 @@
package doc.saulmm.notification;
-public class TestActivity {
+import android.app.Activity;
+import android.os.Bundle;
+import android.widget.TextView;
+
+public class TestActivity extends Activity {
+
+ @Override
+ protected void onCreate (Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ TextView text = new TextView(this);
+ text.setText("Test activity");
+ setContentView(text);
+
+ }
}