notification android原生消息通知代码详解

package us.guaju.notification;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
	private static final int TAG = 8888;
	protected static final int REQUESTCODE = 0;
	private Button send_notification, clear;
	private NotificationManager notificationManager;
	private Notification notificastion;
	private int count = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		send_notification = (Button) findViewById(R.id.send_notification);
		clear = (Button) findViewById(R.id.clear);

		notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		// 点击button发送通知
		send_notification.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Notification.Builder builder = new Notification.Builder(
						MainActivity.this);
				builder.setContentTitle("我是通知");
				builder.setContentText("我是通知内容");
				builder.setContentInfo("我是通知附加信息" + count);
				// 创建一个普通的意图,为下方的pendingIntent做准备
				Intent intent = new Intent();
				intent.setAction(Intent.ACTION_DIAL);
				// 创建pendingIntent 传入上文定制好的意图
				PendingIntent pendingIntent = PendingIntent.getActivity(
						MainActivity.this, REQUESTCODE, intent,
						Intent.FLAG_ACTIVITY_NEW_TASK);
				// 如此点击完成后会跳转到拨号界面
				builder.setContentIntent(pendingIntent);
				builder.setSmallIcon(R.drawable.ic_launcher);
				notificastion = builder.getNotification();
				count++;
				notificationManager.notify(TAG, notificastion);
			}
		});
		clear.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				notificationManager.cancelAll();
			}
		});
	}
}
如果有不懂的可以直接查看developer.android.com去学习。此篇文章基本能满足日常需要
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值