notification的简单使用

package com.yaotai.standarview;
import com.yaotai.standarview.R;

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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RemoteViews;


public class NotificationActivity extends Activity {

	NotificationManager mNotificationManager;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO 自动生成的方法存根
		super.onCreate(savedInstanceState);
		setContentView(R.layout.notification);
		
		//1.设置NotificationManager实例
		mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		
		Button button = (Button) findViewById(R.id.buttonnotification1);
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO 自动生成的方法存根
				//2.拼装Notification实例
			   
			    //这是上面的内容
				Notification notificaty = new Notification(R.drawable.ic_launcher, "You have one email",System.currentTimeMillis() );
				//下面是拼装形式
//				Notification notification = new Notification();
//				notification.icon=R.drawable.ic_launcher;
//				notification.tickerText="启动其他";
//				notification.when=System.currentTimeMillis();
				
				
				PendingIntent pendingIntent = PendingIntent.getActivity(NotificationActivity.this, 345, 
					 new Intent(NotificationActivity.this,FormWidgetActivity.class), 0);
				//这是展开的内容
				//notification.setLatestEventInfo(NotificationActivity.this, "Your Email", "今天天气不错,要不要出去玩啊???", pendingIntent);
				//可用下面代替
				notificaty.contentIntent = pendingIntent;
				notificaty.contentView=new RemoteViews(getPackageName(),R.layout.customnotification);
				
				//3.触发提醒
				mNotificationManager.notify(123, notificaty);
			}
		});
		

		
		Button button2 = (Button) findViewById(R.id.buttonnotification2);
		button2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO 自动生成的方法存根
				mNotificationManager.cancel(123);
			}
		});
		
		
		
		
		
		
		
	}
	@Override
	protected void onStop() {
		// TODO 自动生成的方法存根
		mNotificationManager.cancel(123);
		super.onStop();
	}

	
	
}

notification使用步骤:

1.建立NotificationManager对象

2.建立Notification对象(下拉前的)

a.建立PendingIntent对象

b.调用setLatestEventInfo()方法,把PendingIntent加载(这是下拉后的)

3.触发提醒



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值