Androidサウンドデシベル制御ロック画面demo実現
59480 ワード
タイトルdemoは全体的なロジックを実現する:
1.権限2を宣言します.宣言、receiverを登録し、登録設定マネージャの結果を受信する.APPの登録を完了するのはデバイスマネージャ4です.録音権限申請5.クラスファイルを作成し、ビジネスロジックを実現
OK、始めましょう~~宣言権限は、次の です.声明、receiverを登録し、登録設定マネージャを受信した結果、ブロードキャスト受信機クラスファイルは以下の通りである:
ブロードキャスト受信機の登録:完了登録APPはデバイスマネージャ 録音権限申請
権限コールバック:
runVoiceRecord()メソッド:クラスファイルを作成し、ビジネスロジックマイクロホン管理クラスを実現する:AudioRecordManager ビジネスロジック を実現
これにより、すべてのロジックが完了し、activityファイル全体が提供されます.
これでdemoが実現したら、ぜひ軽く噴き出してね~~~
1.権限2を宣言します.宣言、receiverを登録し、登録設定マネージャの結果を受信する.APPの登録を完了するのはデバイスマネージャ4です.録音権限申請5.クラスファイルを作成し、ビジネスロジックを実現
OK、始めましょう~~
<!-- -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- -->
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<!-- -->
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
/**
* com.gerryrun.gerryandroiddemo.testdemo.lockscreen
* create by GerryRun
* email:[email protected]
* 2020-03-14 21:20
* Describe here: :receiver,
*/
class MyAdminReceiver : DeviceAdminReceiver() {
fun showToast(context: Context, msg: String) {
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}
override fun onEnabled(context: Context, intent: Intent) {
showToast(
context,
" "
);
}
override fun onDisabled(context: Context, intent: Intent) {
showToast(
context,
" "
);
}
}
ブロードキャスト受信機の登録:
<receiver
android:name=".testdemo.lockscreen.MyAdminReceiver"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data
android:name="android.app.device_admin"
android:resource="@xml/lock_screen" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
/**
*
*/
fun CheckAdminPermission() {
var mComponentName = ComponentName(this, MyAdminReceiver::class.java)
var dmp: DevicePolicyManager =
getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager
var who = ComponentName(mActivity, MyAdminReceiver::class.java)
if (dmp.isAdminActive(who)) {
showToast(" ")
runVoiceRecord()
} else {
var intent =
Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN)
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mComponentName)
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, " ...")
startActivity(intent)
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(
arrayOf(Manifest.permission.RECORD_AUDIO),
REQUEST_ASK_RECORD_AUDIO
)
return
}
}
権限コールバック:
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
when (requestCode) {
REQUEST_ASK_RECORD_AUDIO -> {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
showToast(" ")
//
runVoiceRecord()
} else {
showToast(" ")
}
}
}
}
runVoiceRecord()メソッド:
private fun runVoiceRecord() {
var dmp: DevicePolicyManager =
getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager
mHandler = Handler(Handler.Callback {
if (it.what == 1) {
var voice: Double = it.obj as Double
if (voice > 30) {
var who = ComponentName(mActivity, MyAdminReceiver::class.java)
if (dmp.isAdminActive(who)) {
dmp.lockNow()
finish()
} else {
var intent = Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN)
var mcomponentName = ComponentName(mActivity, MyAdminReceiver::class.java)
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mcomponentName)
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, " ")
mActivity.startActivity(intent)
}
}
}
return@Callback false
})
mAudioRecordManager = AudioRecordManager(mHandler, 1)
mAudioRecordManager?.getNoiseLevel(true)
}
/**
* com.gerryrun.gerryandroiddemo.testdemo.lockscreen
* create by GerryRun
* email:[email protected]
* 2020-03-14 21:22
* Describe here:
*/
class AudioRecordManager {
private var thread: Thread? = null
/**
* handler
*/
var mCanSendMsg: Boolean = true
val TAG = "AudioRecordManager"
//
var SAMPLE_RATE_IN_SIZE = 44100
var BUFFER_SIZE = AudioRecord.getMinBufferSize(
SAMPLE_RATE_IN_SIZE,
AudioFormat.CHANNEL_IN_DEFAULT,
AudioFormat.ENCODING_PCM_16BIT
)
//
var mAudioRecord: AudioRecord? = null
//
var isVoiceRun = false
var mHandler: Handler? = null
var mWhat = 0
//
lateinit var mLock: Object
/**
*
*/
constructor(handler: Handler, what: Int) {
mHandler = handler
mWhat = what
mLock = Object()
}
//
fun getNoiseLevel(canSendMsg: Boolean) {
if (isVoiceRun) return
mCanSendMsg = canSendMsg
isVoiceRun = true
thread = object : Thread() {
override fun run() {
//
while (isVoiceRun) {
if (mCanSendMsg) {
/**
*
*/
if (null == mAudioRecord)
mAudioRecord = AudioRecord(
MediaRecorder.AudioSource.MIC,
SAMPLE_RATE_IN_SIZE,
AudioFormat.CHANNEL_IN_DEFAULT,
AudioFormat.ENCODING_PCM_16BIT, BUFFER_SIZE
)
if (mAudioRecord == null) throw NullPointerException(" ")
var buffer = ShortArray(BUFFER_SIZE)
/**
* , try catch
*/
try {
mAudioRecord?.startRecording()
} catch (e: IllegalStateException) {
e.printStackTrace()
}
var r = mAudioRecord?.read(buffer, 0, BUFFER_SIZE)
var v = 0
for (i in 0 until buffer.size) {
v += buffer[i] * buffer[i]
}
/**
* ,
*/
if (r == null) {
return
}
var mean: Double = (v / (r!!.toDouble()))
var volume = 10 * log10(mean)
if (!this.isInterrupted)
synchronized(mLock) {
mLock.wait(500)
doSendMsg(volume)
}
}
}
}
}
thread?.start()
}
/**
* activity
*/
fun doSendMsg(volume: Double) {
var msg: Message = Message.obtain()
msg.what = mWhat
msg.obj = volume
Log.e(TAG, "volume==$volume")
mHandler?.sendMessage(msg)
}
/**
*
*/
fun stopRecord(canSendMsg: Boolean) {
mCanSendMsg = canSendMsg
Log.e(TAG, "null != thread==" + (null != thread))
if (null != thread) {
/**
* :
* Thread.interrupt()
*
* Thread.stop() , , boom,
*
*/
thread!!.interrupt()
}
thread = null
/**
*
*/
if (null != mAudioRecord) {
mAudioRecord?.release()
mAudioRecord = null
}
}
}
private fun runVoiceRecord() {
/**
* DevicePolicyManager
*/
var dmp: DevicePolicyManager =
getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager
mHandler = Handler(Handler.Callback {
if (it.what == 1) {
var voice: Double = it.obj as Double
if (voice > 30) {
// app
var who = ComponentName(mActivity, MyAdminReceiver::class.java)
if (dmp.isAdminActive(who)) {
//
dmp.lockNow()
finish()
} else {
//
var intent = Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN)
var mcomponentName = ComponentName(mActivity, MyAdminReceiver::class.java)
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mcomponentName)
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, " ")
mActivity.startActivity(intent)
}
}
}
return@Callback false
})
// AudioRecordManager
mAudioRecordManager = AudioRecordManager(mHandler, 1)
//
mAudioRecordManager?.getNoiseLevel(true)
}
これにより、すべてのロジックが完了し、activityファイル全体が提供されます.
class LockScreenActivity : BaseActivity<ActivityLockScreenBinding>() {
private var mAudioRecordManager: AudioRecordManager? = null
private lateinit var mHandler: Handler
var REQUEST_ASK_RECORD_AUDIO = 1001
override fun getContentViewId(): Int {
return R.layout.activity_lock_screen
}
override fun initTheView() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(
arrayOf(Manifest.permission.RECORD_AUDIO),
REQUEST_ASK_RECORD_AUDIO
)
return
}
}
CheckAdminPermission()
}
private fun runVoiceRecord() {
/**
* DevicePolicyManager
*/
var dmp: DevicePolicyManager =
getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager
mHandler = Handler(Handler.Callback {
if (it.what == 1) {
var voice: Double = it.obj as Double
if (voice > 30) {
// app
var who = ComponentName(mActivity, MyAdminReceiver::class.java)
if (dmp.isAdminActive(who)) {
//
dmp.lockNow()
finish()
} else {
//
var intent = Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN)
var mcomponentName = ComponentName(mActivity, MyAdminReceiver::class.java)
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mcomponentName)
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, " ")
mActivity.startActivity(intent)
}
}
}
return@Callback false
})
// AudioRecordManager
mAudioRecordManager = AudioRecordManager(mHandler, 1)
//
mAudioRecordManager?.getNoiseLevel(true)
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
when (requestCode) {
REQUEST_ASK_RECORD_AUDIO -> {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
showToast(" ")
//
runVoiceRecord()
} else {
showToast(" ")
}
}
}
}
/**
*
*/
fun CheckAdminPermission() {
var mComponentName = ComponentName(this, MyAdminReceiver::class.java)
var dmp: DevicePolicyManager =
getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager
var who = ComponentName(mActivity, MyAdminReceiver::class.java)
if (dmp.isAdminActive(who)) {
showToast(" ")
runVoiceRecord()
} else {
var intent =
Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN)
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mComponentName)
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, " ...")
startActivity(intent)
}
}
override fun onDestroy() {
super.onDestroy()
mAudioRecordManager!!.stopRecord(false)
mAudioRecordManager = null
}
}
これでdemoが実現したら、ぜひ軽く噴き出してね~~~