android4.x中notification的使用
1. 创建一个通知的步骤:
1> 创建一个Notification的builder
2> 定义通知的Action
3> 设置点击通知跳转的目的地
4> 获取服务并发送通知
//setp1: NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setSmallIcon(R.drawable.demo); builder.setContentTitle("4.x Notification"); bulider.setContentText("Hello World"); //step2: Intent intent = new Intent(this, demoActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this,0,pendingIntent,PendingIntent.FLAG_UPDATE_CURRENT); //step3: builder.setContentIntent(pendingIntent); //step4: int notificationId = 1; NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); nm.notfy(notificationId,builder.build());
2. 如何在通知使用进度条
1> 如果应用可以明确该进度的大小
2> 如果应用不明确该进度的大小
//setProgress最后一个参数为false builder.setProgress(100,progress,false); nm.notify(notificationId,builer.build()); //setProgress最后一个参数为true builder.setProgress(100,progress,true); nm.notify(notificationId,builer.build());
3. 更新通知
1> 更新通知可以使用前面已经创建好的builder,更改相关内容,再调用服务发送;2> 也可以重新创建builder,但是要保证ID一样,否则将是一个新的通知;
4. 示例代码
package zhangchao.android.notification;
import zhangchao.android.apidemos.MainActivity;
import zhangchao.android.apidemos.R;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class NotificationDemo2 extends Activity {
NotificationManager notificationManager;
NotificationCompat.Builder builder;
final int notification_id = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification2);
builder = getBuilder();
// 4. 获取通知服务
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Button send = (Button) findViewById(R.id.send);
send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 5.发送通知
//notificationManager.notify(notification_id, builder.build());
new Thread(new UpdateProgressTask()).start();
}
});
Button update = (Button) findViewById(R.id.update);
update.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 更新通知
NotificationCompat.Builder updateBuilder = new NotificationCompat.Builder(
NotificationDemo2.this);
updateBuilder.setSmallIcon(R.drawable.ic_launcher);
updateBuilder.setContentTitle("更新通知");
updateBuilder.setContentText("新通知内容");
Intent intent = new Intent(NotificationDemo2.this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(NotificationDemo2.this, 0,
intent, 0);
// 3. Set the Notification's Click Behavior
updateBuilder.setContentIntent(pendingIntent);
notificationManager.notify(notification_id+1, updateBuilder.build());
}
});
Button cancel = (Button) findViewById(R.id.cancel);
cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
notificationManager.cancel(notification_id);
}
});
}
private NotificationCompat.Builder getBuilder() {
NotificationCompat.Builder builder;
// 1. create a notification builder
builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContentTitle("My notification");
builder.setContentText("Hello World");
// 2. define the notification's Action
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
intent, 0);
// 3. Set the Notification's Click Behavior
builder.setContentIntent(pendingIntent);
return builder;
}
class UpdateProgressTask implements Runnable {
@Override
public void run() {
for(int inc = 0; inc <= 100; inc+=5 ) {
builder.setProgress(100, inc, true);
//builder.setContentText("下载进度:" + inc + "%");
notificationManager.notify(notification_id, builder.build());
try {
Thread.sleep(5*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
builder.setContentText("download complete");
builder.setProgress(0, 0, false);
notificationManager.notify(notification_id, builder.build());
}
}
}
参考: http://developer.android.com/training/notify-user/index.html