Professional Documents
Culture Documents
About
Subscribe:
Posts
Comments
Advertise Here
X
Hello there! If you are new here, you might want to subscribe to the RSS feed for updates
on this topic.
Algorithm:
1.) Create a new project by File-> New -> Android Project name it JellyBeanNotifications.
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
2
android:layout_width="match_parent"
3 android:layout_height="match_parent"
4 android:orientation="vertical"
5 android:gravity="center_horizontal">
6
7 <Button
android:id="@+id/btBasicNotification"
8 android:layout_width="fill_parent"
9 android:layout_height="wrap_content"
10 android:gravity="center_horizontal|center_vertical"
11 android:onClick="sendBasicNotification"
12 android:text="@string/btBasicNotification"
android:background="@drawable/button_background"
13 android:textColor="#000000"
14 />
15 <Button
16 android:id="@+id/btBigTextNotification"
android:layout_width="fill_parent"
17 android:layout_height="wrap_content"
18 android:gravity="center_horizontal|center_vertical"
19 android:onClick="sendBigTextStyleNotification"
20 android:text="@string/btBigTextNotification"
21 android:background="@drawable/button_background"
android:textColor="#000000"
22 />
23 <Button
24 android:id="@+id/btBigPictureNotification"
25 android:layout_width="fill_parent"
26 android:layout_height="wrap_content"
android:gravity="center_horizontal|center_vertical"
27 android:onClick="sendBigPictureStyleNotification"
28 android:text="@string/btBigPictureNotification"
29 android:background="@drawable/button_background"
30
31
32
33
34 android:textColor="#000000" />
<Button
35 android:id="@+id/btInboxStyleNotification"
36 android:layout_width="fill_parent"
37 android:layout_height="wrap_content"
38 android:gravity="center_horizontal|center_vertical"
39 android:onClick="sendInboxStyleNotification"
android:text="@string/btInboxStyleNotification"
40 android:background="@drawable/button_background"
41 android:textColor="#000000"/>
42</LinearLayout>
43
44
45
46
1 <resources>
2
3 <dimen name="padding_small">8dp</dimen>
4 <dimen name="padding_medium">8dp</dimen>
5 <dimen name="padding_large">16dp</dimen>
6
</resources>
7
1
2 <?xml version="1.0" encoding="utf-8"?>
3 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
4 android:layout_width="match_parent"
android:layout_height="match_parent"
5 android:orientation="vertical"
6 android:gravity="center_horizontal|center_vertical" >
7
8 <TextView
9 android:id="@+id/textView1"
android:layout_width="wrap_content"
10 android:layout_height="wrap_content"
11 android:text="@string/tvHandleNotification"
12 android:textSize="20dp"
13 android:textStyle="bold|italic" />
14
15</LinearLayout>
16
1 package com.example.jellybeannotifications;
2
import android.app.Activity;
3 import android.os.Bundle;
4
5 public class HandleNotificationActivity extends Activity {
6
7 @Override
8 protected void onCreate(Bundle savedInstanceState) {
9
10 super.onCreate(savedInstanceState);
setContentView(R.layout.handle_notification_activity);
11 }
12 }
13
Steps:
1.) Create a project named JellyBeanNotifications and set the information as stated in the
image.
1 package com.example.jellybeannotifications;
2
import android.app.Activity;
3
import android.app.Notification;
4 import android.app.Notification.Builder;
5 import android.app.NotificationManager;
6 import android.app.PendingIntent;
import android.content.Intent;
7
import android.graphics.BitmapFactory;
8 import android.os.Bundle;
9 import android.view.Menu;
10 import android.view.View;
11
12 public class JellyBeanNotificationsActivity extends Activity {
13
@Override
14 public void onCreate(Bundle savedInstanceState) {
15 super.onCreate(savedInstanceState);
16 setContentView(R.layout.main);
17 }
18
19 @Override
public boolean onCreateOptionsMenu(Menu menu) {
20 getMenuInflater().inflate(R.menu.main, menu);
21 return true;
22 }
23
24 public void sendBasicNotification(View view) {
Notification notification = new Notification.Builder(this)
25 .setContentTitle("Basic Notification")
26 .setContentText("Basic Notification, used earlier")
27 .setSmallIcon(R.drawable.ic_launcher_share).build();
28 notification.flags |= Notification.FLAG_AUTO_CANCEL;
29 NotificationManager notificationManager =
getNotificationManager();
30 notificationManager.notify(0, notification);
31 }
32
33 public void sendBigTextStyleNotification(View view) {
34 String msgText = "Jeally Bean Notification example!! "
35 + "where you will see three different kind of
notification. "
36 + "you can even put the very long string here.";
37
38 NotificationManager notificationManager =
39 getNotificationManager();
40 PendingIntent pi = getPendingIntent();
Builder builder = new Notification.Builder(this);
41 builder.setContentTitle("Big text Notofication")
42 .setContentText("Big text Notification")
43 .setSmallIcon(R.drawable.ic_launcher)
44 .addAction(R.drawable.ic_launcher_web, "show activity",
45 pi)
.setAutoCancel(true);
46 Notification notification = new
47 Notification.BigTextStyle(builder)
48 .bigText(msgText)
49 .build();
notificationManager.notify(0, notification);
50
}
51
52 public void sendBigPictureStyleNotification(View view) {
53 PendingIntent pi = getPendingIntent();
54 Builder builder = new Notification.Builder(this);
55 builder.setContentTitle("BP notification")
56 // Notification title
.setContentText("BigPicutre notification")
57
// you can put subject line.
58 .setSmallIcon(R.drawable.ic_launcher)
59 // Set your notification icon here.
60 .addAction(R.drawable.ic_launcher_web, "show activity",
61 pi)
.addAction(
62 R.drawable.ic_launcher_share,
63 "Share",
64 PendingIntent.getActivity(getApplicationContext(
65 ), 0,
66 getIntent(), 0, null));
67
// Now create the Big picture notification.
68 Notification notification = new
69 Notification.BigPictureStyle(builder)
70 .bigPicture(
71 BitmapFactory.decodeResource(getResources(),
R.drawable.big_picture)).build();
72 // Put the auto cancel notification flag
73 notification.flags |= Notification.FLAG_AUTO_CANCEL;
74 NotificationManager notificationManager =
75 getNotificationManager();
76 notificationManager.notify(0, notification);
}
77
78 public void sendInboxStyleNotification(View view) {
79 PendingIntent pi = getPendingIntent();
80 Builder builder = new Notification.Builder(this)
81 .setContentTitle("IS Notification")
82 .setContentText("Inbox Style notification!!")
.setSmallIcon(R.drawable.ic_launcher)
83 .addAction(R.drawable.ic_launcher_web, "show activity",
84 pi);
85
86 Notification notification = new Notification.InboxStyle(builder)
87 .addLine("First message").addLine("Second message")
.addLine("Thrid message").addLine("Fourth Message")
88 .setSummaryText("+2 more").build();
89 // Put the auto cancel notification flag
90 notification.flags |= Notification.FLAG_AUTO_CANCEL;
91 NotificationManager notificationManager =
92 getNotificationManager();
notificationManager.notify(0, notification);
93 }
94
95 public PendingIntent getPendingIntent() {
96 return PendingIntent.getActivity(this, 0, new Intent(this,
97 HandleNotificationActivity.class), 0);
}
98
99
public NotificationManager getNotificationManager() {
10 return (NotificationManager)
0 getSystemService(NOTIFICATION_SERVICE);
10 }
1 }
10
2
10
3
10
4
10
5
10
6
10
7
10
8
10
9
110
Output
Tags: Jelly Bean Android, Jelly Bean Notifications Android, Jelly Bean Notifications
example Android, Jelly Bean Notifications in Android, Jelly Bean Notifications tutorial
Android
Previous
Next
Leave a Reply
Name (Required)
Buzz
RSS
About Author
Sign Up For Your Free Android Course Material for 3 Weeks including Free Videos and Source
Code.
Name:
Email:
2013 Android Tutorial | Android SDK Development & Programming. All rights reserved.
Proudly designed by Theme Junkie.