您的位置:首页 > 移动开发 > Android开发

Android倚天剑之Notification之动感地带

2013-06-09 02:05 218 查看
传送门 ☞ 轮子的专栏 ☞ 转载请注明 ☞ http://blog.csdn.net/leverage_1229
前面我们谈到怎样管理和删除通知,以及怎样实现保存用户预期导航体验的通知。本文作为Android平台Notification系列的最终章,我们将会给通知融入更多DIY的元素,大胆地在这把“倚天剑”上烙下自己的印记^-^。亲,那些RPG网游的铁匠NPC貌似都有这功能。。。

1显示进度的通知

通知可以包括一个动画进度指示器以显示用户正在运行的操作的进度状态。如果你能估计这种操作需要花费多长时间,可以使用“determinate”形式的指示器(一个progress bar)。如果你不能估计花费的时间,那就使用“indeterminate”形式的指示器。

1.1显示一个固定的时间进度指示器

1.1.1技术要点

调用setProgress()方法添加进度指示器到你的通知中。

1.1.2代码陈列

final NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setContentInfo(String.valueOf(++progressNum))
.setContentTitle("Picture Download")
.setContentText("Download in progress")
.setDefaults(Notification.DEFAULT_ALL)
.setLargeIcon(icon)
.setSmallIcon(R.drawable.stat_notify_gmail)
.setTicker("Progress Notification")
.setOngoing(true);
// 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) {
builder.setProgress(100, incr, false);
mNotiMgr.notify(PROGRESS_NOTI_ID, builder.build());

try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Log.d(TAG, "sleep failure");
}
}
builder.setContentText("Download complete")
.setProgress(0, 0, false)
.setOngoing(false);
mNotiMgr.notify(PROGRESS_NOTI_ID, builder.build());
}
}).start();

1.1.3效果展示



1.2显示一个持续的Activity指示器

1.2.1技术要点

调用setProgress(0, 0, true)添加Activity指示器到你的通知中,前面两个参数可以忽略。

1.2.2代码陈列

builder.setProgress(100, incr, false);
mNotiMgr.notify(0, mBuilder.build());

1.2.3效果展示



2自定义样式的通知

通知框架允许你自定义通知布局,它在一个RemoteViews对象中定义了通知的布局。自定义布局通知和正常的通知类似,它们都是基于一个RemoteViews定义在一个XML布局文件中。自定义通知的可用高度取决于通知view的布局。正常view布局限制为64dp,展开view布局限制为256dp。自定义通知布局,通过实例化一个RemoteViews对象然后inflates一个xml布局文件启动。不再调用setContentTitle()方法,而使用setContent()方法来设置自定义通知的内容细节。使用这个方法在RemoteViews中来设置view子类的值。

2.1技术要点

2.1.1为通知创建一个单独的xml布局文件

2.1.2在应用程序中,使用RemoteViews方法来定义你通知的icon和文本

调用setContent()方法put这个RemoteViews对象到你的NotificationCompat.Builder中。避免正在RemoteViews对象中设置Drawable背景,因为你的文本颜色可能会变的看不清。

2.2代码陈列

2.2.1工程包目录



2.2.2自定义样式通知创建和发布方法:showCustomNoti()

/**
* 自定义样式通知
*/
private void showCustomNoti() {
RemoteViews views = new RemoteViews(getPackageName(), R.layout.custom);
Intent intent = new Intent(INTENT_ACTION);
intent.putExtra("isPlay", true);

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
views.setOnClickPendingIntent(R.id.play_pause_music, pendingIntent);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setContent(views)
.setDefaults(Notification.DEFAULT_ALL)
.setLargeIcon(icon)
.setSmallIcon(R.drawable.music_icon)
.setTicker("Custom Notification")
.setOngoing(true);
mNotiMgr.notify(CUSTOM_NOTI_ID, builder.build());
}

2.2.3自定义通知布局文件:custom.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical" >

<ImageView
android:id="@+id/songer"
android:layout_width="64dp"
android:layout_height="64dp"
android:src="@drawable/songer" />

<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical"
android:layout_weight="1">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/song_name"
android:textSize="18sp"
android:gravity="center_horizontal" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/songer_name"
android:textSize="14sp"
android:gravity="center_horizontal" />

</LinearLayout>

<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:layout_weight="1" >

<ImageView
android:id="@+id/last_music"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="1"
android:src="@drawable/music_previous" />

<ImageView
android:id="@+id/play_pause_music"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="1"
android:src="@drawable/music_play" />

<ImageView
android:id="@+id/next_music"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="1"
android:src="@drawable/music_next" />
</LinearLayout>

</LinearLayout>

2.3效果展示







2.4源码下载

点我下载源码

3将来的议题

通知的附加操作、通知的优先级。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: