- Home /
Question by
Dante-Sparda · May 25, 2016 at 03:19 PM ·
c#androidunityeditorplugin
Pass the Context between classes inside Android plugin
Greetings.
Im kinda new with android plugins for unity and im trying to create an Alert Dialog after receibing a Broadcast but my application crashes right before that.
Here is my Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.fenrir.broadcastunity"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="9" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="com.test.fenrir.broadcastunity.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.test.fenrir.broadcastunity.permission.C2D_MESSAGE" />
<application
android:label="@string/app_name">
<receiver android:name="MyReceiver" >
<intent-filter>
<action android:name="com.test.fenrir.broadcastunity.Broadcast">
</action>
</intent-filter>
</receiver>
<activity android:name="com.test.fenrir.broadcastunity.MainActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
My Sender Class
public class MySender
{
private Context ctxt;
private String _code;
private static MySender instance;
public int notificationIndex = 0;
public MySender() {
this.instance = this;
}
public static MySender instance() {
if(instance == null) {
instance = new MySender();
}
return instance;
}
public void setContext(Context context) {
this.ctxt = context;
}
public void LaunchBroadcast()
{
Log.i("MySender2", "Trying to launch new broadcast2");
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
intent.setAction("com.test.fenrir.broadcastunity.Broadcast");
ctxt.sendBroadcast(intent);
}
}
My Receiver
public class MyReceiver extends BroadcastReceiver {
Context ctx;
@Override
public void onReceive(Context context, Intent intent) {
this.ctx = context;
Log.i("TestBroadcast", "Si estas leyendo esto, el broadcast funciona");
CallAlert();
}
void CallAlert()
{
ProgressDialog progressDialog = new ProgressDialog(this.ctx);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setMessage("Intent Detected");
progressDialog.setProgress(0);
progressDialog.setMax(100);
progressDialog.show();
}
}
And my C# script for unity
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Broadcast : MonoBehaviour {
public Text _text;
AndroidJavaObject instance = null;
AndroidJavaObject activityInstance = null;
AndroidJavaObject activityContext = null;
public void CallBroadcast()
{
using(AndroidJavaClass activityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
activityContext = activityClass.GetStatic<AndroidJavaObject>("currentActivity");
}
using(AndroidJavaClass pluginClass = new AndroidJavaClass("com.test.fenrir.broadcastunity.MySender")) {
if(pluginClass != null) {
instance = pluginClass.CallStatic<AndroidJavaObject>("instance");
instance.Call("setContext", activityContext);
activityContext.Call("runOnUiThread", new AndroidJavaRunnable(() => {
instance.Call("LaunchBroadcast");
}));
}
}
}
Please help. Pdt: Sorry for the bad english, not my native language
Comment
Your answer
Follow this Question
Related Questions
How to write a java lib for unity as a plugin to make run another android app? 1 Answer
plugins - handling native to C# code? 1 Answer
How to pass an interface to Java from Unity code? 2 Answers
Problem reading sensor plugin, event won't trigger 2 Answers
Need to Include a file in the build and use it in the runtime 0 Answers