状态栏中的通知

来源:互联网 发布:淘宝假鞋店铺 编辑:程序博客网 时间:2024/05/29 15:32

package com.example.firsst;

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

public class MainActivity extends Activity {

 private static int notificateID=102;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        Button btn=(Button)findViewById(R.id.del);
        btn.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    notification();
   }
  });
       
        Button bt=(Button)findViewById(R.id.cancle);
        bt.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    notificationCancel();
   }
  });
    }
   //发送通知
   public  void notification()
   {
    NotificationManager noManager=(NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
    //创建状态栏通知
    Notification notification=new Notification();
    //给状态栏通知设置各种属性
    notification.icon=R.drawable.aa;
    //通知时在状态栏显示的内容
    notification.tickerText="注意了,我被扔到状态栏了";
   
    //设置为当前时间
    notification.when=System.currentTimeMillis();
    //通知的默认参数 DEFAULT_SOUND, DEFAULT_VIBRATE, DEFAULT_LIGHTS.   
    //如果要全部采用默认值, 用 DEFAULT_ALL.  
    //此处采用默认声音  
    notification.defaults=Notification.DEFAULT_SOUND;
   
    Intent intent=new Intent(this,Other.class);
    PendingIntent pIntent=PendingIntent.getActivity(this, 0, intent,0);
    //setLatestEventInfo第二个参数 :下拉状态栏时显示的消息标题 expanded message title  
    //第三个参数:下拉状态栏时显示的消息内容 expanded message text  
    //第四个参数:点击该通知时执行页面跳转
    notification.setLatestEventInfo(this, "测试notification", "测试一下哦", pIntent);
    //发出状态栏通知
    noManager.notify(notificateID,notification);
   }
    //清除通知
   public void notificationCancel()
   {
    NotificationManager noManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    noManager.cancel(notificateID);
   
   }
   
    
}

01点击第一个图片中的"显示notification"按钮就会显示第二张图片的通知

02


原创粉丝点击