1. 在Notification中显示ProgressBar,效果图:

2. 实现步骤:
(1)在res/layout/目录中定义notification.xml文件,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:gravity = "center_vertical"
>
<ImageView
android:id = "@+id/image"
android:layout_width = "wrap_content"
android:layout_height = "fill_parent"
/>
<ProgressBar
android:id = "@+id/progressBar"
android:layout_width = "180dip"
android:layout_height = "wrap_content"
style = "?android:attr/progressBarStyleHorizontal"
android:layout_gravity = "center_vertical"
/>
<TextView
android:id = "@+id/tip"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:textSize = "16px"
android:textColor = "#FF0000"
/>
</LinearLayout>
(2)主Activity实现:
public class NotificationActivity extends Activity {
private NotificationManager mNotificationManager;
private RemoteViews mRemoteViews;
private Intent mIntent;
private PendingIntent mPendingIntent;
private int mProgress = 0;
private Notification notification = new Notification();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**
* Activity主布局,布局中存在一个Button,单击Button后启动多线程并向StatusBar发送Notification提醒。
*/
setContentView(R.layout.main);
/**
* 获取Notification管理器。
*/
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
/**
* 利用notification.xml文件创建RemoteView对象。
*/
mRemoteViews = new RemoteViews(getPackageName(), R.layout.notification);
/**
* 单击Notification时发出的Intent消息。
*/
mIntent = new Intent(NotificationActivity.this, 自定义Activity.class);
mPendingIntent = PendingIntent.getActivity(NotificationActivity.this, 0, mIntent, 0);
/**
* 获取Button对象,设置单击事件,单击后启动多线程向StatusBar发送Notification提醒。
*/
Button sendNotification = (Button) findViewById(R.id.sendNotification);
sendNotification.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
/**
* 设置Notification的Icon图标。
*/
notification.icon = R.drawable.icon;
mRemoteViews.setImageViewResource(R.id.image, R.drawable.icon);
/**
* 启动多线程更新Notification提醒。
*/
new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 20; i++) {
mProgress = (i + 1) * 5;
try {
if (i < 19) {
Thread.sleep(1000);
} else {
Thread.currentThread().interrupt();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
Message message = new Message();
mHandler.sendMessage(message);
}
}
}).start();
}
});
}
private Handler mHandler = new Handler() {
public void handleMessage(Message message) {
/**
* 设置RemoteView组件中进度条的进度值。
*/
mRemoteViews.setProgressBar(R.id.progressBar, 100, mProgress, false);
mRemoteViews.setTextViewText(R.id.tip, "Download:" + mProgress + "%");
/**
* 给Notification设置布局。
*/
notification.contentView = mRemoteViews;
/**
* 给Notification设置Intent,单击Notification会发出这个Intent。
*/
notification.contentIntent = mPendingIntent;
/**
* 发送Notification提醒。
*/
mNotificationManager.notify(0, notification);
super.handleMessage(message);
}
};
} 转自:http://blog.youkuaiyun.com/mayingcai1987/archive/2011/03/06/6226685.aspx