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

易企秀怎么做网站链接如何快速增加网站收录

易企秀怎么做网站链接,如何快速增加网站收录,网络推广合作协议,网站备案填写PendingIntent翻译成中文为“待定意图”#xff0c;这个翻译很好地表示了它的涵义。PendingIntent描述了封装Intent意图以及该意图要执行的目标操作。PendingIntent封装Intent的目标行为的执行是必须满足一定条件#xff0c;只有条件满足#xff0c;才会触发意图的目标操作。…PendingIntent翻译成中文为“待定意图”这个翻译很好地表示了它的涵义。PendingIntent描述了封装Intent意图以及该意图要执行的目标操作。PendingIntent封装Intent的目标行为的执行是必须满足一定条件只有条件满足才会触发意图的目标操作。 一.获取PendingIntent对象 获取PendingIntent对象有以下几种方式: PendingIntent.getActivity(Context, int, Intent, int)启动活动PendingIntent.getActivities(Context, int, Intent[], int)启动多个活动意图中为数组PendingIntent.getBroadcast(Context, int, Intent, int)启动广播PendingIntent.getService(Context, int, Intent, int)启动服务 参数说明 Contextcontext上下文PendingIntent启动活动的上下文intrequestCode请求码 ,发送者发送的请求码Intentintent意图要加载活动的意图intflags 标记 对于其中的标记可以定义为下列形式 FLAG_ONE_SHOTPendingIntent对象仅使用一次FLAG_NO_CREATE如果PendingIntent对象不存在则返回nullFLAG_CANCEL_CURRENT如果PendingIntent对象已存在则取消原有的对象创建新的PendingIntent对象FLAG_UPDATE_CURRENT如果PendingIntent对象已存在则保留原有的对象修改原有对象的属性数据FLAG_IMMUTABLEPendingIntent对象是不可变的FLAG_MUTABLEPendingIntent对象是可变的另外其他Intent中支持的标记都可以在标记参数中使用。 二、应用实例 例如在MainActivity启动前台服务播放音乐利用前台服务的通知提供的内容跳转到其他活动例如SongActivity介绍歌曲。界面如下所示。 点击第一张图的播放会播放音频同时发布通知如第二张图所示。在第二张图的红色箭头区域点击可以屏幕会跳转到第三张图。在第三张图中点击“返回”,则返回主活动。 1. AndroidManifest.xml清单配置权限 uses-permission android:nameandroid.permission.POST_NOTIFICATIONS /uses-permission android:nameandroid.permission.ACCESS_NOTIFICATION_POLICY /uses-permission android:nameandroid.permission.FOREGROUND_SERVICE /uses-permission android:nameandroid.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK /2. 定义MusicService 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()playMusic()return null}override fun onUnbind(intent: Intent?): Boolean {stopMusic()return super.onUnbind(intent)}/*** 播放音乐*/private fun playMusic(){mediaPlayer.setOnPreparedListener {mediaPlayer.start()}mediaPlayer.setOnCompletionListener {mediaPlayer.release()}}/*** 停止播放*/private fun stopMusic(){if(mediaPlayer.isPlaying){mediaPlayer.stop()mediaPlayer.release()}}/*** 创建通知渠道* param id String* param name String*/private fun createNotificationChannel(id:String,name:String){//创建通知管理器val notificationManager getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager//定义通知渠道val channel NotificationChannel(id,name,NotificationManager.IMPORTANCE_DEFAULT)//创建通知渠道notificationManager.createNotificationChannel(channel)}/*** 发布通知*/private fun postNotification(){if(Build.VERSION.SDK_INTBuild.VERSION_CODES.O) {createNotificationChannel(music_service,歌曲)}//定义跳转SongActivity的PendingIntentval descPendingIntent getSongPendingIntent()//定义启动控制音乐播放广播接受器的PendingIntentval playPendingIntent getPlayPendingIntent()//定义启动控制音乐停止播放广播接受器的PendingIntentval stopPendingIntent getStopPendingIntent()//定义动作val playAction NotificationCompat.Action(android.R.drawable.ic_media_play,播放,playPendingIntent)val stopAction NotificationCompat.Action(android.R.drawable.ic_media_pause,停止,stopPendingIntent)//创建通知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(descPendingIntent)// addAction(android.R.drawable.ic_media_play,播放,playPendingIntent) //android23开始不支持 // addAction(android.R.drawable.ic_media_pause,停止,stopPendingIntent)//android23开始不支持addAction(playAction)addAction(stopAction)}.build()startForeground(1,notification)}/*** 跳转到歌曲介绍的界面* return PendingIntent*/private fun getSongPendingIntent():PendingIntent{//定义启动服务的意图val intent Intent(this,SongActivity::class.java)//定义PendingIntentreturn PendingIntent.getActivity(this,1,intent,PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE)}private fun getPlayPendingIntent(): PendingIntent {//创建意图过滤器val intentFilter IntentFilter()//增加动作intentFilter.addAction(PLAY_ACTION)//创建音乐播放广播接受器val playReceiver object: BroadcastReceiver(){override fun onReceive(context: Context?, intent: Intent?) {playMusic()}}//注册播放音乐广播器registerReceiver(playReceiver,intentFilter)//创建播放意图val intent Intent(PLAY_ACTION)return PendingIntent.getBroadcast(this,2,intent,PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)}private fun getStopPendingIntent():PendingIntent{//创建意图过滤器val intentFilter IntentFilter()//增加动作intentFilter.addAction(STOP_ACTION)//创建停止播放广播接受器val stopReceiver object: BroadcastReceiver(){override fun onReceive(context: Context?, intent: Intent?) {stopMusic()}}//注册广播接收器registerReceiver(stopReceiver,intentFilter)//创建意图val intent Intent(STOP_ACTION)return PendingIntent.getBroadcast(this,3,intent,PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)} }3.定义主活动MainActivity class MainActivity : ComponentActivity() {lateinit var intent1:Intentval conn object:ServiceConnection{override fun onServiceConnected(name: ComponentName?, service: IBinder?) {}override fun onServiceDisconnected(name: ComponentName?) {}}override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)intent1 Intent(this,MusicService::class.java)requestNotificationPermission()setContent {Lab03Theme {// A surface container using the background color from the themeSurface(modifier Modifier.fillMaxSize(),color MaterialTheme.colorScheme.background) {MainScreen(playAction::playMusic,stopAction::stopMusic)}}}}/*** 请求通知权限*/private fun requestNotificationPermission(){if(Build.VERSION.SDK_INT Build.VERSION_CODES.TIRAMISU) {ActivityCompat.requestPermissions(this,arrayOf(android.Manifest.permission.POST_NOTIFICATIONS),0)}}/*** 绑定播放音频的服务*/private fun playMusic(){bindService(intent1,conn, Context.BIND_AUTO_CREATE)}/*** 解除绑定*/private fun stopMusic(){unbindService(conn)} }Composable fun MainScreen(playAction:()-Unit,stopAction:()-Unit) {Column(horizontalAlignment Alignment.CenterHorizontally,verticalArrangement Arrangement.Center){Row{TextButton(onClick {playAction.invoke()}){Row{Icon(imageVector Icons.Filled.PlayArrow,contentDescription play)Text(播放)}}TextButton(onClick {stopAction.invoke()}){Row{Icon(imageVector Icons.Filled.Stop,contentDescription play)Text(停止)}}}} }4.定义显示歌曲介绍的SongActivity class SongActivity : ComponentActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContent{Column{Text(正在播放歌曲歌曲介绍内容描述暂时没有定义)TextButton(onClick {//结束当前活动finish()}){Text(返回)}}}} }参考文献 1.PendingIntent https://developer.android.google.cn/reference/android/app/PendingIntent
http://www.dnsts.com.cn/news/132720.html

