-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Expand file tree
/
Copy pathNotificationStatusBarWithCustomViewActivity.java
More file actions
85 lines (65 loc) · 2.66 KB
/
Copy pathNotificationStatusBarWithCustomViewActivity.java
File metadata and controls
85 lines (65 loc) · 2.66 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package course.examples.notification.statusbarwithcustomview;
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.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RemoteViews;
public class NotificationStatusBarWithCustomViewActivity extends Activity {
// Notification ID to allow for future updates
private static final int MY_NOTIFICATION_ID = 1;
// Notification Count
private int mNotificationCount;
// Notification Text Elements
private final CharSequence tickerText = "This is a Really, Really, Super Long Notification Message!";
private final CharSequence contentText = "You've Been Notified!";
// Notification Action Elements
private Intent mNotificationIntent;
private PendingIntent mContentIntent;
// Notification Sound and Vibration on Arrival
private Uri soundURI = Uri
.parse("android.resource://course.examples.Notification.StatusBarWithCustomView/"
+ R.raw.alarm_rooster);
private long[] mVibratePattern = { 0, 200, 200, 300 };
RemoteViews mContentView = new RemoteViews(
"course.examples.notification.statusbarwithcustomview",
R.layout.custom_notification);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mNotificationIntent = new Intent(getApplicationContext(),
NotificationSubActivity.class);
mContentIntent = PendingIntent.getActivity(getApplicationContext(), 0,
mNotificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Define the Notification's expanded message and Intent:
mContentView.setTextViewText(R.id.text, contentText + " ("
+ ++mNotificationCount + ")");
// Build the Notification
Notification.Builder notificationBuilder = new Notification.Builder(
getApplicationContext())
.setTicker(tickerText)
.setSmallIcon(android.R.drawable.stat_sys_warning)
.setAutoCancel(true)
.setContentIntent(mContentIntent)
.setSound(soundURI)
.setVibrate(mVibratePattern)
.setContent(mContentView);
// Pass the Notification to the NotificationManager:
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(MY_NOTIFICATION_ID,
notificationBuilder.build());
}
});
}
}