Search Unity

Unity Android Plugin isn't work

Discussion in 'Scripting' started by AKComp, Mar 6, 2018.

  1. AKComp

    AKComp

    Joined:
    Aug 22, 2014
    Posts:
    26
    The plugin written in Android Studio is working fifty fifty,

    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 {

    [USER=530556]@override[/USER]
    public void onReceive(Context context, Intent ıntent) {
    Toast.makeText(context,"Devam ediyoruz...",Toast.LENGTH_SHORT).show();

    }
    }


    and In 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");

    }
    }

    }



    When i try my apk, PluginClass's Toast Messages are showing but Alarm Manager's toast Messages isn't work.

    Thanks...

     
  2. AKComp

    AKComp

    Joined:
    Aug 22, 2014
    Posts:
    26
    UP, this problem still there...
     
  3. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    838
  4. AKComp

    AKComp

    Joined:
    Aug 22, 2014
    Posts:
    26
    Sorry for wrong topic, i dont check anything,what is the adb
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Logcat basically.

    But honestly, you should try the Android part of the forums, you might have better luck there. Or if this is a plugin you found somewhere, might see if you can get in touch with the person who wrote it.
     
  6. AKComp

    AKComp

    Joined:
    Aug 22, 2014
    Posts:
    26
    I follow my apk on adb and get this message only, there is no error :
    "Suspiciously short interval 3000 millis; expanding to 60 seconds".