检查Android设备是否已静音 [英] Check if Android device is muted

查看:905
本文介绍了检查Android设备是否已静音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为android应用创建了简单的插件.但是由于某些奇怪的原因,调用我的函数的结果始终是"false".我的插件应通知应用有关手机是否静音的信息.这是我的插件的代码:

I created simple plugin for android apps. But for some strange reason the result of calling of my function is always "false". My plugin should inform the app about is phone muted or not. Here is code of my plugin:

import android.app.Fragment;
import android.content.Context;
import android.media.AudioManager;

public class AndroidMuteCtrl extends Fragment  {

    public static String debugThis()
    {
        return "Test message from AndroidMuteCtrl plugin.";
    }

    public boolean isMuted()
    {
        AudioManager audio = (AudioManager) this.getActivity().getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
        if (audio.getRingerMode() ==  AudioManager.RINGER_MODE_NORMAL) return false;
        else  return  true;
    }

}

还有我的C#代码:

//...
AndroidJavaClass pluginClass = new AndroidJavaClass("com.overly.mutecontrol.AndroidMuteCtrl");
//...
bool isMuted = pluginClass.Call<bool>("isMuted"); // ALWAYS FALSE
//...

推荐答案

这是因为AudioManager需要引用活动或上下文才能起作用.

This is because AudioManager requires a reference to Activity or Context to work.

该行:

(AudioManager) this.getActivity()

应该失败,因为那里没有.您需要将当前的Activity或Context从Unity插件发送到静态函数,然后才能调用它.

should fail miserably since there is non. You need to send the current Activity or Context from Unity plugin to a static function then you can call that.

此外,在此处扩展Fragment完全没有用.

Also, it is totally useless to extend Fragment here.

是发送到Java插件并是如何发送到Java插件.

This is how to send Context to java plugin and this is how to send Activity to java plugin.

在这种情况下,我会将Context发送到插件.

In this case, I will send Context to the plugin.

Java:

public final class AndroidMuteCtrl{

    static Context myContext;
    // Called From C# to get the Context Instance
    public static void receiveContextInstance(Context tempContext) {
        myContext = tempContext;
    }

    public static String debugThis()
    {
        return "Test message from AndroidMuteCtrl plugin.";
    }

    public static boolean isMuted()
    {
        AudioManager audio = myContext.getSystemService(Context.AUDIO_SERVICE);
        if (audio.getRingerMode() ==  AudioManager.RINGER_MODE_NORMAL) return false;
        else  return  true;
    }
}

C#:

AndroidJavaClass unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject unityActivity = unityClass.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject unityContext = unityActivity.Call<AndroidJavaObject>("getApplicationContext");

AndroidJavaClass pluginClass = new AndroidJavaClass("com.overly.mutecontrol.AndroidMuteCtrl");
//Send the Context
pluginClass.CallStatic("receiveContextInstance", unityContext);
bool isMuted = pluginClass.CallStatic<bool>("isMuted"); 

这是直接在编辑器中输入的,未经测试,因此您可能需要对其进行一些修改才能使其正常工作.

This was typed into the Editor directly and is not tested, so you may have to modify it a little bit to get it working.

这篇关于检查Android设备是否已静音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