当前位置: 首页 > news >正文

建站时候源码有验证怎么办苏州优化平台

建站时候源码有验证怎么办,苏州优化平台,品牌推广公司是做什么的,长安公司网站设计设置服务为前台服务。前台服务会在状态栏显示一个通知。通知界面与服务进行关联。 一、什么是通知#xff1f; Notification通知是在移动应用APP提供给用户的消息提示#xff0c;是在移动系统的通知栏中显示。当移动应用不在运行时或者在后台状态下#xff0c;通过发布通知…设置服务为前台服务。前台服务会在状态栏显示一个通知。通知界面与服务进行关联。 一、什么是通知 Notification通知是在移动应用APP提供给用户的消息提示是在移动系统的通知栏中显示。当移动应用不在运行时或者在后台状态下通过发布通知给用户用户可以浏览通知的提示信息或者对通知进行某些操作。 图1 通知显示示例 如上图所示 ①小图标为必要图标通过setSmallIcon()设置 ②应用名称由系统提供。 ③时间戳由系统提供可以通过setWhen()进行替换或使用setShowWhen(false)将其隐藏。 ④标题可选内容通过setContentTitle()设置。 ⑤文本可选内容通过setContentText()设置。 ⑥大图标可选图标通常仅用于联系人照片通过setLargeIcon设置。 ⑦样式通过样式设置大图片。 1.在AndroidManifest.xml中配置发布通知的权限 注意从Android13开始发布通知需要配置android.permission.POST_NOTIFICATIONS许可 !-- 设置发布通知许可 -- uses-permission android:nameandroid.permission.POST_NOTIFICATIONS /2.请求发布通知权限 // 请求发布通知权限 if(Build.VERSION.SDK_INTBuild.VERSION_CODES.TIRAMISU) { ActivityCompat.requestPermissions( this, arrayOf(android.Manifest.permission.POST_NOTIFICATIONS), 0) } 3. 创建通知渠道和创建通知 1创建通知渠道 从 Android 8.0API 级别 26开始所有通知都必须分配到相应的渠道。 //定义通知管理器val notificationManager:NotificationManager getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager//定义通知渠道的标识val channelId com.example.ch07//定义通知渠道的名称val channelName 移动应用开发//定义通知渠道:指定通知渠道的标识、名称和通知渠道的重要级别val channel NotificationChannel(channelId,channelName, NotificationManager.IMPORTANCE_DEFAULT)//定义通知渠道的描述信息val channelDesc 移动应用通知渠道描述//创建并配置通知渠道notificationManager.createNotificationChannel(channel)2创建通知 //创建通知val notification Notification.Builder(this,channelId) .apply{//设置通知标题setContentTitle(通知实例一)//设置通知内容setContentText(欢迎使用通知)//设置通知时间setWhen(System.currentTimeMillis())//设置通知的小图标setSmallIcon(R.mipmap.nature)//设置通知的大图标setLargeIcon(BitmapFactory.decodeResource(resources,R.mipmap.someone))//设置通知样式style Notification.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(resources,R.mipmap.honggutan))}.build()3发布通知 //创建通知标记val notificationID 1//发布通知到通知栏notificationManager.notify(notificationID,notification)4. 通知实例 接下来通过实例来说明发布通知 1定义界面MainScreen 在MainScreen中定义发布通知的按钮提供交互处理 Composable fun MainScreen(postAction:()-Unit){Box(modifier Modifier.fillMaxSize(),contentAlignment Alignment.Center){TextButton(onClick{postAction.invoke()}){Text(发布通知,fontSize 24.sp)}} }2定义主活动MainActivity class MainActivity : ComponentActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)//请求权限requestNotificationPermission()setContent {Ch07_DemoTheme {Surface(modifier Modifier.fillMaxSize(),color MaterialTheme.colorScheme.background) {MainScreen {showNotification()}}}}}private fun requestNotificationPermission(){// 检查通知权限是否已经授予 注意API 33以上版本需要检查POST_NOTIFICATIONS发布通知权限if(Build.VERSION.SDK_INT Build.VERSION_CODES.TIRAMISU) {ActivityCompat.requestPermissions(this,arrayOf(android.Manifest.permission.POST_NOTIFICATIONS),0)}}private fun showNotification(){//定义通知管理器val notificationManager:NotificationManager getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager//定义通知渠道的标识val channelId com.example.ch07//定义通知渠道的名称val channelName 移动应用开发//定义通知渠道:指定通知渠道的标识、名称和通知渠道的重要级别val channel NotificationChannel(channelId,channelName, NotificationManager.IMPORTANCE_DEFAULT)//定义通知渠道的描述信息val channelDesc 移动应用开发通知渠道描述//创建并配置通知渠道notificationManager.createNotificationChannel(channel)//创建通知val notification Notification.Builder(this,channelId).apply{//设置通知标题setContentTitle(通知实例一)//设置通知内容setContentText(欢迎使用通知)//设置通知时间setWhen(System.currentTimeMillis())//设置通知的小图标setSmallIcon(R.mipmap.nature)//设置通知的大图标setLargeIcon(BitmapFactory.decodeResource(resources,R.mipmap.someone))//设置通知样式style Notification.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(resources,R.mipmap.honggutan))}.build()//创建通知标记val notificationID 1//发布通知到通知栏,注意从Android13开始发布通知需要配置android.permission.POST_NOTIFICATIONS许可notificationManager.notify(notificationID,notification)} }运行结果如下所示 图2 运行结果 运行结果的图片来自网络如有侵权告知会主动删除。 二、前台服务 设置服务为前台服务。前台服务会在状态栏显示一个通知。通知界面与服务进行关联。从Android 14版本API Level 34开始每个前台服务需要声明合适的服务类型。也就是在启动前台服务时系统会根据服务类型检查是否满足前提条件。 表2-1 常见服务类型 前台服务类型要声明的权限运行时要求前提条件运行时请求权限请求权限调用的方法说明相机FOREGROUND_SERVICE_CAMERA请求CAMERA运行时权限在后台访问相机连接设备FOREGROUND_SERVICE_CONNECTED_DEVICECHANGE_NETWORK_STATE或CHANGE_WIFI_STATE或CHANGE_WIFI_MULTICAST_STATE或NFC或TRANSMIT_IRBLUETOOTH_CONNECT或BLUETOOTH_ADVERTISE或BLUETOOTH_SCAN或UWEB_RANGING调用UsbManager.requestPermission()与需要的蓝牙、NFC、IR、USB或网络的外部设备互动数据同步FOREGROUND_SERVICE_DATA_SYNC数据传输的操作健康FOREGROUND_SERVICE_HEALTHHIGH_SAMPLING_RATE_SENSORSBORY_SENSORS或ACTIVITY_RECOGNITION为健身类别的应用提供支持位置FOREGROUND_SERVICE_LOCATIONACESS__COARSE_LOCATION或ACESS_FINE_LOCATION为位置信息使用权的长时间运行用例媒体FOREGROUND_SERVICE_MEDIA_PLAYBACK在后台继续播放音频或视频媒体投影FOREGROUND_SERVICE_MEDIA_PROJECTION调用createScreenCaptureIntent()使用MediaProjection API将内容投影到外部麦克风FOREGROUND_SERVICE_MICROPHONERECORD_AUDIO在后台继续捕获麦克风内容致电FOREGROUND_SERVICE_PHONE_CALLMANAGE_OWN_CALLS使用COnnetionService API继续当前通话远程消息传递FOREGROUND_SERVICE_REMOTE_MESSAGING将短信从一台设备转移另外一台设备配置前台服务 启动前台服务 Context.startForeground(int,Notification) 例创建一个前台服务播放mp3文件并在状态栏中显示相应的通知。 1.创建服务 自定义服务可以控制音频的播放代码如下 class MusicService : Service() {lateinit var mediaPlayer: MediaPlayeroverride fun onCreate() {super.onCreate()mediaPlayer MediaPlayer.create(this,R.raw.song3)}override fun onBind(intent: Intent): IBinder? {postNotification()mediaPlayer.setOnPreparedListener {mediaPlayer.start()}mediaPlayer.setOnCompletionListener {mediaPlayer.stop()}return null}override fun onUnbind(intent: Intent?): Boolean {if(mediaPlayer.isPlaying) {mediaPlayer.stop()}return super.onUnbind(intent)}/*** Request notification permission* 请求通知权限*/private fun requestNotificationPermission(){val notificationPermissionGranted NotificationManagerCompat.from(this).areNotificationsEnabled()if(!notificationPermissionGranted){val intent Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS)intent.putExtra(Settings.EXTRA_APP_PACKAGE,packageName)startActivity(intent)}}private fun postNotification(){//请求通知权限requestNotificationPermission()//创建通知管理器val notificationManager getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager//定义通知渠道val channel NotificationChannel(music_service,一剪梅,NotificationManager.IMPORTANCE_DEFAULT)//创建通知渠道notificationManager.createNotificationChannel(channel)//定义启动服务的意图val intent1 Intent(this,NextActivity::class.java)//定义悬浮意图val pendingIntent PendingIntent.getActivity(this,0,intent1,PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE)//创建通知val notification NotificationCompat.Builder(this,music_service).apply{setOngoing(true)setOnlyAlertOnce(true)setContentTitle(播放音乐)setContentText(正在歌曲播放一剪梅...)setSmallIcon(R.mipmap.ic_launcher)setColorized(true)color resources.getColor(R.color.teal_200,null)setContentIntent(pendingIntent)}.build()startForeground(1,notification)} }2.在AndroidManifest.xml配置服务 serviceandroid:foregroundServiceTypemediaPlaybackandroid:name.MusicServiceandroid:enabledtrueandroid:exportedtrue /设置前台服务类型为mediaPlayback 3.在自定义应用中创建通知渠道 class MusicApp: Application() {override fun onCreate() {super.onCreate()//从API 26开始使用通知渠道if(Build.VERSION.SDK_INTBuild.VERSION_CODES.O){//定义通知管理器val notificationManager: NotificationManager getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager//定义通知渠道的标识val channelId com.example.course.ch07//定义通知渠道的名称val channelName 移动应用开发//定义通知渠道:指定通知渠道的标识、名称和通知渠道的重要级别val channel NotificationChannel(channelId,channelName,NotificationManager.IMPORTANCE_DEFAULT)//创建并配置通知渠道notificationManager.createNotificationChannel(channel)}} }需要在AndroidManifest.xml中指定自定义的应用为当前应用配置自定义应用 applicationandroid:name.MusicApp ........../application4. 在主活动MainActivity中启动服务 要启动前台服务需要在AndroidManifest.xml配置前台服务的权限 uses-permission android:nameandroid.permission.FOREGROUND_SERVICE/ uses-permission android:nameandroid.permission.POST_NOTIFICATIONS /在主活动启动前台服务 class MainActivity : ComponentActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)val intent1 Intent(this,MusicService::class.java)requestPermissions()setContent {Ch07_DemoTheme {// A surface container using the background color from the themeSurface(modifier Modifier.fillMaxSize(),color MaterialTheme.colorScheme.background) {Column(modifier Modifier.fillMaxSize(),verticalArrangement Arrangement.Center,horizontalAlignment Alignment.CenterHorizontally){Row{TextButton(onClick {startService(intent1)}){Text(播放,fontSize 20.sp)}TextButton(onClick {stopService(intent1)}){Text(停止,fontSize 20.sp)}}}}}}}private fun requestPermissions(){//从API 33开始请求发布通知权限if(Build.VERSION.SDK_INTBuild.VERSION_CODES.TIRAMISU) {ActivityCompat.requestPermissions(this,arrayOf(android.Manifest.permission.POST_NOTIFICATIONS),0)}} } 图3 参考文献 陈轶 《Android移动应用开发微课版》清华大学出版社 2022-9 P203-P238 前台服务启动限制 https://developer.android.google.cn/about/versions/12/foreground-services?hlzh-cn
http://www.pierceye.com/news/970257/

