通知(Notification)入门

通知(Notification)的简单实现

先贴出代码,不理解的代码基本都有注释。
首先贴出布局文件activity_main.xml的代码,很简单就是一个触发按钮,点击按钮显示通知。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    >
<Button
    android:id="@+id/send_notice"
    android:text="Send notice"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

再是逻辑代码MainActivity.java

package com.example.notification;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity implements View.OnClickListener{
    private Button sendNotice;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sendNotice = (Button) findViewById(R.id.send_notice);
        sendNotice.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.send_notice:
                NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);//创建一个NotificationManager对通知进行管理
                Notification.Builder builder = new Notification.Builder(this);
                builder.setContentInfo("补充内容");
                builder.setContentText("主内容区");
                builder.setContentTitle("通知标题");
                builder.setSmallIcon(R.mipmap.ic_launcher);
                builder.setTicker("新消息");
                builder.setWhen(System.currentTimeMillis());//指定通知被创建的时间
                Notification notification = builder.build();
                manager.notify(1, notification);//第一个参数设置id
                break;
            default:
                break;
        }
    }
}

值得一提的是以下方法在sdk22可以使用。但sdk23中已经不能使用
Notification notification = new Notification(R.mipmap.ic_launcher,”this is ticker text”,System.currentTimeMillis());//创建的这个对象用于存储通知所需的各种信息。第一个参数指定通知的图标,第二个指定通知的内容,第三个指定通知被创建的时间
notification.setLatestEventInfo(this,”thisis content title”,”this is content text”,null);//对布局进行设定第一个参数context上下文第二个参数指定通知标题的内容,第三个参数用于指定通知的正文内容

那么sdk23使用的是什么方法来实现的呢?
答案是使用 Notification.Builder

Notification.Builder builder = new Notification.Builder(this);
builder.setContentInfo(“补充内容”);
builder.setContentText(“主内容区”);
builder.setContentTitle(“通知标题”);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setTicker(“新消息”);

这只是一个简单的demo实现通知功能,还没有加入跳转等复杂功能,在后续的章节中为大家继续介绍。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值