notification的简单使用

来源:互联网 发布:阿里云客服待遇怎么算 编辑:程序博客网 时间:2024/06/05 14:14
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;@Overrideprotected 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() {@Overridepublic 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() {@Overridepublic void onClick(View v) {// TODO 自动生成的方法存根mNotificationManager.cancel(123);}});}@Overrideprotected void onStop() {// TODO 自动生成的方法存根mNotificationManager.cancel(123);super.onStop();}}

notification使用步骤:

1.建立NotificationManager对象

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

a.建立PendingIntent对象

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

3.触发提醒



0 0