android4.x Notification使用

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




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
非常抱歉,我之前的回答有误。在最新的 Android Support Library 中,`android.support.v4.media.app.NotificationCompat.MediaStyle` 已被弃用。 为了在通知中显示媒体控制按钮,您可以使用 `androidx.media.app.NotificationCompat.MediaStyle` 类。您需要确保您的项目已迁移到 AndroidX 库,并使用最新版本的 `androidx.appcompat:appcompat` 和 `androidx.media:media` 库。 以下是一个使用 `androidx.media.app.NotificationCompat.MediaStyle` 的示例代码: ```java NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("音乐播放器") .setContentText("正在播放歌曲") .setStyle(new androidx.media.app.NotificationCompat.MediaStyle() .setShowActionsInCompactView(0, 1, 2) // 显示媒体控制按钮 .setMediaSession(mediaSession.getSessionToken())) .addAction(R.drawable.ic_previous, "上一曲", previousPendingIntent) .addAction(R.drawable.ic_pause, "暂停", pausePendingIntent) .addAction(R.drawable.ic_next, "下一曲", nextPendingIntent) .setPriority(NotificationCompat.PRIORITY_HIGH); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); notificationManager.notify(notificationId, builder.build()); ``` 请确保您的项目已正确配置并引入了最新版本的 AndroidX 库。如果您仍然遇到问题,请检查您的项目配置和依赖项是否正确。如果需要进一步帮助,请随时提问!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值