六、Notification(通知)
一:
建一个NotificationManager
NotificationManager类是一个通知管理器类,这个对象是由系统维护的服务,是以单例模式的方式获得,所以一般并不直接实例化这个对象。在Activity中,可以使用Activity.getSystemService(String)方法获取NotificationManager对象, Activity-etSystemService(String)方法可以通过Android系统级服务的句柄,返回对应的对象。在这里需要返回NotificationManager,所以直接传递Context.NOTIFICATION_SERVICE即可。
使用Builder构造器来创建Notification对象
使用NotificationCompat类的Builder构造器来创建Notification对象,可以保证程序在所有的版本上都能正常工作。Android8.0新增了通知渠道这个概念,如果没有设置,则通知无法在Android8.0的机器上显示
二:
通知渠道: Android 8.0引入了通知渠道,其允许您为要显示的每种通知类型创建用户可自定义的渠道。
通知重要程度设置, NotificationManager类中
IMPORTANCE_NONE 关闭通知
IMPORTANCE_MIN 开启通知,不会弹出,但没有提示音,状态栏中无显示
IMPORTANCE_LOW 开启通知,不会弹出,不发出提示音,状态栏中显示
IMPORTANCE_DEFAULT 开启通知,不会弹出,发出提示音,状态栏中显示
IMPORTANCE_HIGH 开启通知,会弹出,发出提示音,状态栏中显示
三:常见方法说明(红色为必写项)
1. setContentTitle(String string)设置标题
2. setContentText(String string)设置文本内容
3. setSmallicon(int icon)设置小图标
4. setLargelcon(Bitmşp icon)设置通知的大图标
5. setColor(int argb)设置小图标的颜色
6. setContentIntent(Pendinglntent intent)设置点击通知后的跳转意图
7. setAutoCancel(boolean boolean)设置点击通知后自动清除通知
8. setWhen(long when)设置通知被创建的时间
四:源码显示
ui源码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?xml version= "1.0" encoding= "utf-8" ?> <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:orientation= "vertical" > <Button android:text= "发送通知" android:onClick= "sendNotification" android:layout_width= "wrap_content" android:layout_height= "wrap_content" /> <Button android:text= "取消通知" android:onClick= "celNotification" android:layout_width= "wrap_content" android:layout_height= "wrap_content" /> </LinearLayout> |
后台源码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | package com.example.myapplication; 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); //获取NotifactionManager对象 manager= (NotificationManager)getSystemService(NOTIFICATION_SERVICE); //进行版本判断 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){ NotificationChannel Channel= new NotificationChannel( "leo" , "测试通知" , 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 , "leo" ) .setContentTitle( "官方通知" ) //设置通知的标题 .setContentText( "世界那么大,想去看看" ) //设置通知的内容 .setSmallIcon(R.drawable.ic_baseline_person_24) //设置通知的小图标 .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.mins)) //设置通知的大图标(图片) .setColor(Color.parseColor( "#ffff0000" )) //设置小图标的颜色 .setContentIntent(pendingIntent) //点击通知跳转 .setAutoCancel( true ) //关闭通知 .build(); } public void sendNotification(View view) { manager.notify( 1 ,notification); } public void celNotification(View view) { } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | package com.example.myapplication; import android.app.Activity; import android.os.Bundle; import android.util.Log; import androidx.annotation.Nullable; public class NotificationActivity extends Activity { @Override public void onCreate( @Nullable Bundle savedInstanceState){ super .onCreate(savedInstanceState); Log.e( "leo" , "onCreate:Notification:通知跳转 " ); } } |
五:效果图
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」