Skip to content

Commit 2f58a3a

Browse files
committed
Added button notification
1 parent fc9cb19 commit 2f58a3a

File tree

6 files changed

+266
-20
lines changed

6 files changed

+266
-20
lines changed

AndroidManifest.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@
2121
<category android:name="android.intent.category.LAUNCHER" />
2222
</intent-filter>
2323
</activity>
24+
25+
<activity
26+
android:name="doc.saulmm.notification.TestActivity">
27+
28+
<!-- Necessary to the task manager to know who is the parent activity, if you are developing API 16 or higher, you can use:
29+
android:parentActivityName="doc.saulmm.notification.MainActivity" -->
30+
<meta-data
31+
android:name="android.support.PARENT_ACTIVITY"
32+
android:value="doc.saulmm.notification.MainActivity" />
33+
</activity>
34+
35+
36+
2437
</application>
2538

2639
</manifest>

res/layout/activity_main.xml

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,38 @@
1010
tools:context=".MainActivity" >
1111

1212
<Button
13+
android:id="@+id/simple_notification"
1314
android:text="Simple notification"
15+
android:layout_width="match_parent"
16+
android:layout_height="wrap_content"
1417
style="@style/buttonStyle">
1518
</Button>
1619

1720
<Button
18-
android:text="Simple notification"
21+
android:id="@+id/big_notification"
22+
android:text="Expandable with buttons"
23+
android:layout_width="match_parent"
24+
android:layout_height="wrap_content"
1925
style="@style/buttonStyle">
2026
</Button>
2127

2228
<Button
23-
android:text="Simple notification"
24-
style="@style/buttonStyle">
25-
</Button>
26-
<Button
27-
android:text="Simple notification"
28-
style="@style/buttonStyle">
29-
</Button>
30-
<Button
31-
android:text="Simple notification"
29+
android:id="@+id/progress_notification"
30+
android:text="Progress notification"
31+
android:layout_width="match_parent"
32+
android:layout_height="wrap_content"
3233
style="@style/buttonStyle">
3334
</Button>
35+
3436
<Button
35-
android:text="Simple notification"
37+
android:id="@+id/button_notifcation"
38+
android:text="Button notification"
39+
android:layout_width="match_parent"
40+
android:layout_height="wrap_content"
3641
style="@style/buttonStyle">
3742
</Button>
3843

3944

4045

46+
4147
</LinearLayout>

res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
<string name="app_name">Notifications</string>
55
<string name="action_settings">Settings</string>
66
<string name="hello_world">Hello world!</string>
7+
<string name="long_lorem">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</string>
78

89
</resources>
Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,48 @@
11
package doc.saulmm.notification;
22

3-
import android.os.Bundle;
43
import android.app.Activity;
5-
import android.view.Menu;
4+
import android.os.Bundle;
5+
import android.view.View;
66

