短视频带货源码,android 自定义常驻通知栏
短视频带货源码,android 自定义常驻通知栏
1、自定义xml 如下:
1 | <br><?xml version= "1.0" encoding= "utf-8" ?><br><LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" <br> xmlns:tools= "http://schemas.android.com/tools" <br> android:layout_width= "match_parent" <br> android:layout_height= "wrap_content" <br> android:orientation= "horizontal" <br> ><br> <br> <ImageView<br> android:layout_width= "wrap_content" <br> android:layout_height= "wrap_content" <br> android:layout_gravity= "center" <br> android:src= "@mipmap/ic_launcher" /><br> <br> <TextView<br> android:id= "@+id/tvTitle" <br> style= "@style/TextAppearance.Compat.Notification.Title" <br> android:layout_width= "0dp" <br> android:layout_height= "wrap_content" <br> android:layout_gravity= "center" <br> android:layout_weight= "1" <br> android:ellipsize= "end" <br> android:maxLength= "15" <br> android:paddingLeft= "5dp" <br> android:paddingRight= "5dp" <br> android:singleLine= "true" <br> android:text= "点击关闭音频" /><br> <br> <ImageView<br> android:id= "@+id/ivStop" <br> android:layout_width= "wrap_content" <br> android:layout_height= "match_parent" <br> android:src= "@drawable/ic_baseline_stop_circle_24" /><br></LinearLayout><br> <br> |
注意:style="@style/TextAppearance.Compat.Notification.Title" 这是google官方的建议、一般加到我们Textview中。
2、加入权限
1 | <br><uses-permission android:name= "android.permission.FOREGROUND_SERVICE" /> |
部分设备需要加入 悬浮窗权限
1 | <br><uses-permission android:name= "android.permission.SYSTEM_ALERT_WINDOW" /> |
3、详细代码
1 | <br> public void createMusicNotification(Context context) {<br> <br> NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);<br> <br> //android 8.0的判断、需要加入NotificationChannel<br> if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {<br> NotificationChannel channel = new NotificationChannel("qiqile", "齐齐乐渠道",<br> NotificationManager.IMPORTANCE_DEFAULT);<br> notificationManager.createNotificationChannel(channel);<br> }<br> <br> NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "qiqile");<br> //自定义布局必须加上、否则布局会有显示问题、可以自己try try <br> builder.setSmallIcon(R.mipmap.ic_launcher);<br> builder.setOngoing(true);//代表是常驻的,主要是配合服务<br> <br> RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.notify_custom);<br> //自定义点击事件、会在Service. onStartCommand中回调<br> Intent stopIntent = new Intent(context, MediaService.class);<br> stopIntent.setAction(STOP_PLAY_SERVICE);<br> <br> PendingIntent startOrPauseP = PendingIntent.getService(context, MediaService.RELEASE, stopIntent, 0);<br> remoteViews.setOnClickPendingIntent(R.id.ivStop, startOrPauseP);<br> <br> builder.setContent(remoteViews);<br> Notification notification = builder.build();<br> //0x11 为通知id 自定义可<br> startForeground(0x11, notification);<br> } |
以上就是 短视频带货源码,android 自定义常驻通知栏,更多内容欢迎关注之后的文章
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现