使用AndroidStudio建立模块与unity进行交互

Android Studio:1.新建一个Module -> android library ;2.在该Module下有个src/main/java/xxx包下新建两个Java class3.写入设置手机 亮度 和 音量 的脚本:package erlangshensystem.datanetwork.com.mylibrary;import android.app.Activ
摘要由CSDN通过智能技术生成

Android Studio:

1.新建一个Module -> android library ;

2.在该Module下有个src/main/java/xxx包下新建两个Java class

3.写入设置手机 亮度 和 音量 的脚本:

package erlangshensystem.datanetwork.com.mylibrary;
import  android.app.Activity;
import android.provider.Settings;
import android.util.Log;
import android.view.WindowManager;

/**
 * Created by C on 2015/7/15.
 */

public class Brightness {
    private Activity mActivity;

    public Brightness(Activity currentActivity) {
        Log.i("JavaClass", "Constructor called with currentActivity = " + currentActivity);
        mActivity = currentActivity;
        //Settings.System.putInt(mActivity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
    }

    public void SetBrightness(int value) {
        float tmp = value / 100f;
        Settings.System.putInt(mActivity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, (int) (tmp * 255));
        /*
        WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
        if (tmp == -1)
            lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
        else
            lp.screenBrightness = tmp <= 0 ? 1 : tmp;
        mActivity.getWindow().setAttributes(lp);
        */
    }

    public  float GetBrightness()
    {
        int current = Settings.System.getInt(mActivity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS,0);
        float tmp = current/255f;
        return  tmp;
    }
}
package erlangshensystem.datanetwork.com.mylibrary;
import  android.app.Activity;
import android.app.Service;
import android.media.AudioManager;
import android.util.Log;

/**
 * Created by C on 2015/7/16.
 */
public class SoundVolume
{
    private Activity mActivity;
    private  AudioManager am;

    public SoundVolume(Activity currentActivity) {
        Log.i("JavaClass", "Constructor called with currentActivity = " + currentActivity);
        mActivity = currentActivity;
        am = (AudioManager)mActivity.getApplication().getSystemService(Service.AUDIO_SERVICE);
    }

    public  void SetSoundVolume(int value)
    {
        int max = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        float tmp = value /100f;
        am.setStreamVolume(AudioManager.STREAM_MUSIC,(int)(tmp*max), 0);
    }

    public float GetSoundVolume()
    {
        int current = am.getStreamVolume(AudioManager.STREAM_MUSIC);
        float tmp = current/(float)am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        return  tmp;
    }
}
4.选中该module右击选择 make module ‘xxx’;

5.然后右击show in explorer,找到..\xxx\build\outputs\aar\xxx-debug.arr的压缩包,解压获取class.jar(包名可以更改)包导入unity工程plugins/Android/下即可。

Unity Project:

新建一个脚本然后添加如下内容

AndroidJavaClass jc;
    AndroidJavaObject jo,act;

    private void GetAndroidAcitvity()
    {
        jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        act = jc.GetStatic<AndroidJavaObject>("currentActivity");
    }

    public void SetSoundVolume()
    {
        GetAndroidAcitvity();
        jo = new AndroidJavaObject("erlangshensystem.datanetwork.com.mylibrary.SoundVolume", act);
        jo.Call("SetSoundVolume", volume);
    }

    public void SetBrightness()
    {
        GetAndroidAcitvity();
        jo = new AndroidJavaObject("erlangshensystem.datanetwork.com.mylibrary.Brightness", act);
        jo.Call("SetBrightness", intensity);
    }

    public void GetSoundVolume()
    {
        GetAndroidAcitvity();
        jo = new AndroidJavaObject("erlangshensystem.datanetwork.com.mylibrary.SoundVolume", act);
        float curVolume = jo.Call<float>("GetSoundVolume");
        volume = (int)(curVolume * 100);
    }

    public void GetBrightness()
    {
        GetAndroidAcitvity();
        jo = new AndroidJavaObject("erlangshensystem.datanetwork.com.mylibrary.Brightness", act);
        float curBrightness = jo.Call<float>("GetBrightness");
        intensity = (int)(curBrightness * 100);
    }



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值