Unity Android插件不起作用

时间:2018-03-06 20:55:13

标签: c# android android-studio unity3d unity5

Android Studio编写的插件正在运行五十五十五,

Android Studio PluginClass.java:



package androidproject.alientechlab.com.unityplugin;



import android.app.AlarmManager;

import android.app.PendingIntent;

import android.content.Context;

import android.content.Intent;

import android.os.SystemClock;

import android.support.v7.app.AppCompatActivity;

import android.widget.Toast;

import com.unity3d.player.UnityPlayer;

import com.unity3d.player.UnityPlayerActivity;



public class PluginClass {



    private Context context;

    private static PluginClass instance;



    public PluginClass() {

        this.instance = this;

    }



    public static PluginClass instance() {

        if(instance == null) {

            instance = new PluginClass();

        }

        return instance;

    }



    public void setContext() {

        this.context = UnityPlayer.currentActivity.getApplicationContext();



    }

    public void basla () {

        Toast.makeText(this.context, "Alarm Başladı", Toast.LENGTH_SHORT).show();



        startAlarm();

    }



    private void startAlarm() {

        AlarmManager manager = (AlarmManager)UnityPlayer.currentActivity.getSystemService(Context.ALARM_SERVICE);

        Intent myIntent;

        PendingIntent pendingIntent;

        myIntent = new Intent (UnityPlayer.currentActivity,AlarmReceiver.class);

        pendingIntent = PendingIntent.getBroadcast(UnityPlayer.currentActivity,0,myIntent,PendingIntent.FLAG_UPDATE_CURRENT);

        manager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+3000,pendingIntent);



    }



    public void showMessage(String message) {

        Toast.makeText(this.context, message, Toast.LENGTH_SHORT).show();

    }



}




Android Studio AlarmReceiver.java:



package androidproject.alientechlab.com.unityplugin;



import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.widget.Toast;

import com.unity3d.player.UnityPlayer;



/**

* Created by Alpaslan on 6.3.2018.

*/



public class AlarmReceiver extends BroadcastReceiver {



    @override

    public void onReceive(Context context, Intent ıntent) {

        Toast.makeText(context,"Devam ediyoruz...",Toast.LENGTH_SHORT).show();



    }

}




并在Unity Script中:



using System.Collections;

using System.Collections.Generic;

using UnityEngine;



public class PluginWrapper : MonoBehaviour {

    private AndroidJavaObject toastExample = null;

    private AndroidJavaObject activityContext = null;



    // Use this for initialization

    void Start () {

        if(toastExample == null) {

           

            using(AndroidJavaClass pluginClass = new AndroidJavaClass("androidproject.alientechlab.com.unityplugin.PluginClass")) {

                if(pluginClass != null) {

                    toastExample = pluginClass.CallStatic<AndroidJavaObject>("instance");

                    toastExample.Call("setContext");

                    toastExample.Call("showMessage", "This is a Toast message");

                        }

            }

        }

    }

    public void basla () {

        using(AndroidJavaClass pluginClass = new AndroidJavaClass("androidproject.alientechlab.com.unityplugin.PluginClass")) {

            toastExample.Call("basla");



        }

    }



}
&#13;
&#13;
&#13;

当我尝试使用apk时,会显示PluginClass的Toast消息,但是Alarm Manager的Toast消息不起作用。函数&#34; basla,setContext和showMessage&#34;工作得很好但是&#34; startAlarm&#34;功能不是或我无法接收报警信息。

...谢谢

https://i.stack.imgur.com/Bl6jN.png

0 个答案:

没有答案