相关文章:

  • 普通网站和营销网站有何不同哪里有网络课程平台网站_就是帮老师建设一个教学的网站
  • 河南网站开发公司人才网网站建设方案
  • 营销型网站建设服务商青龙建站教程
  • 做网站时如何上传图片codewars网站
  • 网站建设列入无形资产管理吗建筑模板规格
  • 联邦快递网站建设的目标网站的特征包括哪些
  • 东莞市品牌网站建设把网站传到服务器上怎么做
  • 代做土木毕业设计网站网站重新解析
  • 做淘宝那样的网站做远程培训网站用什么系统
  • 思途智旅游网站开发建设网站要准备什么
  • 网站公司做文员seo需要会网站建设吗
  • 创建网站要多长时间广州外贸公司联系方式
  • 校园招生网站建设的简报淮北论坛招聘求职
  • 湖南沙坪建设有限公司网站注册安全工程师白考了
  • 公司网站制作费用浙江省特种作业证查询官网
  • 德阳市住房和城乡建设局网站首页百度助手免费下载
  • 网站建设及使用制作一个网站需要多少钱
  • 杭州企业网站制作加驰牛科技长春阿凡达网站建设
  • 阳新网站建设用什么编程语言做网站好
  • 口碑好的网站开发公司电话网站建设ktv
  • 郑州网站提升排名视频号直播怎么引流
  • 网站建设协议 模板wordpress友情链接导入
  • 鄂尔多斯市住房和城乡建设厅网站网站没有索引量是什么
  • 怎么免费建自己的网站运城市做网站
  • 河北企业建网站天津市建筑信息平台
  • 宠物网站设计模板做网站如何设计数据库
  • 网站建设优化一年赚几十万江西宣传片制作公司
  • 网站搜索不到公司网站海纳企业网站管理系统
  • 建设捐款网站网站建设公司面临的问题
  • 做网站大作业的心得体会如何把自己做的网站