Multiple Notifications are a unique feature in ANDROID phones. This tutorial will show you how to create notification from your application.
ANDROID uses the NotificationManager class to create the notification.
Let’s look at the code.
Explanation.
mManager.notify(APP_ID, notification);
This line in the code sends the notification where notification is the object of the Notification class where you can set the data and title for the notification.
This line will cancel the notification created by this application.
mManager.cancel(APP_ID);
The layout file for the above code “main.xml”
ANDROID uses the NotificationManager class to create the notification.
Let’s look at the code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
| package com.Webviews; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class NotificationActivity extends Activity implements View.OnClickListener { private Button button, clear; private NotificationManager mManager; private static final int APP_ID = 0 ; @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); button = (Button) this .findViewById( R.id.my_button); clear = (Button) this .findViewById( R.id.cancel); clear.setOnClickListener( new OnClickListener() { @Override public void onClick(View arg0) { mManager.cancel(APP_ID); } }); button.setOnClickListener( this ); mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); } @Override public void onClick(View v) { Intent intent = new Intent( this ,NotificationActivity. class ); Notification notification = new Notification(R.drawable.icon, "Notification!!" , System.currentTimeMillis()); notification.setLatestEventInfo(NotificationActivity. this , "Send by which application" , "Description of the notification" , PendingIntent.getActivity( this .getBaseContext(), 0 , intent, PendingIntent.FLAG_NO_CREATE)); mManager.notify(APP_ID, notification); } } |
mManager.notify(APP_ID, notification);
This line in the code sends the notification where notification is the object of the Notification class where you can set the data and title for the notification.
This line will cancel the notification created by this application.
mManager.cancel(APP_ID);
The layout file for the above code “main.xml”
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| <? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:orientation = "vertical" android:layout_width = "fill_parent" android:layout_height = "fill_parent" > < TextView android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text = "@string/hello" /> < Button android:text = "Send Notification" android:id = "@+id/my_button" android:layout_width = "wrap_content" android:layout_height = "wrap_content" > </ Button > < Button android:text = "Cancel Notification" android:id = "@+id/cancel" android:layout_width = "wrap_content" android:layout_height = "wrap_content" > </ Button > </ LinearLayout > |
No comments:
Post a Comment