android-Notification

public class Text_Noti extends Activity {
	private Button sendBtn, cancelBtn;
	private Notification notification;
	private NotificationManager nManager;
	private Builder builder; // android.app.Notification.Builder;
	private Intent intent;
	private PendingIntent pIntent;
	// Notification的标示ID

	private static final int NOTIFYID_1 = 1;
	private static final int NOTIFYID_2 = 2;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.text_noti);

		sendBtn = (Button) this.findViewById(R.id.send);
		cancelBtn = (Button) this.findViewById(R.id.cancel);
		nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

		sendBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				Intent it = new Intent(getApplicationContext(),
						OtherActivity.class);
				PendingIntent pit = PendingIntent.getActivity(
						getApplicationContext(), 0, it, 0);

				// 2.新建一个Builder类
				builder = new Notification.Builder(getApplicationContext());
				// 依次设置是否打开自动消失,状态栏的通知提示信息,通知图标
				// 通知内容的标题,通知内容
				builder.setAutoCancel(true);
				builder.setTicker("你有新的信息!");
				builder.setSmallIcon(R.drawable.ic_launcher);
				builder.setContentTitle("中奖通知");
				builder.setContentText("恭喜你是第100名用户,点击抽奖...");
				// 设置自定义的声音或者震动效果
				// builder.setSound(Uri.parse("android.resource://com.jay.demo.notificationdemo2.res/"
				// + R.raw.login));
				builder.setContentIntent(pit);
				// 3.为notification赋值,调用Builder.build()方法
				notification = builder.build();
				// 4.使用NotificationManage
				nManager.notify(NOTIFYID_1, notification);
			}
		});

		cancelBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				nManager.cancel(NOTIFYID_1);//关闭Notification
			}
		});
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值