本文介绍了我想访问我的系统铃声在颤动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使用Ffltter获取手机的所有铃声,并将选择的铃声设置为我的应用的默认铃声?

提前谢谢

推荐答案

我设法使用本机代码完成了

  1. 首先,您将在颤动端创建这些东西。

    
    // 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');
        }
      }
    
    
  2. 知道您需要实现本机代码。转到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
    }
    

就是这样,我希望这对你有帮助。有任何问题请告诉我。

这篇关于我想访问我的系统铃声在颤动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 01:26