ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

9.基本控件 Notification

2021-09-04 22:03:05  阅读:217  来源: 互联网

标签:基本 控件 NotificationManager Notification app manager 通知 import android


 

 Notifaction常用的方法

必须要设置前三个,不设置不好用

 

 注意

 

 

package com.example.sixnotification;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    NotificationManager manager;
    Notification notification;
    private String channelId = "musichhhh";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //获得通知管理器这个对象
        manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        //Android8.0以上的适配
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            //创建通知渠道 这三个参数是必须的  现在我们设置这个通知级别是高的
            NotificationChannel channel = new NotificationChannel(channelId,"音乐消息",NotificationManager.IMPORTANCE_HIGH);
            //创建通知渠道的通知管理器
            NotificationManager manager1 = getSystemService(NotificationManager.class);
            //将通知渠道交给管理器
            manager1.createNotificationChannel(channel);

        }

        //为了setContentIntent这个功能做的activity
        //PendingIntent是对Intent的封装,但它不是立刻执行某个行为,而是满足某些条件或触发某些事件后才执行指定的行为
        Intent intent = new Intent(this, MessageActivity.class);
        PendingIntent activity = PendingIntent.getActivity(this, 250, intent, 0);

        //通知管理
     notification = new NotificationCompat.Builder(this,channelId)
                .setContentTitle("官方通知")
                .setContentText("你的绿钻已到期")
                .setSmallIcon(R.drawable.ic_audiotrack_black_24dp)
                .setContentIntent(activity)
                .build();

    }

    public void sentMessage(View view) {
        manager.notify(250,notification);
    }

    public void cancelSentButton(View view) {
        manager.cancel(250);  //注意昂,这个id要和sentMessage的id一致
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <Button
        android:id="@+id/sentButton"
        android:text="发送通知"
        android:onClick="sentMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <Button
        android:id="@+id/cancelSentButton"
        android:text="取消发送"
        android:onClick="cancelSentButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

 

标签:基本,控件,NotificationManager,Notification,app,manager,通知,import,android
来源: https://www.cnblogs.com/tuyaojiao/p/15227927.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有