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" tools:context=".MainActivity"> <!--Toast按键布局--> <button androID:ID="@+ID/createNotify" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_gravity="center" androID:text="创建消息" /> <button androID:ID="@+ID/deleteNotify" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_gravity="center" androID:text="删除消息" /></linearLayout>
MainActivity
package com.example.toastnotificationdemo;import androID.app.Notification;import androID.app.NotificationChannel;import androID.app.notificationmanager;import androID.graphics.BitmapFactory;import androID.os.Build;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.Toast;import androIDx.appcompat.app.AppCompatActivity;import androIDx.core.app.NotificationCompat;public class MainActivity extends AppCompatActivity { private static final int NOTIFICATION_ID = 1; String ID = "channelID"; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); //获取控件 button createNotify = findVIEwByID(R.ID.createNotify); button deleteNotify = findVIEwByID(R.ID.deleteNotify); createNotify.setonClickListener(new VIEw.OnClickListener() { public voID onClick(VIEw v) { Toast.makeText(MainActivity.this, "创建消息", Toast.LENGTH_SHORT).show(); Notification notification; notificationmanager manager = (notificationmanager) getSystemService(NOTIFICATION_SERVICE); //API>=26 (androID 8.0) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { String name = "channelname"; NotificationChannel channel = new NotificationChannel(ID, name, notificationmanager.importANCE_HIGH); manager.createNotificationChannel(channel); notification = new NotificationCompat.Builder(MainActivity.this, ID) .setChannelID(ID)//通道ID .setContentTitle("Notification的标题测试")//设置Notification在状态栏中的标题 .setContentText("Notification的内容测试")//设置Notification在状态栏中的内容 .setWhen(System.currentTimeMillis())//设置Notification启动时间 .setSmallicon(R.drawable.sms)//设置Notification的在状态栏中的图标 .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))//消息大图标 .build(); } else { notification = new NotificationCompat.Builder(MainActivity.this, ID) .setContentTitle("Notification的标题测试")//设置Notification在状态栏中的标题 .setContentText("Notification的内容测试")//设置Notification在状态栏中的内容 .setWhen(System.currentTimeMillis())//设置Notification启动时间 .setSmallicon(R.drawable.sms)//设置Notification的在状态栏中的图标 .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) .build(); } // 发送Notification消息 manager.notify(1, notification); } }); deleteNotify.setonClickListener(new VIEw.OnClickListener() { public voID onClick(VIEw v) { Toast.makeText(MainActivity.this, "删除消息", Toast.LENGTH_SHORT).show(); //获取系统的Notification服务 notificationmanager notificationmanager = (notificationmanager) getSystemService(NOTIFICATION_SERVICE); //取消通知 notificationmanager.cancel(NOTIFICATION_ID); } }); }}
总结 以上是内存溢出为你收集整理的Android 消息通知栏用法全部内容,希望文章能够帮你解决Android 消息通知栏用法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)