android之Notification

Notification为android中的通知,是在android桌面上方的状态栏中显示的提示信息,显示时不会影响用户的操作,同时Notification提供了各种不同的风格,可以显示文字、图像或按钮,只要用户不清楚Notificaion,Notification将永久的保留在手机的状态栏中。Notification中常用的几个对象如下:

NotificationManager 用于管理Notification,是用于处理系统通知的服务

Notification属于通知,可以设置其图标、标题以及内容,也可以自定义Notification 的UI。

PendingIntent名为预处理意图,用于处理点击通知后的跳转对象的封装。下面我们将用实际的代码来讲解一下:

NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

用于获取系统通知服务,实例代码如下:

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{

       privatestatic final int NOTIFICAITON_ID = 0;

       privateButton btnSend;

       privateNotificationManager notificationManager;

       @Override

       protectedvoid onCreate(Bundle savedInstanceState) {

              super.onCreate(savedInstanceState);

              setContentView(R.layout.activity_main);

              this.btnSend= (Button) findViewById(R.id.btnSend);

              this.btnSend.setOnClickListener(clickListener);

              this.notificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

       }

      

       OnClickListenerclickListener = new OnClickListener() {

 

              @Override

              publicvoid onClick(View arg0) {

                     Intentintent = new Intent(MainActivity.this, PendingIntentActivity.class);

                     PendingIntentpendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);

                     Notificationnotification = new Notification(R.drawable.ic_launcher, "通知。。。。", System.currentTimeMillis());

                     notification.setLatestEventInfo(MainActivity.this,"标题", "详细内容。。。。", pendingIntent);

                     notificationManager.notify(NOTIFICAITON_ID,notification);

              }

       };

}

以上纯属个人笔记,以方便以后查看,如果各位大牛们有何指点,还请赐教!多交流!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值