小米开源录音机解析

xiaoxiao2021-02-28  28

小米开源的录音机(git地址:https://github.com/MiCode/SoundRecorder)

首先源码上我们不难看出2011年12月29日提交的源码,目前为止,前前后后总共有4次提交记录,12年2月底完成了版本的完善,该开源源码在开源社区还是占有一定的地位的;2013年5月份,Google在I/O开发者不会上推出了给予IntelliJ IDEA Java IDE上的AndroidStudio,定位很简单:官方的Android IDE;因此小米的这个源码当然也就是最老的ES开发的版本,那么在这六年多的时间里,直至今日,再来翻看小米的源码且运行,会发生怎样的问题呢?

一、AndroidStudio自定导入Ecplise工程文件的功能,只要等它自行构建文件即可;

二、导入文件之后会发现setLatestEventInfo()方法过期,由此也出现了最新的替代方法,出现了全新的Android通知栏,它可以兼容更高的版本;

主要参数介绍:

notification的title发送notification的application的icon或者发送者的image。notification的messagenotification的数目显示当main icon用来显示sender’s image时 Secondary icon用来显示发送application的icon。时间戳,默认为系统发出通知的时间。

在小米录音机的方法中,我们可以根据本身的应用进行设置,例:

notification = new Notification.Builder(this) .setContentTitle(getString(R.string.app_name)) .setContentText(getString(R.string.notification_recording)) .setContentIntent(pendingIntent) .build();

当写到这里,运行完了之后会再次出现一个问题,狗血的是这个问题还是Notification相关的问题

java.lang.IllegalArgumentException:Invalidnotification(novalidsmallicon):Notification(pri=0contentView=....

查了一下,发现都是通知栏自身的坑啊。。。。

那么这个问题应该怎么解决呢,很好解决,系统自带的属性setContentText()  setContentTitle()  setSmallIcon() ,三者缺一不可,因此上述源码应该更改为:

notification = new Notification.Builder(this) .setContentTitle(getString(R.string.app_name)) .setContentText(getString(R.string.notification_recording)) .setContentIntent(pendingIntent) .setSmallIcon(R.drawable.ic_launcher_soundrecorder) .build();

如果大家是用的Android N以上的设备,那么问题又来了,android.os.FileUriExposedException,这个问题还是一个比较麻烦的;

首先我们需要在清单文件中添加我们的provider

<provider android:name="android.support.v4.content.FileProvider" android:authorities="包名.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/filepaths" /> </provider>

第一步:android:authorties将其改为当前自己应用的包名;

第二步:android:resource在资源文件及res文件夹下面创建一个xml文件夹,新建资源文件filepaths.xml文件,如下所示:

<?xml version="1.0" encoding="utf-8"?> <paths> <root-path name="honjane" path="" /> </paths>

最后还需特别注意的是android6.0以上的设备需要动态添加权限,这个时候我们可以单独写一个权限工具类,添加相应权限,网上有很多这样的工具类,就不赘述啦(* ̄︶ ̄)

转载请注明原文地址: https://www.6miu.com/read-2150116.html

最新回复(0)