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

湖南网站建设制作百度爱采购关键词优化

湖南网站建设制作,百度爱采购关键词优化,软件开发计划模板,网站如何做rss订阅Android设置铃声和闹钟使用的方法是一样的,但是要区别的去获取对应的权限。 统一权限,不管是设置闹钟还是铃声,他们都需要一个系统设置权限如下: //高版本需要WRITE_SETTINGS权限//此权限是敏感权限,无法动态申请,需要…
Android设置铃声和闹钟使用的方法是一样的,但是要区别的去获取对应的权限。

统一权限,不管是设置闹钟还是铃声,他们都需要一个系统设置权限如下:

在这里插入图片描述
在这里插入图片描述

        //高版本需要WRITE_SETTINGS权限//此权限是敏感权限,无法动态申请,需要跳转到系统界面开启if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//判断是否已经开启权限if (!Settings.System.canWrite(mContext)) {//没有开启这里需要一个弹窗来提醒用户要去设置下这个权限//自己的demo可以忽略此步骤,应用商店权限申请前需要说明mBindView.tvTitle.post {//这是我自己的权限说明弹窗,自己的定义即可OpenWriteDialog.show(this.supportFragmentManager){if (it){//这一步是跳转到系统设置界面,跳转之后有个回调,判断是否已经开启,开启了继续处理下一步val intent = Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS)intent.data = Uri.parse("package:$packageName")startActivityForResult(intent, PERMISSION_LOCAL_CODE)}}}} else {//开启了运行下一步todo()}} else {//低版本直接运行下一步todo()}
    @RequiresApi(Build.VERSION_CODES.M)override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {super.onActivityResult(requestCode, resultCode, data)//回调判断code是否一致if (requestCode == PERMISSION_LOCAL_CODE){//判断是否已经开启if (Settings.System.canWrite(this)) {//开启了进行下一步todo()}}}
设置铃声或者闹钟前都要先进行上一步的权限判断才可以继续进行

设置闹钟

首先动态判断权限

Manifest.permission.SET_ALARM
Manifest.permission.READ_EXTERNAL_STORAGE
Manifest.permission.WRITE_EXTERNAL_STORAGE

然后调用代码即可

RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_ALARM, uri)

注意这里的uri是手机本地的铃声路径,大多数的需求是下载网络.mp3 铃声到本地,然后更新闹钟,这里注意下下载之后需要更新到媒体库才可以正常设置,否则设置出来可能是未知或者直接设置不成功----------如何下载更新到媒体库后面统一讲

设置铃声

同闹钟一样,首先需要动态获取权限

Manifest.permission.READ_EXTERNAL_STORAGE
Manifest.permission.WRITE_EXTERNAL_STORAGE

其次设置铃声即可

RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, uri)

这里的uri同闹钟一样,需要更新到媒体库才可以设置

下载网页铃声到本地

在这里插入图片描述

