
notificationmanager类是一个通知管理类,这个对象是由系统维护的服务,是以单例模式的方式获得,所以一般并不直接实例化这个对象。在Activity中,可以使用Activity.getSystemService(String)方法获取notificationmanager对象,Activity.getSystemService(String)方法可以通过AndroID系统级服务的句柄,返回对应的对象。在这里需要返回notificationmanager,所以直接传递Context.NOTIFICATION_SERVICE即可。
使用Builder构造器来创建Notification对象使用NotificationCompat类的Builder构造器来创建Notification对象,可以保证程序在所有的版本上都能正常工作。Android8.0新增了通知渠道这个概念,如果没有设置,则通知无法在Android8.0的机器上显示
NotificationChannel通知渠道:AndroID 8.0 引入了通知渠道,其允许您为要显示的每种通知类型创建用户可自定的渠道。
通知重要设置,notificationmanager类中
importANCE_NONE 关闭通知importANCE_MIN 开启通知,不会d出,但没有提示音,状态栏中无显示IMPOPTANCE_LOW 开启通知,不会d出,不发出提示音,状态栏中显示importTANCE_DEFAulT 开启通知,不会d出,发出提示音,状态栏中显示importANCE_HIGH 开启通知,会d出,发出提示音,状态栏中显示常用方法说明setContentTitle(String string) 设置标题setContentText(String string) 设置文本内容setSmallicon(int icon) 设置小图标setLargeicon(Bitmap icon) 设置通知的大图标setcolor(int argb) 设置小图标的颜色setContentIntent(PendingIntent intent) 设置点击通知后的跳转意图setautoCancel(boolean boolean) 设置点击通知后自动清除通知setWhen(long when) 设置通知被创建的时间package com.example.mynotification;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.graphics.BitmapFactory;import androID.graphics.color;import androID.os.Build;import androID.os.Bundle;import androID.vIEw.VIEw;public class MainActivity extends AppCompatActivity { private notificationmanager manager; private Notification notification; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); manager = (notificationmanager) getSystemService(NOTIFICATION_SERVICE); if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){ NotificationChannel channel =new NotificationChannel("通知","测试通知", notificationmanager.importANCE_HIGH); manager.createNotificationChannel(channel); } Intent intent = new Intent(this,NotificationActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0); notification =new NotificationCompat.Builder(this,"通知") .setContentTitle("官方通知") .setContentText("你今天早睡了吗?") .setSmallicon(R.drawable.ic_baseline_access_alarms_24) .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.download)) .setcolor(color.parsecolor("#ff0000")) .setContentIntent(pendingIntent) .setautoCancel(true) .build(); //通知渠道 } public voID sendNotification(VIEw vIEw) { manager.notify(1,notification); } public voID cacleNotification(VIEw vIEw) { manager.cancel(1); }}<?xml version="1.0" enCoding="utf-8"?><linearLayout androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" xmlns:androID="http://schemas.androID.com/apk/res/androID"> <button androID:onClick="sendNotification" androID:text="发出通知" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content"/> <button androID:onClick="cacleNotification" androID:text="关闭通知" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content"/></linearLayout>package com.example.mynotification;import androID.app.Activity;import androID.os.Bundle;import androID.util.Log;import androIDx.annotation.Nullable;public class NotificationActivity extends Activity { @OverrIDe protected voID onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.e("通知","onCreat:进入NotificationActivity"); }} 总结 以上是内存溢出为你收集整理的Android(安卓)开发-控件-Notification全部内容,希望文章能够帮你解决Android(安卓)开发-控件-Notification所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)