首页 > 其他分享 >Android 《Notification》

Android 《Notification》

时间:2024-03-11 16:33:20浏览次数:16  
标签:IMPORTANCE Notification app manager static notification Android public

代码

package com.xian.app.broadcast;

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

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private NotificationManager manager;
    private Notification notification;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn_send_notification).setOnClickListener(this);
        findViewById(R.id.btn_close_notification).setOnClickListener(this);

        initNotify();


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
			case R.id.btn_send_notification:
                manager.notify(1,notification); //ID要一致
                break;
            case R.id.btn_close_notification: // ID 要一致
                manager.cancel(1);
                break;
        }
    }

    private void SendNotification() {
        manager.notify(1,notification);
    }

    /***
     * 发送Notification
     */
    private static final String channelId = "xian";
    private void initNotify() {
        manager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
        //兼容性处理
        if(Build.VERSION.SDK_INT >=Build.VERSION_CODES.O){
            /**
             *     public static final int IMPORTANCE_NONE = 0;
             *       关闭通知
             *     public static final int IMPORTANCE_MIN = 1;
             *       开启通知,不会弹出,没有提示音,状态栏中没显示
             *    public static final int IMPORTANCE_LOW = 2;
             *      开启通知,不会弹出,没有提示音,状态栏中有显示
             *    public static final int IMPORTANCE_DEFAULT = 3;
             *       开启通知,不会弹出,发出提示音,状态栏中有显示
             *    public static final int IMPORTANCE_HIGH = 4;
             *        开启通知,会弹出,发出提示音,状态栏中有显示
             */
            NotificationChannel channel = new NotificationChannel(channelId, "测试通知", NotificationManager.IMPORTANCE_HIGH);
            manager.createNotificationChannel(channel);

        }
		Intent intent = new Intent(this,ReturnDeskActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        //FLAG_ONE_SHOT, FLAG_NO_CREATE, FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT
        PendingIntent pendingIntent = PendingIntent.getActivity(this,10086,intent,0);
        notification = new NotificationCompat.Builder(this, channelId)
                .setContentTitle("你有一个新的通知")
                .setContentText("银行卡新收入500w人民币")
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setColor(Color.parseColor("#ff0000"))
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher_foreground)) //设置大图标
                .setContentIntent(pendingIntent) //点击通知后跳转PendingIntent
                .setAutoCancel(true) //点击通知后是否自动取消
                .build();
    }
}

效果图

image

标签:IMPORTANCE,Notification,app,manager,static,notification,Android,public
From: https://www.cnblogs.com/paylove/p/18066414

相关文章

  • Android 《回到桌面与切换到任务列表》
    App不但能监测手机屏幕的方向变更,还能获知回到桌面的事件,连打开任务列表的事件也能实时得知。回到桌面与打开任务列表都由按键触发,例如按下主页键会回到桌面,按下任务键会打开任务列表。虽然这两个操作看起来属于按键事件,但系统并未提供相应的按键处理方法,而是通过广播发出事件信......
  • Android 《屏幕切换》
    默认处理机制除了系统广播之外,App所处的环境也会影响运行,比如手机有竖屏与横屏两种模式,竖屏时水平方向较短而垂直方向较长,横屏时水平方向较长而垂直方向较短。两种屏幕方向不但造成App界面的展示差异,而且竖屏和横屏切换之际,甚至会打乱App的生命周期。无需重启Activity横竖屏......
  • Android 获取设备的CPU型号和设备型号
    原文:Android获取设备的CPU型号和设备型号-Stars-One的杂货小窝之前整的项目的总结信息,可能不太全,凑合着用吧,代码在最下面一节CPU型号数据华为:ro.mediatek.platformvivo:ro.vivo.product.platformoppo:ro.board.platform或ro.product.board三星:ro.board.pla......
  • Android 获取设备的亮度百分比
    一般的屏幕亮度都是0-255,而小米手机的高版本不一样为了使亮度调节更加细腻,MIUI对原生亮度级别进行了扩展,由原有的255级调整根据不同屏幕分别支持255/1023/2047/4095级。开发者在进行亮度调整时需要先去确认机型亮度的最大值和最小值,然后进行比例调整。计算屏幕亮度fung......
  • Android 《系统广播》
    一.接收系统的分钟广播TimeReceiver.javapackagecom.xian.app.broadcast.receiver;importandroid.content.BroadcastReceiver;importandroid.content.Context;importandroid.content.Intent;importandroid.util.Log;importjava.util.Date;publicclassTimeReceiv......
  • Android Studio 2021 Gradle 下载失败时,解决方法
    我是mac,Androidstudio2021,创建一个android项目,gradle下载失败,折腾了半天,以下是解决方法:1.出现的错误: 无法下载gradle,2.解决方法使用腾讯的镜像,这里的 地址为:https://mirrors.cloud.tencent.com/gradle/gradle-8.4-all.zip后面的版本可以按自己的需求随意切换然......
  • 【测试】Android Studio 相关下载及参数
    AndroidStudio仅限命令行工具ZipfilesWindows:commandlinetools-win-11076708_latest.zip(153.6MB)SHA-256Checksums4d6931209eebb1bfb7c7e8b240a6a3cb3ab24479ea294f3539429574b1eec862commandlinetools-win-11076708_latest.zipAndroidEmulatorAndroid模拟器......
  • Delphi11安装Android安卓SDK
    由于Google的AndroidSDK不再允许其他程序分发。Delphi11安装后,默认是没有Android安卓SDK的,在默认安装路径的情况下,在C:\Users\Public\Documents\Embarcadero\Studio\22.0\CatalogRepository\AndroidSDK-2525-22.0.46141.0937\目录下面,只有最基本的安卓SDK的命令行工具,我们可以使......
  • 区块链钱包-android篇
    ​1:使用ProtocolBuffers 首先根目录gradle中添加依赖:classpath"com.google.protobuf:protobuf-gradle-plugin:0.8.3"然后项目文件中添加plugin,添加依赖包:applyplugin:'com.google.protobuf'protobuf{protoc{artifact='com.google.protobuf:protoc:3.6.1......
  • Android 二维码相关(二)
    Android二维码相关(二)本篇文章继续讲述下如何使用zxing解析二维码图片,获取内容.1:创建RGBLuminanceSource对象.首先获取二维码图片的bitmap对象.Bitmapbitmap=BitmapFactory.decodeResource(getResources(),R.mipmap.test);根据getPixels()获取位图指定区域的像素颜......