有什么办法可以得到所有的铃声手机使用的flutter,并将所选铃声设置为我的应用程序的默认铃声?
提前致谢
我设法使用本机代码完成它
-
首先,您将在 Flutter 端创建这些东西。
// here where your ringtones will be stored
List<Object?> result = ['Andromeda'];
// this is the channel that links flutter with native code
static const channel = MethodChannel('com.example.pomo_app/mychannel');
// this method waits for results from the native code
Future<void> getRingtones() async {
try {
result = await channel.invokeMethod('getAllRingtones');
} on PlatformException catch (ex) {
print('Exception: $ex.message');
}
}
-
知道您需要实现本机代码。转到MainActivity.kt
// add the name of the channel that we made in flutter code
private val channel = "com.example.pomo_app/mychannel"
// add this method to handle the calls from flutter
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, channel)
.setMethodCallHandler { call, result ->
when (call.method) {
"getAllRingtones" -> {
result.success(getAllRingtones(this))
}
// this function will return all the ringtones names as a list
private fun getAllRingtones(context: Context): List<String> {
val manager = RingtoneManager(context)
manager.setType(RingtoneManager.TYPE_RINGTONE)
val cursor: Cursor = manager.cursor
val list: MutableList<String> = mutableListOf()
while (cursor.moveToNext()) {
val notificationTitle: String = cursor.getString(RingtoneManager.TITLE_COLUMN_INDEX)
list.add(notificationTitle)
}
return list
}
就是这样,我希望这对您有所帮助。如有任何问题请告诉我。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)