7-
public class MainActivity extends Activity {
7+
public class MainActivity extends Activity implements View.OnClickListener {
8+
NotificationHandler nHandler;
89

910
@Override
1011
protected void onCreate(Bundle savedInstanceState) {
1112
super.onCreate(savedInstanceState);
13+
nHandler = NotificationHandler.getInstance(this);
14+
initUI();
15+
}
16+
17+
18+
private void initUI () {
1219
setContentView(R.layout.activity_main);
20+
findViewById(R.id.simple_notification).setOnClickListener(this);
21+
findViewById(R.id.big_notification).setOnClickListener(this);
22+
findViewById(R.id.progress_notification).setOnClickListener(this);
23+
findViewById(R.id.button_notifcation).setOnClickListener(this);
1324
}
1425

26+
1527
@Override
16-
public boolean onCreateOptionsMenu(Menu menu) {
17-
// Inflate the menu; this adds items to the action bar if it is present.
18-
getMenuInflater().inflate(R.menu.main, menu);
19-
return true;
20-
}
28+
public void onClick (View v) {
29+
switch (v.getId()) {
30+
31+
case R.id.simple_notification:
32+
nHandler.createSimpleNotification(this);
33+
break;
34+
35+
case R.id.big_notification:
36+
nHandler.createExpandableNotification(this);
37+
break;
2138

39+
case R.id.progress_notification:
40+
nHandler.createProgressNotification(this);
41+
break;
42+
43+
case R.id.button_notifcation:
44+
nHandler.createButtonNotification(this);
45+
}
46+
47+
}
2248
}
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,190 @@
11
package doc.saulmm.notification;
22

3+
import android.app.NotificationManager;
4+
import android.app.PendingIntent;
5+
import android.content.Context;
6+
import android.content.Intent;
7+
import android.os.AsyncTask;
8+
import android.os.Build;
9+
import android.support.v4.app.NotificationCompat;
10+
import android.support.v4.app.TaskStackBuilder;
11+
import android.widget.Toast;
12+
13+
import java.util.Random;
14+
315
public class NotificationHandler {
16+
// Notification handler singleton
17+
private static NotificationHandler nHandler;
18+
private static NotificationManager mNotificationManager;
19+
20+
21+
private NotificationHandler () {}
22+
23+
24+
/**
25+
* Singleton pattern implementation
26+
* @return
27+
*/
28+
public static NotificationHandler getInstance(Context context) {
29+
if(nHandler == null) {
30+
nHandler = new NotificationHandler();
31+
mNotificationManager =
32+
(NotificationManager) context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
33+
}
34+
35+
return nHandler;
36+
}
37+
38+
39+
/**
40+
* Shows a simple notification
41+
* @param context aplication context
42+
*/
43+
public void createSimpleNotification(Context context) {
44+
// Creates an explicit intent for an Activity
45+
Intent resultIntent = new Intent(context, TestActivity.class);
46+
47+
// Creating a artifical activity stack for the notification activity
48+
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
49+
stackBuilder.addParentStack(TestActivity.class);
50+
stackBuilder.addNextIntent(resultIntent);
51+
52+
// Pending intent to the notification manager
53+
PendingIntent resultPending = stackBuilder
54+
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
55+
56+
// Building the notification
57+
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
58+
.setSmallIcon(R.drawable.ic_launcher) // notification icon
59+
.setContentTitle("I'm a simple notification") // main title of the notification
60+
.setContentText("I'm the text of the simple notification") // notification text
61+
.setContentIntent(resultPending); // notification intent
62+
63+
// mId allows you to update the notification later on.
64+
mNotificationManager.notify(10, mBuilder.build());
65+
}
66+
67+
68+
public void createExpandableNotification (Context context) {
69+
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
70+
// Building the expandable content
71+
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
72+
String lorem = context.getResources().getString(R.string.long_lorem);
73+
String [] content = lorem.split("\\.");
74+
75+
inboxStyle.setBigContentTitle("This is a big title");
76+
for (String line : content) {
77+
inboxStyle.addLine(line);
78+
}
79+
80+
// Building the notification
81+
NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(context)
82+
.setSmallIcon(R.drawable.ic_launcher) // notification icon
83+
.setContentTitle("Expandable notification") // title of notification
84+
.setContentText("This is an example of an expandable notification") // text inside the notification
85+
.setStyle(inboxStyle); // adds the expandable content to the notification
86+
87+
mNotificationManager.notify(11, nBuilder.build());
88+
89+
} else {
90+
Toast.makeText(context, "Can't show", Toast.LENGTH_LONG).show();
91+
}
92+
}
93+
94+
95+
/**
96+
* Show a determinate and undeterminate progress notification
97+
* @param context, activity context
98+
*/
99+
public void createProgressNotification (final Context context) {
100+
101+
// used to update the progress notification
102+
final int progresID = new Random().nextInt(1000);
103+
104+
// building the notification
105+
final NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(context)
106+
.setSmallIcon(R.drawable.refresh)
107+
.setContentTitle("Progres notification")
108+
.setContentText("Now waiting")
109+
.setTicker("Progress notification created")
110+
.setUsesChronometer(true)
111+
.setProgress(100, 0, true);
112+
113+
114+
115+
AsyncTask<Integer, Integer, Integer> downloadTask = new AsyncTask<Integer, Integer, Integer>() {
116+
@Override
117+
protected void onPreExecute () {
118+
super.onPreExecute();
119+
mNotificationManager.notify(progresID, nBuilder.build());
120+
}
121+
122+
@Override
123+
protected Integer doInBackground (Integer... params) {
124+
try {
125+
// Sleeps 2 seconds to show the undeterminated progress
126+
Thread.sleep(5000);
127+
128+
// update the progress
129+
for (int i = 0; i < 101; i+=5) {
130+
nBuilder
131+
.setContentTitle("Progress running...")
132+
.setContentText("Now running...")
133+
.setProgress(100, i, false)
134+
.setSmallIcon(R.drawable.download)
135+
.setContentInfo(i + " %");
136+
137+
// use the same id for update instead created another one
138+
mNotificationManager.notify(progresID, nBuilder.build());
139+
Thread.sleep(500);
140+
}
141+
142+
} catch (InterruptedException e) {
143+
e.printStackTrace();
144+
}
145+
146+
return null;
147+
}
148+
149+
150+
@Override
151+
protected void onPostExecute (Integer integer) {
152+
super.onPostExecute(integer);
153+
154+
nBuilder.setContentText("Progress finished :D")
155+
.setContentTitle("Progress finished !!")
156+
.setTicker("Progress finished !!!")
157+
.setSmallIcon(R.drawable.accept)
158+
.setUsesChronometer(false);
159+
160+
mNotificationManager.notify(progresID, nBuilder.build());
161+
}
162+
};
163+
164+
// Executes the progress task
165+
downloadTask.execute();
166+
}
167+
168+
169+
public void createButtonNotification (Context context) {
170+
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
171+
// Prepare intent which is triggered if the notification button is pressed
172+
Intent intent = new Intent(context, TestActivity.class);
173+
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
174+
175+
// Building the notifcation
176+
NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(context)
177+
.setSmallIcon(R.drawable.ic_launcher) // notification icon
178+
.setContentTitle("Button notification") // notification title
179+
.setContentText("Expand to show the buttons...") // content text
180+
.setTicker("Showing button notification") // status bar message
181+
.addAction(R.drawable.accept, "Accept", pIntent) // accept notification button
182+
.addAction(R.drawable.cancel, "Cancel", pIntent); // cancel notification button
183+
184+
mNotificationManager.notify(1001, nBuilder.build());
185+
186+
} else {
187+
Toast.makeText(context, "You need a higher version", Toast.LENGTH_LONG).show();
188+
}
189+
}
4190
}
Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
11
package doc.saulmm.notification;
22

3-
public class TestActivity {
3+
import android.app.Activity;
4+
import android.os.Bundle;
5+
import android.widget.TextView;
6+
7+
public class TestActivity extends Activity {
8+
9+
@Override
10+
protected void onCreate (Bundle savedInstanceState) {
11+
super.onCreate(savedInstanceState);
12+
13+
TextView text = new TextView(this);
14+
text.setText("Test activity");
15+
setContentView(text);
16+
17+
}
418
}

0 commit comments

Comments
 (0)