首页 > 其他分享 >android notification

android notification

时间:2022-10-06 14:56:12浏览次数:47  
标签:NotificationManager notificationManager notification new android channel

    //创建普通通知
     String channelId = "测试渠道";
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            Notification notification = new Notification.Builder(getApplicationContext(),channelId)
                    .setContentTitle("测试通知标题")
                    .setContentText("测试通知文本")
                    .setWhen(System.currentTimeMillis())
                    .setSmallIcon(R.drawable.chips)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.chips))
                    .build();
            NotificationManager notificationManager = (NotificationManager)getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
            NotificationChannel channel = new NotificationChannel(channelId,"测试渠道名称", NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
            notificationManager.notify((int)Math.random(), notification);
        }

 //创建自定义布局的通知:

MainActivity:

    private static final String CHANNEL_ID = "channelID";

    private void customNotification() {
        // Get the layouts to use in the custom notification
        RemoteViews rv = new RemoteViews(getPackageName(), R.layout.custom_notification);
        // Apply the layouts to the notification
        Notification customNotification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setSmallIcon(R.drawable.chips)
                .setCustomContentView(rv)
                .build();
        NotificationManager notificationManager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
        NotificationChannel channel = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            channel = new NotificationChannel(CHANNEL_ID, "测试渠道名称", NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }

        // adding action to left button(添加点击事件)
        Intent rightIntent = new Intent(this, NotificationIntentService.class);
        rightIntent.setAction("left");
        rv.setOnClickPendingIntent(R.id.button2, PendingIntent.getService(this, 1, rightIntent, PendingIntent.FLAG_UPDATE_CURRENT));

        notificationManager.notify((int) Math.random(), customNotification);
    }

布局:(注意:不可以使用constraint布局)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    >
    <Button
        android:layout_marginRight="5dp"
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="show"
        />

    <Button
        android:layout_marginLeft="5dp"
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="hide"
        />
</LinearLayout>

AndroidManifest.xml(注册service)

<service android:name=".NotificationIntentService"></service>

获取点击事件:

public class NotificationIntentService extends IntentService {
    public NotificationIntentService() {
        super("notificationIntentService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        switch (intent.getAction()){
            case "left":
                android.os.Handler leftHandler = new android.os.Handler(Looper.getMainLooper());
                leftHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getBaseContext(),
                                "You clicked the left button", Toast.LENGTH_LONG).show();
                    }
                });
                break;
        }
    }
}

 

标签:NotificationManager,notificationManager,notification,new,android,channel
From: https://www.cnblogs.com/laremehpe/p/16756666.html

相关文章

  • Android 小项目之--解析如何获取SDCard 内存
    1、讲述Environment类。2、讲述StatFs类。3、完整例子读取SDCard内存1、讲述Environment类Environment是一个提供访问环境变量的类。Environment 包含常量:​​MED......
  • android之定时器AlarmManager .
     果图:      当我们点击定时时,会弹出一个时间选择器,选定好时间之后,系统便可以进行定时了。注意,这里可不是会真的响铃,我们在定时的任务里并没有......
  • Android中的JSON详细总结
    1、JSON(JavaScriptObjectNotation)定义:一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性。业内主流技术为其提供了完整的解决方案(有点类似于正则表达式,获得......
  • Android开发之PreferenceActivity .
    今天我们来讲PreferenceActivity的使用。我们先来认识一下它,看看它长什么样?呵呵,截图如下:看到没?这就是PreferenceActivity.看起来蛮眼熟的,在哪见过。呵呵,对,在我们得模拟器“......
  • Android软件开发之盘点自定义View界面大合集
    今天我用自己写的一个Demo和大家详细介绍一个Android中自定义View中的使用与绘制技巧。1.自定义view绘制字符串            相信在实际开发过程中必然很多......
  • Android 设置主题实现点击波纹效果
    开头先说说大家都知道的MaterialDesign。这里推荐​​大苞米​​的系列博客,介绍的很全面。MaterialDesign:MaterialDesign是Google推出的一个全新的设计语言,它的特点就是......
  • Android中 android:gravity 和 android:layout_gravity的区别
    在配置xml布局时,经常用到 android:gravity 和 android:layout_gravity这两个属性,这里记录一下他们的区别。1.android:gravity android:gravity常用于控制view的内部......
  • Action Bar使用方法 - Android活动栏(一)
     ActionBar主要功能包含: 1.显示选项菜单 2.提供标签页的切换方式的导航功能,可以切换多个fragment. 3. 提供下拉的导航条目. 4.提供交互式活动视图代替选......
  • Android如何判断应用程序是安装在手机内存中还是SD卡
    /***判断包名所对应的应用是否安装在SD卡上*@parampackageName*@return,trueifinstallonSDcard*/publicstaticbooleanisInstallOnSDCard(Stringpac......
  • Android 创建与解析XML(一)—— 概述
    Android是最常用的智能手机平台,XML是数据交换的标准媒介,Android中可以使用标准的XML生成器、解析器、转换器API,对XML进行解析和转换。XML,相关有DOM、SAX、JDOM、DOM......