unity 实现OnCompletionListener 使用AndroidJavaProxy

public class CompletionCallback : AndroidJavaProxy {

        public CompletionCallback()
        :base("android.media.MediaPlayer$OnCompletionListener") {
        }

        void onCompletion(AndroidJavaObject mp) {
            CommonUtil.showToast("finish");
        }
    }

 

using UnityEngine;
using System.Collections;

public class SelectedDate : MonoBehaviour {
    public static Date date = System.DateTime.Now;
}

public class DateCallback : AndroidJavaProxy {
    public DateCallback()
        :base("android.app.DatePickerDialog$OnDateSetListener")
    {
    }

    void onDateSet(AndroidJavaObject view, int year, int monthOfYear, int dayOfMonth) {
        SelectedDate.date = new Date(year, monthOfYear + 1, dayOfMonth);
    }
}

public class ExampleClass : MonoBehaviour {
    void OnGUI() {
        if (GUI.Button(new Rect(10, 10, 450, 100), String.Format("{0:yyyy-MM-dd}", SelectedDate.date))) {
            AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
            activity.Call(
                "runOnUiThread",
                AndroidJavaRunnable(
                    new AndroidJavaObject(
                        "android.app.DatePickerDialog",
                        activity,
                        new DateCallback(),
                        SelectedDate.date.Year,
                        SelectedDate.date.Month - 1,
                        SelectedDate.date.Day
                    ).Call("show");
                )
            );
        }
    }
}

 

ExActivityListener.java
public interface ExActivityListener
{
    public void onRestart();

    public void onStart();

    public void onResume();

    public void onPause();

    public void onStop();

    public void onActivityResult(int requestCode, int resultCode, Intent data);
}

 

ExActivity.java
public class ExActivity extends com.unity3d.player.UnityPlayerActivity
{
    private ExActivityListener listener;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
    }

    public void setListener(ExActivityListener listener)
    {
        this.listener = listener;
    }

    @Override
    puglic void onRestart()
    {
        super.onRestart();
        if(listener != null) listener.onRestart();
    }

    @Override
    puglic void onStart()
    {
        super.onStart();
        if(listener != null) listener.onStart();
    }

    @Override
    puglic void onResume()
    {
        super.onResume();
        if(listener != null) listener.onResume();
    }

    @Override
    puglic void onPause()
    {
        super.onPause();
        if(listener != null) listener.onPause();
    }

    @Override
    puglic void onStop()
    {
        if(listener != null) listener.onStop();
        super.onStop();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if(listener != null) listener.onActivityResult(requestCode, resultCode, data);
    }
}

 

public class Hoge : MonoBehavior
{
    public class ActivityListener : AndroidJavaProxy
    {
        public ActivityListener()
            : base("com.hoge.ExActivityListener")
        {
        }

        puglic void onRestart()
        {
        }

        puglic void onStart()
        {
        }

        puglic void onResume()
        {
        }

        puglic void onPause()
        {
        }

        puglic void onStop()
        {
        }

        public void onActivityResult(int requestCode, int resultCode, AndroidJavaObject data)
        {
        }
    }

    void Awake()
    {
        AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");

        activity.Call("setListener", new ActivityListener());
    }
}

 

猜你喜欢

转载自wang-peng1.iteye.com/blog/2309482