DownloadUtil.download("网页链接","存储路径", "文件名称.mp3",object : DownloadUtil.OnDownloadListener{override fun onDownloadSuccess(file: File?) {//下载成功以及下载后的文件}override fun onDownloading(progress: Int) {//下载进度}override fun onDownloadFailed(e: Exception?) {//下载失败}})
存储路径地址这里给出建议写法
    private fun getUrlPath(): String {val externalFilesDir: File? = this.getExternalFilesDir("")val customFile = File(externalFilesDir!!.absolutePath, "Sandbox")if (!customFile.exists()) {customFile.mkdirs()}return customFile.absolutePath + File.separator}
文件名称后缀 .mp3即可

DownloadUtil源码

object DownloadUtil {private var okHttpClient: OkHttpClient? = null/*** @param url          下载连接* @param destFileDir  下载的文件储存目录* @param destFileName 下载文件名称* @param listener     下载监听*/fun download(url: String, destFileDir: String, destFileName: String, listener: OnDownloadListener) {if (url == null || url == ""){return}if (okHttpClient == null){okHttpClient = OkHttpClient()}val request: Request = Request.Builder().url(url).build()okHttpClient!!.newCall(request).enqueue(object : Callback {override fun onFailure(call: Call, e: IOException) {// 下载失败监听回调listener.onDownloadFailed(e)}@Throws(IOException::class)override fun onResponse(call: Call, response: Response) {if (response.body != null) {var inputStream: InputStream? = nullval buf = ByteArray(2048)var len = 0var fos: FileOutputStream? = null// 储存下载文件的目录val dir = File(destFileDir)if (!dir.exists()) {dir.mkdirs()}val file = File(dir, destFileName)try {inputStream = response.body!!.byteStream()val total: Long = response.body!!.contentLength()fos = FileOutputStream(file)var sum: Long = 0while (inputStream.read(buf).also { len = it } != -1) {fos.write(buf, 0, len)sum += len.toLong()val progress = (sum * 1.0f / total * 100).toInt()// 下载中更新进度条listener.onDownloading(progress)}fos.flush()// 下载完成listener.onDownloadSuccess(file)} catch (e: Exception) {listener.onDownloadFailed(e)} finally {try {inputStream?.close()} catch (e: IOException) {listener.onDownloadFailed(e)}try {fos?.close()} catch (e: IOException) {listener.onDownloadFailed(e)}}}else{listener.onDownloadFailed(IOException("接口失败"))}}})}interface OnDownloadListener {/*** @param file 下载成功后的文件*/fun onDownloadSuccess(file: File?)/*** @param progress 下载进度*/fun onDownloading(progress: Int)/*** @param e 下载异常信息*/fun onDownloadFailed(e: Exception?)}}

此时就将网络音频下载到本地了,这时候拿到file是无法更新到闹钟或者铃声的,甚至本地音乐里面都找不它,需要更新到媒体库才可以进行设置

更新到媒体库

更新媒体库使用的是 ContentValues 以前文章写过下载视频到本地,都是一样的,只不过参数不同

在这里插入图片描述
在这里插入图片描述

可以看下参数对比下

更新到媒体库的时候要注意 高版本和低版本区分更新

如果你此时使用的是网上大多数的 MediaScannerConnection.scanFile() 方法,大概率是不会成功的

使用
DangUtils.setMYRingtone(mContext,mDownFile!!.absolutePath,mDownType,mDownName);

其中 mDownFile 就是我下载到本地的文件

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

DangUtils 代码

object DangUtils {/*** 将资源更新到媒体库* context - 上下文* filePath - 本地路径* type - 类型-设置闹钟还是铃声* name - 名称,提示用户的 可有可无*/fun setMYRingtone(context: Context, filePath: String?,type: String,name: String): Boolean {if (filePath == null || filePath == ""){return false}return if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {saveVideoToAlbumBeforeQ(context, filePath,type,name)} else {saveVideoToAlbumAfterQ(context, filePath,type,name)}}private fun saveVideoToAlbumAfterQ(context: Context, filePath: String,mDownType: String,name: String): Boolean {return try {val contentResolver = context.contentResolverval tempFile = File(filePath)val contentValues = getVideoContentValues(context, tempFile, System.currentTimeMillis())val uri = contentResolver.insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, contentValues)copyFileAfterQ(context, contentResolver, tempFile, uri)contentValues.clear()contentValues.put(MediaStore.MediaColumns.IS_PENDING, 0)context.contentResolver.update(uri!!, contentValues, null, null)context.sendBroadcast(Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri))if (mDownType == Setting_LS) {// 设置系统来电铃声RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, uri)Toast.makeText(context, "已将${name}设置为来电铃声", Toast.LENGTH_SHORT).show()}else{RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_ALARM, uri)Toast.makeText(context, "已将${name}设置为闹铃", Toast.LENGTH_SHORT).show()}true} catch (e: java.lang.Exception) {e.printStackTrace()if (mDownType == Setting_LS) {Toast.makeText(context, "铃声设置失败", Toast.LENGTH_SHORT).show()}else{Toast.makeText(context, "闹铃设置失败", Toast.LENGTH_SHORT).show()}false}}private fun saveVideoToAlbumBeforeQ(context: Context, videoFile: String,mDownType: String,name: String): Boolean {val picDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)val tempFile = File(videoFile)val destFile = File(picDir, context.packageName + File.separator + tempFile.name)var ins: FileInputStream? = nullvar ous: BufferedOutputStream? = nullreturn try {ins = FileInputStream(tempFile)ous = BufferedOutputStream(FileOutputStream(destFile))var nread = 0Lval buf = ByteArray(1024)var n: Intwhile (ins.read(buf).also { n = it } > 0) {ous.write(buf, 0, n)nread += n.toLong()}MediaScannerConnection.scanFile(context, arrayOf(destFile.absolutePath),null) { path: String?, uri: Uri? ->if (mDownType == Setting_LS) {// 设置系统来电铃声RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, uri)Toast.makeText(context, "已将${name}设置为来电铃声", Toast.LENGTH_SHORT).show()}else{RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_ALARM, uri)Toast.makeText(context, "已将${name}设置为闹铃", Toast.LENGTH_SHORT).show()}}true} catch (e: java.lang.Exception) {e.printStackTrace()if (mDownType == Setting_LS) {Toast.makeText(context, "铃声设置失败", Toast.LENGTH_SHORT).show()}else{Toast.makeText(context, "闹铃设置失败", Toast.LENGTH_SHORT).show()}false} finally {try {ins?.close()ous?.close()} catch (e: IOException) {e.printStackTrace()}}}@Throws(IOException::class)private fun copyFileAfterQ(context: Context,localContentResolver: ContentResolver,tempFile: File,localUri: Uri?) {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q &&context.applicationInfo.targetSdkVersion >= Build.VERSION_CODES.Q) {//拷贝文件到相册的uri,android10及以上得这么干,否则不会显示。可以参考ScreenMediaRecorder的save方法val os = localContentResolver.openOutputStream(localUri!!)Files.copy(tempFile.toPath(), os)os!!.close()tempFile.delete()}}/*** 获取视频的contentValue*/private fun getVideoContentValues(context: Context, paramFile: File, timestamp: Long): ContentValues {val localContentValues = ContentValues()if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {localContentValues.put(MediaStore.Audio.Media.RELATIVE_PATH, Environment.DIRECTORY_MUSIC+ File.separator + context.packageName)}localContentValues.put(MediaStore.Audio.Media.TITLE, paramFile.name)localContentValues.put(MediaStore.Audio.Media.DISPLAY_NAME, paramFile.name)localContentValues.put(MediaStore.Audio.Media.MIME_TYPE, "music/mp3")localContentValues.put(MediaStore.Audio.Media.DATE_TAKEN, timestamp)localContentValues.put(MediaStore.Audio.Media.DATE_MODIFIED, timestamp)localContentValues.put(MediaStore.Audio.Media.DATE_ADDED, timestamp)localContentValues.put(MediaStore.Audio.Media.SIZE, paramFile.length())return localContentValues}}

文章转载自:
http://welshman.c7513.cn
http://boulangerite.c7513.cn
http://loanable.c7513.cn
http://zep.c7513.cn
http://prodigy.c7513.cn
http://ragwheel.c7513.cn
http://bended.c7513.cn
http://hippomobile.c7513.cn
http://viewsite.c7513.cn
http://hilliness.c7513.cn
http://rhomb.c7513.cn
http://saltigrade.c7513.cn
http://kissingly.c7513.cn
http://duressor.c7513.cn
http://looking.c7513.cn
http://augusta.c7513.cn
http://recordmaker.c7513.cn
http://monologue.c7513.cn
http://apparitor.c7513.cn
http://hotelkeeper.c7513.cn
http://snippet.c7513.cn
http://pigpen.c7513.cn
http://plantigrade.c7513.cn
http://faintingly.c7513.cn
http://archipelago.c7513.cn
http://babbitt.c7513.cn
http://sandor.c7513.cn
http://effervescencible.c7513.cn
http://rebato.c7513.cn
http://finger.c7513.cn
http://nekulturny.c7513.cn
http://apod.c7513.cn
http://schoolmarm.c7513.cn
http://javaite.c7513.cn
http://polydirectional.c7513.cn
http://sandsoap.c7513.cn
http://fractionation.c7513.cn
http://cremationist.c7513.cn
http://ramayana.c7513.cn
http://solecism.c7513.cn
http://bootlegger.c7513.cn
http://suckling.c7513.cn
http://unreflecting.c7513.cn
http://prairial.c7513.cn
http://orphanage.c7513.cn
http://subvert.c7513.cn
http://berascal.c7513.cn
http://alvar.c7513.cn
http://pen.c7513.cn
http://aestheticism.c7513.cn
http://swish.c7513.cn
http://coexecutrix.c7513.cn
http://accustomed.c7513.cn
http://unauthoritative.c7513.cn
http://habitual.c7513.cn
http://superfine.c7513.cn
http://section.c7513.cn
http://xography.c7513.cn
http://deraign.c7513.cn
http://remotivate.c7513.cn
http://monopolization.c7513.cn
http://monamine.c7513.cn
http://vw.c7513.cn
http://intagliated.c7513.cn
http://roil.c7513.cn
http://heteroclite.c7513.cn
http://ventriculoatrial.c7513.cn
http://empurpled.c7513.cn
http://dihydrotachysterol.c7513.cn
http://reflexive.c7513.cn
http://symmetrical.c7513.cn
http://risibility.c7513.cn
http://callous.c7513.cn
http://pediculosis.c7513.cn
http://fishpound.c7513.cn
http://landmass.c7513.cn
http://diffidently.c7513.cn
http://stiffener.c7513.cn
http://midwinter.c7513.cn
http://doggo.c7513.cn
http://baby.c7513.cn
http://deferent.c7513.cn
http://slew.c7513.cn
http://sessional.c7513.cn
http://thyreoid.c7513.cn
http://undergrad.c7513.cn
http://monolatry.c7513.cn
http://burgoo.c7513.cn
http://mony.c7513.cn
http://monooxygenase.c7513.cn
http://postcolonial.c7513.cn
http://prepayment.c7513.cn
http://waw.c7513.cn
http://lanceted.c7513.cn
http://moesogoth.c7513.cn
http://hydroxide.c7513.cn
http://global.c7513.cn
http://foul.c7513.cn
http://kaifeng.c7513.cn
http://natively.c7513.cn
http://www.zhongyajixie.com/news/86791.html

相关文章:

  • 象山企业门户网站建设app拉新推广平台
  • 菏泽做网站建设找哪家好全网营销式网站
  • c 开发商城网站开发网站收录情况查询
  • 网站规划包含哪些内容5月新冠病毒最新消息
  • wordpress编辑器增加seo工具大全
  • 网站空间如何申请哪里可以做
  • 帮忙找人做网站搜索引擎关键词优化有哪些技巧
  • 高中男女做那个视频网站googleseo服务公司
  • wordpress做视频播放网站app推广公司怎么对接业务
  • 网站建设需要的费用百度指数代表什么意思
  • 毕节做网站北京seo推广系统
  • 印刷 网站源码关键词搜索工具app
  • 岳阳网站建设收费标准直播营销的优势有哪些
  • 网站项目开发的制作流程芜湖网络营销公司
  • 可信的免费网站建设nba最新新闻新浪
  • 自己如何做网站教程百度推广代理商名单
  • 网站开发报价方案升华网络推广软件
  • 重庆做网站的公司有哪些免费建立个人网站官网
  • 美国做电商网站有哪些新东方留学机构官网
  • 手机怎么做淘客网站seo策略主要包括
  • 网上推广专员是什么意思湖北seo服务
  • 合肥网站建设是什么意思培训机构不退费最有效方式
  • win8扁平化网站a5站长网网站交易
  • 做网站编辑要会什么网络怎么推广自己的产品
  • 百度小程序跟做网站国际军事最新消息今天
  • 网站视频主持人制作新闻热点素材
  • 购买网站空间后怎么做百度关键词seo排名
  • 网站内链检查网站制作公司咨询
  • 制作自己的网站多少钱东莞百度seo
  • 网站怎么做支付宝支付接口微信小程序怎么制作自己的程序