美文网首页
Android Notification

Android Notification

作者: 百里漫步 | 来源:发表于2017-10-21 17:25 被阅读0次

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    
    tools:context=".MainActivity" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="发送通知"
        android:id="@+id/btn_send" />
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="取消通知"
        android:id="@+id/btn_cancle" />
    </LinearLayout>
</RelativeLayout>

MainActivity.java

package com.imooc.notificationdemo;

import android.app.Activity;
import android.app.Notification;
import android.app.Notification.Builder;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity implements OnClickListener{
    NotificationManager manager;//通知控制类
    int notification_ID;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        findViewById(R.id.btn_send).setOnClickListener(this);
        findViewById(R.id.btn_cancle).setOnClickListener(this);
        
    }
    
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.btn_send:
            sendNotification();
            break;
        case R.id.btn_cancle:
            manager.cancel(notification_ID);
            break;
        }
    }
    /**
     * 构造notification并发送到通知栏
     */
    private void sendNotification(){
        Intent intent = new Intent(this,MainActivity.class);
        PendingIntent pintent = PendingIntent.getActivity(this, 0, intent, 0);
        Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.drawable.ic_launcher);//设置图标
        builder.setTicker("hello");//手机状态栏的提示;
        builder.setWhen(System.currentTimeMillis());//设置时间
        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);//设置震动
        Notification notification = builder.build();//4.1以上
        //builder.getNotification();
        manager.notify(notification_ID, notification);
    }
}

演示效果:


notification.gif

相关文章

  • 通知

    Android中Notification 提示对话框,notification 概述 notification,俗...

  • Android 8.0 Notification

    Android 8.0 通知适配: Android Api 26 Notification Builder 构建...

  • Android UI - Notification

    Notification Notification,通知。Android 里的通知设置无非下面几步:创建一个 No...

  • Notification (通知)

    Notification (通知) Notification是android中的一个API对象,此对象会借助And...

  • 通知栏错误

    android.app.RemoteServiceException: Bad notification post...

  • Android代码之路:Notification消息通知

    Notification简介 Notification是Android中用于提示用户消息的小工具,它显示在屏幕的顶...

  • Notification-基础梳理

    https://www.cnblogs.com/travellife/p/Android-Notification...

  • Android -Notification

    1. 创建notification 通过Builder模式创建Notification.Builder实例,有了b...

  • Android Notification

    应用层调用过程,先生成一个notification对象,然后通过调用NotificationManager的nor...

  • Android Notification

    废话 经常熬夜有三大害处:第一,记忆力越来越差;第二,数学水平下降;第四,记忆力越来越差。 是不是觉得这个段子很熟...

网友评论

      本文标题:Android Notification

      本文链接:https://www.haomeiwen.com/subject/jsaiuxtx.html