編輯:關於Android編程
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); builder = new Notification.Builder(this); builder.setAutoCancel(true); builder.setSmallIcon(R.drawable.alert); builder.setContentTitle("標題"); builder.setContentText("文本"); builder.setDefaults(Notification.DEFAULT_SOUND| Notification.DEFAULT_VIBRATE);
Intent intent = new Intent(this, SecondActivity.class); PendingIntent pIntent = PendingIntent.getActivity(this, 1, intent,PendingIntent.FLAG_ONE_SHOT); builder.setContentIntent(pIntent); manager.notify(0, builder.build());
/** * 在狀態欄顯示通知 */ private void showNotification(){ // 創建一個NotificationManager的引用 NotificationManager notificationManager = (NotificationManager) this.getSystemService(android.content.Context.NOTIFICATION_SERVICE); // 定義Notification的各種屬性 Notification notification =new Notification(R.drawable.icon, "督導系統", System.currentTimeMillis()); //FLAG_AUTO_CANCEL 該通知能被狀態欄的清除按鈕給清除掉 //FLAG_NO_CLEAR 該通知不能被狀態欄的清除按鈕給清除掉 //FLAG_ONGOING_EVENT 通知放置在正在運行 //FLAG_INSISTENT 是否一直進行,比如音樂一直播放,知道用戶響應 notification.flags |= Notification.FLAG_ONGOING_EVENT; // 將此通知放到通知欄的"Ongoing"即"正在運行"組中 notification.flags |= Notification.FLAG_NO_CLEAR; // 表明在點擊了通知欄中的"清除通知"後,此通知不清除,經常與FLAG_ONGOING_EVENT一起使用 notification.flags |= Notification.FLAG_SHOW_LIGHTS; //DEFAULT_ALL 使用所有默認值,比如聲音,震動,閃屏等等 //DEFAULT_LIGHTS 使用默認閃光提示 //DEFAULT_SOUNDS 使用默認提示聲音 //DEFAULT_VIBRATE 使用默認手機震動,需加上權限 notification.defaults = Notification.DEFAULT_LIGHTS; //疊加效果常量 //notification.defaults=Notification.DEFAULT_LIGHTS|Notification.DEFAULT_SOUND; notification.ledARGB = Color.BLUE; notification.ledOnMS =5000; //閃光時間,毫秒 // 設置通知的事件消息 CharSequence contentTitle ="督導系統標題"; // 通知欄標題 CharSequence contentText ="督導系統內容"; // 通知欄內容 Intent notificationIntent =new Intent(MainActivity.this, MainActivity.class); // 點擊該通知後要跳轉的Activity PendingIntent contentItent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(this, contentTitle, contentText, contentItent); // 把Notification傳遞給NotificationManager notificationManager.notify(0, notification); } ? //刪除通知 private void clearNotification(){ // 啟動後刪除之前我們定義的通知 NotificationManager notificationManager = (NotificationManager) this .getSystemService(NOTIFICATION_SERVICE); notificationManager.cancel(0);}}
Android Notification通知詳解 根據activity的生命周期,在activity不顯示時,會執行onStop函數(比如按下home鍵),所以你在onStop函數(按退出鍵除外)裡面把notification放在通知欄裡,再此顯示時,把notification從通知欄裡去掉。或者,只要程序在運行就一直顯示通知欄圖標。 下面對Notification類中的一些常量,字段,方法簡單介紹一下: 常量: DEFAULT_ALL 使用所有默認值,比如聲音,震動,閃屏等等 DEFAULT_LIGHTS 使用默認閃光提示 DEFAULT_SOUNDS 使用默認提示聲音 DEFAULT_VIBRATE 使用默認手機震動 【說明】:加入手機震動,一定要在manifest.xml中加入權限:以上的效果常量可以疊加,即通過 notification.defaults =DEFAULT_SOUND|DEFAULT_VIBRATE; notification.defaults |= DEFAULT_SOUND (最好在真機上測試,震動效果模擬器上沒有) //設置flag位 FLAG_AUTO_CANCEL 該通知能被狀態欄的清除按鈕給清除掉 FLAG_NO_CLEAR 該通知能被狀態欄的清除按鈕給清除掉 FLAG_ONGOING_EVENT 通知放置在正在運行 FLAG_INSISTENT 是否一直進行,比如音樂一直播放,知道用戶響應 常用字段: contentIntent 設置PendingIntent對象,點擊時發送該Intent defaults 添加默認效果 flags 設置flag位,例如FLAG_NO_CLEAR等 icon 設置圖標 sound 設置聲音 tickerText 顯示在狀態欄中的文字 when 發送此通知的時間戳 NotificationManager常用方法介紹: public void cancelAll() 移除所有通知(只是針對當前Context下的Notification) public void cancel(int id) 移除標記為id的通知 (只是針對當前Context下的所有Notification) public void notify(String tag ,int id, Notification notification) 將通知加入狀態欄,標簽為tag,標記為id public void notify(int id, Notification notification) 將通知加入狀態欄,標記為id
NotificationManager
NotificationCompat
A notification is a message you can display to the user outside of your application's normal UI. When you tell the system to issue a notification, it first appears as an icon in the notification area. To see the details of the notification, the user opens the notification drawer. Both the notification area and the notification drawer are system-controlled areas that the user can view at any time.
Figure 1. Notifications in the notification area.
Figure 2. Notifications in the notification drawer.<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+Tm90aWZpY2F0aW9uIERlc2lnbjwvcD4KPHA+Tm90aWZpY2F0aW9ucywgYXMgYW4gaW1wb3J0YW50IHBhcnQgb2YgdGhlIEFuZHJvaWQgVUksIGhhdmUgdGhlaXIgb3duIGRlc2lnbiBndWlkZWxpbmVzLiBUbyBsZWFybiBob3cgdG8gZGVzaWduIG5vdGlmaWNhdGlvbnMgYW5kIHRoZWlyIGludGVyYWN0aW9ucywgcmVhZCB0aGUgQW5kcm9pZCBEZXNpZ24gR3VpZGUgTm90aWZpY2F0aW9ucyB0b3BpYy48L3A+CjxwPk5vdGU6IEV4Y2VwdCB3aGVyZSBub3RlZCwgdGhpcyBndWlkZSByZWZlcnMgdG8gdGhlIDxjb2RlPk5vdGlmaWNhdGlvbkNvbXBhdC5CdWlsZGVyPC9jb2RlPiBjbGFzcwogaW4gdGhlIHZlcnNpb24gNCBTdXBwb3J0IExpYnJhcnkuIFRoZSBjbGFzcyA8Y29kZT5Ob3RpZmljYXRpb24uQnVpbGRlcjwvY29kZT4gd2FzCiBhZGRlZCBpbiBBbmRyb2lkIDMuMC48L3A+CjxoMj5Ob3RpZmljYXRpb24gRGlzcGxheSBFbGVtZW50czwvaDI+Cjxocj4KPHA+Tm90aWZpY2F0aW9ucyBpbiB0aGUgbm90aWZpY2F0aW9uIGRyYXdlciBjYW4gYXBwZWFyIGluIG9uZSBvZiB0d28gdmlzdWFsIHN0eWxlcywgZGVwZW5kaW5nIG9uIHRoZSB2ZXJzaW9uIGFuZCB0aGUgc3RhdGUgb2YgdGhlIGRyYXdlcjo8L3A+CjxkbD48ZHQ+Tm9ybWFsIHZpZXc8ZGQ+VGhlIHN0YW5kYXJkIHZpZXcgb2YgdGhlIG5vdGlmaWNhdGlvbnMgaW4gdGhlIG5vdGlmaWNhdGlvbiBkcmF3ZXIuPGR0PkJpZyB2aWV3PGRkPkEgbGFyZ2UgdmlldyB0aGF0"s visible when the notification is expanded. Big view is part of the expanded notification feature available as of Android 4.1.
These styles are described in the following sections.
A notification in normal view appears in an area that's up to 64 dp tall. Even if you create a notification with a big view style, it will appear in normal view until it's expanded. This is an example of a normal view:
Figure 3. Notification in normal view.
The callouts in the illustration refer to the following:
setWhen()
;
if you don"t it defaults to the time that the system received the notification.
A notification's big view appears only when the notification is expanded, which happens when the notification is at the top of the notification drawer, or when the user expands the notification with a gesture. Expanded notifications are available starting with Android 4.1.
The following screenshot shows an inbox-style notification:
Figure 4. Big view notification.
Notice that the big view shares most of its visual elements with the normal view. The only difference is callout number 7, the details area. Each big view style sets this area in a different way. The available styles are:
All of the big view styles also have the following content options that aren"t available in normal view:
Applying a big view style to a notification is described in the section Applying a big view style to a notification.
You specify the UI information and actions for a notification in a NotificationCompat.Builder
object.
To create the notification itself, you call NotificationCompat.Builder.build()
,
which returns a Notification
object
containing your specifications. To issue the notification, you pass the Notification
object
to the system by calling NotificationManager.notify()
.
A Notification
object must contain
the following:
setSmallIcon()
setContentTitle()
setContentText()
All other notification settings and contents are optional. To learn more about them, see the reference documentation for NotificationCompat.Builder
.
Although they're optional, you should add at least one action to your notification. An action allows users to go directly from the notification to an Activity
in
your application, where they can look at one or more events or do further work.
A notification can provide multiple actions. You should always define the action that's triggered when the user clicks the notification; usually this action opens an Activity
in
your application. You can also add buttons to the notification that perform additional actions such as snoozing an alarm or responding immediately to a text message; this feature is available as of Android 4.1. If you use additional action buttons, you must
also make their functionality available in an Activity
in
your app; see the section Handling compatibility for more details.
Inside a Notification
,
the action itself is defined by a PendingIntent
containing
an Intent
that
starts an Activity
in
your application. To associate the PendingIntent
with
a gesture, call the appropriate method of NotificationCompat.Builder
.
For example, if you want to start Activity
when
the user clicks the notification text in the notification drawer, you add the PendingIntent
by
callingsetContentIntent()
.
Starting an Activity
when
the user clicks the notification is the most common action scenario. You can also start an Activity
when
the user dismisses a notification. In Android 4.1 and later, you can start an Activity
from
an action button. To learn more, read the reference guide for NotificationCompat.Builder
.
The following snippet illustrates a simple notification that specifies an activity to open when the user clicks the notification. Notice that the code creates a TaskStackBuilder
object
and uses it to create the PendingIntent
for
the action. This pattern is explained in more detail in the section Preserving Navigation when Starting
an Activity:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ResultActivity.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
That's it. Your user has now been notified.
To have a notification appear in a big view when it's expanded, first create a NotificationCompat.Builder
object
with the normal view options you want. Next, call Builder.setStyle()
with
a big view style object as its argument.
Remember that expanded notifications are not available on platforms prior to Android 4.1. To learn how to handle notifications for Android 4.1 and for earlier platforms, read the section Handling compatibility.
For example, the following code snippet demonstrates how to alter the notification created in the previous snippet to use the Inbox big view style:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Event tracker")
.setContentText("Events received")
NotificationCompat.InboxStyle inboxStyle =
new NotificationCompat.InboxStyle();
String[] events = new String[6];
// Sets a title for the Inbox style big view
inboxStyle.setBigContentTitle("Event tracker details:");
...
// Moves events into the big view
for (int i=0; i < events.length; i++) {
inboxStyle.addLine(events[i]);
}
// Moves the big view style object into the notification object.
mBuilder.setStyle(inBoxStyle);
...
// Issue the notification here.
Not all notification features are available for a particular version, even though the methods to set them are in the support library classNotificationCompat.Builder
.
For example, action buttons, which depend on expanded notifications, only appear on Android 4.1 and higher, because expanded notifications themselves are only available on Android 4.1 and higher.
To ensure the best compatibility, create notifications with NotificationCompat
and
its subclasses, particularly NotificationCompat.Builder
.
In addition, follow this process when you implement a notification:
Activity
in
your app. You may want to add a new Activity
to
do this.
For example, if you want to use addAction()
to
provide a control that stops and starts media playback, first implement this control in an Activity
in
your app.
Activity
,
by having it start when users click the notification. To do this, create a PendingIntent
for
the Activity
.
Call setContentIntent()
to
add the PendingIntent
to
the notification.Activity
that
starts when users click the notification.
When you need to issue a notification multiple times for the same type of event, you should avoid making a completely new notification. Instead, you should consider updating a previous notification, either by changing some of its values or by adding to it, or both.
For example, Gmail notifies the user that new emails have arrived by increasing its count of unread messages and by adding a summary of each email to the notification. This is called "stacking" the notification; it's described in more detail in the Notifications Design guide.
Note: This Gmail feature requires the "inbox" big view style, which is part of the expanded notification feature available starting in Android 4.1.
The following section describes how to update notifications and also how to remove them.
To set up a notification so it can be updated, issue it with a notification ID by calling NotificationManager.notify(ID,
notification)
. To update this notification once you've issued it, update or create a NotificationCompat.Builder
object,
build a Notification
object
from it, and issue the Notification
with
the same ID you used previously. If the previous notification is still visible, the system updates it from the contents of the Notification
object.
If the previous notification has been dismissed, a new notification is created instead.
The following snippet demonstrates a notification that is updated to reflect the number of events that have occurred. It stacks the notification, showing a summary:
mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Sets an ID for the notification, so it can be updated
int notifyID = 1;
mNotifyBuilder = new NotificationCompat.Builder(this)
.setContentTitle("New Message")
.setContentText("You've received new messages.")
.setSmallIcon(R.drawable.ic_notify_status)
numMessages = 0;
// Start of a loop that processes data and then notifies the user
...
mNotifyBuilder.setContentText(currentText)
.setNumber(++numMessages);
// Because the ID remains unchanged, the existing notification is
// updated.
mNotificationManager.notify(
notifyID,
mNotifyBuilder.build());
...
This produces a notification that looks like this:
Figure 5. Updated notification displayed in the notification drawer.
Notifications remain visible until one of the following happens:
setAutoCancel()
when
you created the notification.cancel()
for
a specific notification ID. This method also deletes ongoing notifications.cancelAll()
,
which removes all of the notifications you previously issued.
When you start an Activity
from
a notification, you must preserve the user's expected navigation experience. Clicking Back should take the user back through the application's normal work flow to the Home screen, and clicking Recents should
show the Activity
as
a separate task. To preserve the navigation experience, you should start the Activity
in
a fresh task. How you set up the PendingIntent
to
give you a fresh task depends on the nature of the Activity
you're
starting. There are two general situations:
Activity
that's
part of the application's normal workflow. In this situation, set up the PendingIntent
to
start a fresh task, and provide the PendingIntent
with
a back stack that reproduces the application's normal Back behavior.
Notifications from the Gmail app demonstrate this. When you click a notification for a single email message, you see the message itself. Touching Back takes you backwards through Gmail to the Home screen, just as if you had entered Gmail from the Home screen rather than entering it from a notification.
This happens regardless of the application you were in when you touched the notification. For example, if you're in Gmail composing a message, and you click a notification for a single email, you go immediately to that email. Touching Back takes you to the inbox and then the Home screen, rather than taking you to the message you were composing.
Activity
if
it's started from a notification. In a sense, the Activity
extends
the notification by providing information that would be hard to display in the notification itself. For this situation, set up the PendingIntent
to
start in a fresh task. There's no need to create a back stack, though, because the started Activity
isn't
part of the application's activity flow. Clicking Back will still take the user to the Home screen.
To set up a PendingIntent
that
starts a direct entry Activity
,
follow these steps:
Activity
hierarchy
in the manifest.
Activity
you're
starting by adding a
element
as the child of the
.
For this element, set android:name="android.support.PARENT_ACTIVITY"
.
Set android:value="
where
is
the value of android:name
for
the parent element.
See the following XML for an example.
android:parentActivityName
attribute
to the
element
of the Activity
you're
starting.
The final XML should look like this:
android:name=".MainActivity"
android:label="@string/app_name" >
android:name=".ResultActivity"
android:parentActivityName=".MainActivity">
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>
Intent
that
starts the Activity
:
Intent
to
start the Activity
.TaskStackBuilder.create()
.addParentStack()
.
For each Activity
in
the hierarchy you've defined in the manifest, the back stack contains an Intent
object
that starts the Activity
.
This method also adds flags that start the stack in a fresh task.
Note: Although the argument to addParentStack()
is
a reference to the started Activity
,
the method call doesn't add the Intent
that
starts the Activity
.
Instead, that's taken care of in the next step.
Intent
that
starts the Activity
from
the notification, by calling addNextIntent()
.
Pass the Intent
you
created in the first step as the argument to addNextIntent()
.Intent
objects
on the stack by calling TaskStackBuilder.editIntentAt()
.
This is sometimes necessary to ensure that the target Activity
displays
meaningful data when the user navigates to it using Back.PendingIntent
for
this back stack by calling getPendingIntent()
.
You can then use this PendingIntent
as
the argument to setContentIntent()
.
The following code snippet demonstrates the process:
...
Intent resultIntent = new Intent(this, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent to the top of the stack
stackBuilder.addNextIntent(resultIntent);
// Gets a PendingIntent containing the entire back stack
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
...
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(id, builder.build());
The following section describes how to set up a special activity PendingIntent
.
A special Activity
doesn't
need a back stack, so you don't have to define its Activity
hierarchy
in the manifest, and you don't have to call addParentStack()
to
build a back stack. Instead, use the manifest to set up the Activity
task
options, and create the PendingIntent
by
calling getActivity()
:
element
for the Activity
android:name="activityclass"
android:taskAffinity=""
FLAG_ACTIVITY_NEW_TASK
flag
that you set in code, this ensures that this Activity
doesn't
go into the application's default task. Any existing tasks that have the application's default affinity are not affected.android:excludeFromRecents="true"
This snippet shows the element:
android:name=".ResultActivity"
...
android:launchMode="singleTask"
android:taskAffinity=""
android:excludeFromRecents="true">
...
Intent
that
starts the Activity
.Activity
to
start in a new, empty task by calling setFlags()
with
the flags FLAG_ACTIVITY_NEW_TASK
andFLAG_ACTIVITY_CLEAR_TASK
.Intent
.PendingIntent
from
the Intent
by
calling getActivity()
.
You can then use this PendingIntent
as
the argument to setContentIntent()
.
The following code snippet demonstrates the process:
// Instantiate a Builder object.
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
// Creates an Intent for the Activity
Intent notifyIntent =
new Intent(new ComponentName(this, ResultActivity.class));
// Sets the Activity to start in a new, empty task
notifyIntent.setFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK);
// Creates the PendingIntent
PendingIntent notifyIntent =
PendingIntent.getActivity(
this,
0,
notifyIntent
PendingIntent.FLAG_UPDATE_CURRENT
);
// Puts the PendingIntent into the notification builder
builder.setContentIntent(notifyIntent);
// Notifications are issued by sending them to the
// NotificationManager system service.
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Builds an anonymous Notification object from the builder, and
// passes it to the NotificationManager
mNotificationManager.notify(id, builder.build());
Notifications can include an animated progress indicator that shows users the status of an ongoing operation. If you can estimate how long the operation takes and how much of it is complete at any time, use the "determinate" form of the indicator (a progress bar). If you can't estimate the length of the operation, use the "indeterminate" form of the indicator (an activity indicator).
Progress indicators are displayed with the platform's implementation of the ProgressBar
class.
To use a progress indicator on platforms starting with Android 4.0, call setProgress()
.
For previous versions, you must create your own custom notification layout that includes a ProgressBar
view.
The following sections describe how to display progress in a notification using setProgress()
.
To display a determinate progress bar, add the bar to your notification by calling setProgress()
setProgress(max, progress, false)
and then issue the notification. As your operation proceeds, increment progress
,
and update the notification. At the end of the operation, progress
should equal max
.
A common way to call setProgress()
is
to set max
to 100 and then increment progress
as
a "percent complete" value for the operation.
You can either leave the progress bar showing when the operation is done, or remove it. In either case, remember to update the notification text to show that the operation is complete. To
remove the progress bar, call setProgress()
setProgress(0, 0, false)
. For example:
...
mNotifyManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentTitle("Picture Download")
.setContentText("Download in progress")
.setSmallIcon(R.drawable.ic_notification);
// Start a lengthy operation in a background thread
new Thread(
new Runnable() {
@Override
public void run() {
int incr;
// Do the "lengthy" operation 20 times
for (incr = 0; incr <= 100; incr+=5) {
// Sets the progress indicator to a max value, the
// current completion percentage, and "determinate"
// state
mBuilder.setProgress(100, incr, false);
// Displays the progress bar for the first time.
mNotifyManager.notify(0, mBuilder.build());
// Sleeps the thread, simulating an operation
// that takes time
try {
// Sleep for 5 seconds
Thread.sleep(5*1000);
} catch (InterruptedException e) {
Log.d(TAG, "sleep failure");
}
}
// When the loop is finished, updates the notification
mBuilder.setContentText("Download complete")
// Removes the progress bar
.setProgress(0,0,false);
mNotifyManager.notify(ID, mBuilder.build());
}
}
// Starts the thread by calling the run() method in its Runnable
).start();
The resulting notifications are shown in figure 6. On the left side is a snapshot of the notification during the operation; on the right side is a snapshot of it after the operation has finished.
Figure 6. The progress bar during and after the operation.
To display an indeterminate activity indicator, add it to your notification with setProgress(0,
0, true)
(the first two arguments are ignored), and issue the notification. The result is an indicator that has the same style as a progress bar, except that its animation is ongoing.
Issue the notification at the beginning of the operation. The animation will run until you modify your notification. When the operation is done, call setProgress()
setProgress(0, 0, false)
and then update the notification to remove the activity indicator. Always do this; otherwise, the animation will run even when the operation is complete. Also remember to change the notification text to indicate that the
operation is complete.
To see how activity indicators work, refer to the preceding snippet. Locate the following lines:
// Sets the progress indicator to a max value, the current completion
// percentage, and "determinate" state
mBuilder.setProgress(100, incr, false);
// Issues the notification
mNotifyManager.notify(0, mBuilder.build());
Replace the lines you've found with the following lines:
// Sets an activity indicator for an operation of indeterminate length
mBuilder.setProgress(0, 0, true);
// Issues the notification
mNotifyManager.notify(0, mBuilder.build());
The resulting indicator is shown in figure 7:
Figure 7. An ongoing activity indicator.
The notifications framework allows you to define a custom notification layout, which defines the notification"s appearance in a RemoteViews
object.
Custom layout notifications are similar to normal notifications, but they're based on a RemoteViews
defined
in a XML layout file.
The height available for a custom notification layout depends on the notification view. Normal view layouts are limited to 64 dp, and expanded view layouts are limited to 256 dp.
To define a custom notification layout, start by instantiating a RemoteViews
object
that inflates an XML layout file. Then, instead of calling methods such as setContentTitle()
,
call setContent()
.
To set content details in the custom notification, use the methods inRemoteViews
to
set the values of the view's children:
.xml
RemoteViews
methods
to define your notification's icons and text. Put this RemoteViews
object
into yourNotificationCompat.Builder
by
calling setContent()
.
Avoid setting a background Drawable
on
your RemoteViews
object,
because your text color may become unreadable.
The RemoteViews
class
also includes methods that you can use to easily add a Chronometer
or ProgressBar
to
your notification's layout. For more information about creating custom layouts for your notification, refer to the RemoteViews
reference
documentation.
Caution: When you use a custom notification layout, take special care to ensure that your custom layout works with different device orientations and resolutions. While this advice applies to all View layouts, it's especially important for notifications because the space in the notification drawer is very restricted. Don't make your custom layout too complex, and be sure to test it in various configurations.
Always use style resources for the text of a custom notification. The background color of the notification can vary across different devices and versions, and using style resources helps you account for this. Starting in Android 2.3, the system defined a style for the standard notification layout text. If you use the same style in applications that target Android 2.3 or higher, you'll ensure that your text is visible against the display background.
當你點擊一個view的時候,它的底層還有其他的View/ViewGroup,那麼這個點擊事件誰處理,它又是怎麼傳遞的在控件樹上?我們知道點擊事件是從Activity-&g
在沒有google的時代,當在開發中遇到問題時,程序員唯一的方式就是去讀源代碼,雖然現在可以通過搜索引擎解決大部分開發問題,但是要想理解其內部運行原理,還是要去讀源代碼。
費了好久終於下載好了源碼4.4.2 (如果想只是了解下編譯過程 能用現成的最好了)下載好後 接下來開始編譯了我的虛擬機Ubuntu配置 源碼有16g+ (.repo 占了
本文實例講述了Android編程實現讀取手機聯系人、撥號、發送短信及長按菜單操作方法。分享給大家供大家參考,具體如下:1.Andrid項目結構圖↓主要操作圖中紅色方框內的