1. ActivityMain.java
package com.lec.notification;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RemoteViews;
import android.widget.TextView;
public class ActivityMain extends Activity {
TextView tv;
Button btnShowNotification = null;
Button btnCleanNotification = null;
Button runningNotification = null;
private static final int NOTIFICATION_ID = 101;
private Notification notification = null;
private NotificationManager notificationManager = null;
private PendingIntent contentIntent = null;
private Thread thread = null;
private Handler handler = null;
private int len = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//取得NotificationManager
notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
//Notification 点击后跳转的Activity 可以是Service或者Broadcast
contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, ActivityMain.class), 0);
//创建Notification 状态栏显示的,一个图标和后缀文字,
notification = new Notification(R.drawable.icon, "Notification coming", System.currentTimeMillis());
//设置声音,震动,灯光的。可以用 "|=" 分别设置 DEFAULT_SOUND,DEFAULT_VIBRATE,DEFAULT_LIGHTS
//如果是振动或者全部,必须在AndroidManifest.xml加入振动权限 android.permission.VIBRATE
notification.defaults = Notification.DEFAULT_ALL;
//指定Flag,Notification.FLAG_AUTO_CANCEL意指点击这个Notification后,立刻取消自身
notification.flags=Notification.FLAG_AUTO_CANCEL;
//小图标上显示的数字
notification.number = 3;
//设置下来后的显示内同
notification.setLatestEventInfo(this,
"contentTitle",
"contentText",
contentIntent);
//设置下来后自定义的显示内容
RemoteViews contentView = new RemoteViews(getApplication().getPackageName(), R.layout.notification_view);
contentView.setTextViewText(R.id.notificationTitle, "Diownload");
contentView.setTextViewText(R.id.notificationPercent, len+"%");
contentView.setProgressBar(R.id.notificationProgress, 100, len, true);
notification.contentView = contentView;
notification.contentIntent = contentIntent;
btnShowNotification = (Button)findViewById(R.id.mainact_btn_shownotification);
btnShowNotification.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
showNotification();
}
});
btnCleanNotification = (Button)findViewById(R.id.mainact_btn_cleannotification);
btnCleanNotification.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
cleanNotification();
}
});
runningNotification = (Button)findViewById(R.id.mainact_btn_runingnotification);
runningNotification.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
runningNotification();
}
});
}
@Override
protected void onPause() {
// notificationManager.cancel(NOTIFICATION_ID);
super.onPause();
}
private void showNotification(){
//显示这个notification
notificationManager.notify(NOTIFICATION_ID, notification);
}
private void cleanNotification(){
notificationManager.cancel(NOTIFICATION_ID);
}
private void runningNotification(){
notificationManager.notify(NOTIFICATION_ID, notification);
thread = new Thread(new Runnable() {
@Override
public void run() {
Thread.currentThread();
while(len <= 100){
Log.e("----len", len+"");
Message msg = handler.obtainMessage();
msg.arg1 = len;
msg.sendToTarget();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
++len;
}
}
});
thread.start();
handler = new Handler(){
@Override
public void handleMessage(Message msg) {
notification.contentView.setProgressBar(R.id.notificationProgress, 100, msg.arg1, false);
notification.contentView.setTextViewText(R.id.notificationPercent, msg.arg1+"%");
notificationManager.notify(NOTIFICATION_ID, notification);
if(msg.arg1 >= 100){
notificationManager.cancel(NOTIFICATION_ID);
len = 0;
}
super.handleMessage(msg);
}
};
}
}
2. main.xml
<?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:id="@+id/mainact_btn_shownotification"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="show Notification"/>
<Button
android:id="@+id/mainact_btn_cleannotification"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="clean Notification"/>
<Button
android:id="@+id/mainact_btn_runingnotification"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="run Notification"/>
</LinearLayout>
3.notification_view.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="3dp"
>
<ImageView android:id="@+id/notificationImage"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:src="@android:drawable/stat_sys_download"
/>
<TextView android:id="@+id/notificationTitle"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_toRightOf="@id/notificationImage"
android:layout_alignParentRight="true"
android:paddingLeft="6dp"
android:text="1111111111"/>
<TextView android:id="@+id/notificationPercent"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="@id/notificationImage"
android:paddingTop="2dp"
android:text="2222222222"/>
<ProgressBar android:id="@+id/notificationProgress"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="@id/notificationTitle"
android:layout_alignLeft="@id/notificationTitle"
android:layout_alignParentRight="true"
android:layout_alignTop="@id/notificationPercent"
android:paddingLeft="6dp"
android:paddingRight="3dp"
android:paddingTop="2dp"
style="?android:attr/progressBarStyleHorizontal"/>
</RelativeLayout>