Notification

来源:互联网 发布:淘宝图片侵权赔偿标准 编辑:程序博客网 时间:2024/05/22 12:39

Notification 通知栏通知

Notification是显示手机状态栏的文本消息的提示。

如何实现:

    1 获取NotificationManager    NotificationManager mn =  (NotificationManager) getSystemService(NOTIFICATION_SERVICE);    2 显示通知栏 notify(id,notification);    3 取消通知栏 cancle(id);    4 构造Notification并设置显示的内容    5 通知栏通知可以设置声音提示,指示灯。以及震动效果;

  NotificationManager mn =  (NotificationManager) getSystemService(NOTIFICATION_SERVICE);//设置点击通知要跳转的意图    Intent intent = new intent(this,MainActivity.class);    PendingIntent pintent = PendingIntent.getActivity(this,0,intent);    Builder builder =  new Notification.Builder(this);    builder.setSmallIcaon(R.drawable.ic_launch);//设置图片    builder.setTicker(”hello“);    builder.setWhen(System.currentTimeMills());//设置时间    builder.setContentTitle("通知栏通知");    builder.setContentText("我是来自NotificationDemo");//设置文本内容    builder.setContentIntent(pintent);//点击后的意图    builder.setDefaults(Notification.DEFAULT_SOUND);设置声音    builder.setDefaults(Notification.DEFAULT_LIGHTS);//设置指示灯    builder.setDefaults(Notification.DEFAULT_VIBRATE);设置震动     builder.setDefaults(Notification.DEFAULT_ALL);指示灯和震动 需要相应的权限;;                                builder.getNotification();//4.1以下要使用    Notification notification = builder.build();//4.1以上    mn.notify(id,notification);//发送通知。id是当前的id号,随便自己定义

    mn.cancel(id);//取消通知




小demo

package com.itheima.notification;import android.annotation.SuppressLint;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;import android.graphics.BitmapFactory;import android.net.Uri;import android.os.Bundle;import android.view.View;public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }    public void click(View view){        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);        //旧版本的        Notification notification = new Notification(R.drawable.notification, "我是一个通知", System.currentTimeMillis());        notification.flags = Notification.FLAG_AUTO_CANCEL;        //隐士意图        Intent intent = new Intent();        intent.setAction(Intent.ACTION_CALL);        intent.setData(Uri.parse("tel:110"));        //处理即将发生的事情        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);        notification.setLatestEventInfo(this, "我是标题", "我是内容", contentIntent);        nm.notify(0, notification);    }    /**     * 新版本的notification     * @param view     */    @SuppressLint("NewApi")    public void click2(View view){         Notification noti = new Notification.Builder(this)         .setContentTitle("我是标题")         .setContentText("我是内容")         .setSmallIcon(R.drawable.notification)         .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))         .build();         NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);         nm.notify(0, noti);    }}
0 0
原创粉丝点击