相关文章:

  • 哈尔滨h5模板建站设计一个软件需要多少钱
  • 青岛网站建设方案服务惠民卡看电影怎么用
  • 兰州新站点seo加盟网站建设工作有底薪吗
  • 哈尔滨建设网站官网清远头条新闻
  • 泉州网站设计平台wordpress cenos
  • 网站内容批量替换站长之家网站素材
  • asp.net 获取网站域名展览馆展示设计
  • 网站网页设计公司家庭做网站
  • php网站开发实战的书网站开发排行榜
  • 摄影师都在哪些网站发布作品云虚拟主机搭建网站
  • 中小企业电子商务网站建设传奇手游代理平台
  • 网站建设需要每年交钱吗如何选择宣传片制作
  • 建设网站为网站网站做广告芜湖市网站建设
  • 网站建设和维护怎么学android开发编辑wordpress
  • 有哪些学做衣服的网站生产管理软件app
  • 网站换域名 蜘蛛不来广告宣传片制作公司
  • 百度做个网站要多少钱如何在淘宝网做自己的网站
  • 网站建设属于营业范围里的哪一项深圳外贸建站网络推广联客易
  • 网站开发公司 郑州wordpress 服务器环境
  • 网站搭建什么意思砀山做网站
  • 营销型网站服务长沙做网站费用
  • 提供信息门户网站定制怎样做wordpress模板
  • 做爰小视频网站如何制作淘宝客网站
  • 公司架设网站费用怎么做分录linux网站开发软件
  • 网站可信图标精品网站建设费用 地址磐石网络
  • 朝阳住房和城乡建设厅网站学佛网站开发项目需求分析
  • 做快递单的网站会不会是骗人的网站推广营销收费
  • 网站设计师需要学什么wordpress focus
  • 查询网网站十大求职招聘app排行
  • 百度 搜索到手机网站wordpress百科汉化